import obj
from time import clock
# Create secret message
message = 'Don\'t tell my secrets to anyone, please!'
# Save message
obj.save(message, 'data/message')
message
keys = obj.load('data/keys')
#Publicly known values
exponent = keys['exponent']
key_public = keys['key_public']
#Private values are unknown
key_private = 'Unknown'
Encrypting is a mathematic process that can only be achieved on numeric data. Our text string must be converted to a numeric representation before encryption can occur. Fortunately, computers have always needed numeric representations of text and therefore we have many options to choose from. Instead of creating our own translation system, we will use ASCII for our conversion which handles typical American letters and symbols.
def encrypt(message, key_public, exp):
# Convert message to ASCII
message_ascii = []
for i in message:
message_ascii.append(ord(i))
# Encrypt message
message_encrypted = []
for i in message_ascii:
message_encrypted.append((i**exp) % key_public)
# Return encrypted message
return message_encrypted
begin = clock()
message_encrypted = encrypt(message, key_public, exponent)
print('Message encrypted in {:.6} seconds.'.format(clock()-begin))
print('\n')
print(message_encrypted)
obj.save(message_encrypted, 'data/message_encrypted')