Skip to content

Instantly share code, notes, and snippets.

@Diapolo10
Last active February 15, 2021 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Diapolo10/1b097d4e655ba0eebea3abf28c84fe21 to your computer and use it in GitHub Desktop.
Save Diapolo10/1b097d4e655ba0eebea3abf28c84fe21 to your computer and use it in GitHub Desktop.
RSA implementation
#!/usr/bin/env python3
from typing import List
class RSAConfig:
def __init__(self, p, q, e, d):
self.p = p
self.q = q
self.n = p * q
self.phi = (p-1) * (q-1)
self.e = e
self.d = d
# self.alphabet = alphabet
def encrypt(self, nums: List[int]) -> List[int]:
return [
(chunk ** self.e) % self.n
for chunk in nums
]
def decrypt(self, nums: List[int]) -> List[int]:
return [
(chunk ** self.d) % self.n
for chunk in nums
]
if __name__ == '__main__':
rsa = RSAConfig(11, 17, 19, 59)
message = [18, 91, 24, 2]
# Just to confirm it works as expected
assert rsa.decrypt(rsa.encrypt(message)) == message
print(f"Original message: {message}")
print(f"Encrypted: {(encrypted := rsa.encrypt(message))}")
print(f"Decrypted encrypted: {rsa.decrypt(encrypted)}")
print(f"Decrypted original: {rsa.decrypt(message)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment