Last active
February 15, 2021 17:54
-
-
Save Diapolo10/1b097d4e655ba0eebea3abf28c84fe21 to your computer and use it in GitHub Desktop.
RSA implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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