1.3 A strong substitution cipher
Exercise 1.3 (A strong substitution cipher)
What if instead of shifting the alphabet, you randomly jumbled the letters? Create a program that encodes and decodes messages using this kind of substitution.
Some newspapers publish puzzles like this called cryptograms.
In cryptography, a substitution cipher is a method of encrypting, in which units of plaintext are replaced with the ciphertext, in a defined manner, with the help of a key; the “units” may be single letters (the most common), pairs of letters, triplets of letters, mixtures of the above, and so forth. The receiver deciphers the text by performing the inverse substitution process to extract the original message. Read more here.
The following code is used to generate the key.
# key.py
import secrets
import string
= string.ascii_letters + string.digits
alphabet
def getRandomChar():
= list(alphabet)
l while len(l) > 0:
= secrets.randbelow(len(l))
n = l.pop(n)
retval yield retval
def getRandomKeyForSubstitutionCipher() -> dict[str, str]:
return dict(zip(alphabet, getRandomChar()))
def reverseKey(key: dict[str, str]) -> dict[str, str]:
= dict()
retval for k, v in key.items():
= k
retval[v] return retval
if __name__ == '__main__':
print(getRandomKeyForSubstitutionCipher())
# ex1_3.py
import key as km
def encrypt(plaintext: str, key: dict[str, str]) -> str:
return ''.join(key.get(p,p) for p in plaintext)
def decrypt(ciphertext: str, key: dict[str, str]) -> str:
= km.reverseKey(key=key)
_key return encrypt(plaintext=ciphertext, key=_key)