Hacking Secret Ciphers with Python

(Ann) #1

274 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


Like all our previous hacking programs, the main() function will store the ciphertext to be
hacked in a variable named message. We will pass this variable to the hackSimpleSub()
function. However, unlike our previous hacking programs, the hacking function will not return a
string of the decrypted message (or None if it was unable to decrypt it).


Instead, hackSimpleSub() will return a cipherletter mapping (specifically, an intersected
cipherletter mapping that had the solved letters removed, like the kind we made in our interactive
shell exercise). This returned cipherletter mapping will be passed to
decryptWithCipherletterMapping() to decrypt the ciphertext in message.


Partially Hacking the Cipher


simpleSubHacker.py
2 0. # Display the results to the user.
2 1. print('Mapping:')
2 2. pprint.pprint(letterMapping)
2 3. print()


Since the cipherletter mapping stored in letterMapping is a dictionary, we can use the
pprint.pprint() “pretty print” function to display it on the screen. It will look something
like this:


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

Free download pdf