RSA Encryption | 04 | Decrypt Message

Data decryption is achieved using the private key. Only the intended recipient will have the private key, insuring that even if the data is intercepted, it cannot be read.

Import Libraries and Data

In [1]:
import obj
from time import clock

Recieve message

In [2]:
# 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']

Define decryption algorithm

In [3]:
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)

Decrypt message

In [4]:
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)
Message decrypted in 9.8887 seconds.


Don't tell my secrets to anyone, please!

Inspect decryption process

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.

In [6]:
big_number = message_encrypted[0]**key_private

print('This number is {:,} digits long!'.format(len(str(big_number))))
This number is 927,453 digits long!