Hacking Secret Ciphers with Python

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

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


In our program, a cipherletter mapping dictionary will have 26 keys, one for each letter. The
mapping above has potential decryption letters for 'H', 'G', and 'U' above. The other keys
have no potential decryption letters, which is why they have empty lists for values.


If we reduce the number of potential decryption letters for a cipherletter to just one letter, then we
have solved what that cipherletter decrypts to. Even if we do not solve all 26 cipherletters, we
might be able to hack most of the ciphertext’s cipherletters.


But first we must find the pattern for every word in the dictionary file and sort them in a list so it
will be easy to get a list of all the candidates for a given cipherword’s word pattern. We can use
the same dictionary file from Chapter 12, which you can download from
http://invpy.com/dictionary.txt.


(Note that the terms “word pattern”, “candidate”, and “cipherletter mapping” are terms I came up
with to describe things in this particular hacking program. These are not general cryptography
terms.)


Practice Exercises, Chapter 18, Set A


Practice exercises can be found at http://invpy.com/hackingpractice 18 A.


Source Code of the Word Pattern Module


Since the word patterns for words never change, we can just calculate the word pattern for every
word in a dictionary file once and store them in another file. Our makeWordPatterns.py program
creates a file named wordPatterns.py that will contain a dictionary value with the word pattern for
every word in the dictionary file. Our hacking program can then just import wordPatterns to
look up the candidates for a certain word pattern.


Source code for makeWordPatterns.py




  1. Makes the wordPatterns.py File




  2. http://inventwithpython.com/hacking (BSD Licensed)






  3. Creates wordPatterns.py based on the words in our dictionary




  4. text file, dictionary.txt. (Download this file from




  5. http://invpy.com/dictionary.txt))





  6. import pprint

Free download pdf