Hacking Secret Ciphers with Python

(Ann) #1
Chapter 18 – Hacking the Simple Substitution Cipher 271

But since we know K decrypts to A, we can remove A from the list of potential decryption letters
for cipherletter M. This shortens the list down to just ['D']. Because this new list only has one
string in it, we’ve also solved the cipherletter M!


The removeSolvedLettersFromMapping() function takes a cipherletter mapping and
removes these solved potential decryption letters from the other cipherletters’ lists. Try typing the
following into the interactive shell:





letterMapping =
simpleSubHacker.removeSolvedLettersFromMapping(letterMapping)
intersectedMapping
{'A': [], 'C': ['T'], 'B': ['S'], 'E': [], 'D': [], 'G': ['B'], 'F': [], 'I':
['O'], 'H': ['M'], 'K': ['A'], 'J': [], 'M
': ['D'], 'L': ['N'], 'O': ['U'], 'N': ['L'], 'Q': ['C'], 'P': ['I'], 'S':
['P'], 'R': ['R'], 'U': [], 'T': [], 'W': [],
'V': [], 'Y': [], 'X': ['F'], 'Z': ['E']}





Now when we pass the intersected mapping to decryptWithCipherletterMapping(), it
gives us the full solution. Try typing the following into the interactive shell:





simpleSubHacker.decryptWithCipherletterMapping('OLQIHXIRCKGNZ PLQRZKBZB
MPBKSSIPLC', intersectedMapping)
UNCOMFORTABLE INCREASES DISAPPOINT





The ciphertext OLQIHXIRCKGNZ PLQRZKBZB MPBKSSIPLC decrypts to the message,
“Uncomfortable increases disappoint”.


This is a rather short ciphertext to hack. Normally the encrypted messages we hack will be much
longer. (Messages as short as our example usually cannot be hacked with our word pattern
method.) We’ll have to create a cipherletter mapping for each cipherword in these longer
messages and then intersect them all together, which is exactly what the hackSimpleSub()
function does.


Now that we know the basic steps and what each function does, let’s learn how the code in these
functions work.


How the Program Works..........................................................................................................................................


simpleSubHacker.py



  1. Simple Substitution Cipher Hacker



Free download pdf