Welcome to the world of Cryptography.
This is the world of Bob, Alice and Eve.
We have two kinds of code:
King Bob wants to send a secret to Queen Alice. Therefore, he sends her a decoder ring, or something similar, that allows her to decode Bob's forthcoming message.
from string import ascii_lowercase
ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
from random import shuffle
letters = list(ascii_lowercase + " ")
print(letters)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ']
copy_of_letters = letters[:] # slice with everything
print(copy_of_letters)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ']
shuffle(letters)
print(letters)
['p', 'e', 'q', 'v', 'r', 'c', 's', 'l', 'b', 'x', 'g', 'n', 'w', 'h', 'd', 'f', 'm', 'y', 'o', 'u', 'j', 'z', ' ', 'a', 'i', 'k', 't']
print(copy_of_letters)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ']
secret_key = dict(zip(copy_of_letters, letters))
print(secret_key)
{'a': 'p', 'b': 'e', 'c': 'q', 'd': 'v', 'e': 'r', 'f': 'c', 'g': 's', 'h': 'l', 'i': 'b', 'j': 'x', 'k': 'g', 'l': 'n', 'm': 'w', 'n': 'h', 'o': 'd', 'p': 'f', 'q': 'm', 'r': 'y', 's': 'o', 't': 'u', 'u': 'j', 'v': 'z', 'w': ' ', 'x': 'a', 'y': 'i', 'z': 'k', ' ': 't'}
secret_key['r']
'y'
def encrypt(m: str, key: dict) -> str:
c = [] # will become ciphertext
for letter in m:
new_letter = key[letter]
c.append(new_letter)
return "".join(c)
m = "i will attack at dawn eve will not know"
c = encrypt(m, secret_key)
c
'bt bnntpuupqgtputvp htrzrt bnnthdutghd '
def decrypt(c, key):
rev_dict = dict(zip(key.values(), key.keys()))
return encrypt(c, rev_dict)
decrypt(c, secret_key)
'i will attack at dawn eve will not know'
Queen Alice wants to send a secure message King Bob, A to B. Eve is threat in the middle.
Alice uses Bob's "public key" to encrypt her message. Only Bob can decrypt messages sent using that key to scramble.
We will use Euler's Theorem to construct our algorithm (RSA).
RSA232 = \
17969491597941066732916128449573246156367561808012600070888918835531726460341490933493372247868650755230855864199929221814436684722874052065257937495694348389263171152522525654410980819170611742509702440718010364831638288518852689
p = 4528450358010492026612439739120166758911246047493700040073956759261590397250033699357694507193523000343088601688589
q = 3968132623150957588532394439049887341769533966621957829426966084093049516953598120833228447171744337427374763106901
if RSA232 == p*q:
print("Check!")
Check!
Bob's public key, N, is not factorable using any algorithm we currently have. If you know p, q you can derive d. But otherwise, without d, you will never be able to decrypt a message encrypted using N.