286 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
Summary
Whew! This hacking program was fairly complicated. The cipherletter mapping is the main tool
for modeling the possible letters that each ciphertext letter can decrypt to. By adding letters
(based on the candidates for each cipherword) to the mapping, and then intersecting mappings
and removing solved letters from other lists of potential decryption letters, we can narrow down
the number of possible keys. Instead of trying all 403 , 291 , 461 , 126 , 605 , 635 , 584 , 000 , 000 possible
keys we can use some sophisticated Python code to figure out exactly what most (if not all) of the
original simple substitution key was.
The main strength of the simple substitution cipher is the large number of possible keys. But the
downfall is that it is easy enough to compare the cipherwords to words in a dictionary file to
slowly figure out which cipherletters decrypt to which letters. The next chapter’s cipher is much
more powerful. For several hundred years, it was considered impossible to break. It is a
“polyalphabetic” substitution cipher called the Vigenère cipher.