Chapter 18 – Hacking the Simple Substitution Cipher 267
wordPat = makeWordPatterns.getWordPattern('OLQIHXIRCKGNZ')
wordPat
0.1.2.3.4.5.3.6.7.8.9.10.11
To figure out which English words in the dictionary have the word pattern
0.1.2.3.4.5.3.6.7.8.9.10.11 (that is, to figure out the candidates for the cipherword
OLQIHXIRCKGNZ) we will import the wordPatterns module and look up this pattern. Try
typing the following into the interactive shell:
import wordPatterns
candidates = wordPatterns.allPatterns['0.1.2.3.4.5.3.6.7.8.9.10.11']
candidates
['UNCOMFORTABLE', 'UNCOMFORTABLY']
There are two English words that OLQIHXIRCKGNZ could decrypt to (that is, only two English
words that have the same word pattern that OLQIHXIRCKGNZ does): UNCOMFORTABLE and
UNCOMFORTABLY. (It’s also possible that the cipherword decrypts to a word that does not
exist in our dictionary, but we will just have to assume that’s not the case.) We need to create a
cipherletter mapping that has the cipherletters in OLQIHXIRCKGNZ map to letters in
UNCOMFORTABLE and UNCOMFORTABLY as potential decryption letters. That is, O maps
to U, L maps to N, Q maps to C, and so on. Z will map to two different letters: E and Y.
We can do this with the addLettersToMapping() function. We will need to pass it our
(currently blank) cipherletter mapping in letterMapping1, the string 'OLQIHXIRCKGNZ',
and the string 'UNCOMFORTABLE' (which is the first string in the candidates list). Try
typing the following into the interactive shell:
letterMapping1 = simpleSubHacker.addLettersToMapping(letterMapping1,
'OLQIHXIRCKGNZ', candidates[0])
letterMapping1
{'A': [], 'C': ['T'], 'B': [], 'E': [], 'D': [], 'G': ['B'], 'F': [], 'I':
['O'], 'H': ['M'], 'K': ['A'], 'J': [], 'M': [], 'L': ['N'], 'O': ['U'], 'N':
['L'], 'Q': ['C'], 'P': [], 'S': [], 'R': ['R'], 'U': [], 'T': [], 'W': [],
'V': [], 'Y': [], 'X': ['F'], 'Z': ['E']}
From the letterMapping1 value, you can see that the letters in OLQIHXIRCKGNZ map to
the letters in UNCOMFORTABLE: 'O' maps to ['U'], 'L' maps to ['N'], 'Q' maps to
['C'], and so on.