import obj
from time import clock
# Received encrypted message
message_encrypted = obj.load('data/message_encrypted')
# All keys are known
keys = obj.load('data/keys')
exponent = keys['exponent']
key_public = keys['key_public']
key_private = keys['key_private']
def decrypt(message_encrypted, key_private, key_public):
# Decrypt message
message_ascii = []
for i in message_encrypted:
message_ascii.append((i**key_private) % key_public)
# Convert message to string
message = []
for i in message_ascii:
message.append(chr(i))
# Return decrypted message
return ('').join(message)
begin = clock()
message_decrypted = decrypt(message_encrypted, key_private, key_public)
print('Message decrypted in {:.6} seconds.'.format(clock()-begin))
print('\n')
print(message_decrypted)
The math involved with decrypting the message is very taxing. Even with our small message, it took approx. 10 seconds to finish the decryption process. If the prime factors used to generate the algorithm are known, advanced algorithms can be used to more quickly decrypt the message, but it will still be much longer than the encryption process.
As mentioned before, our keys are much smaller than some modern crypto keys. Even still, the values being managed are quite large. Below is an example of an intermediate calculation on our first encrypted charcter.
big_number = message_encrypted[0]**key_private
print('This number is {:,} digits long!'.format(len(str(big_number))))