Hacking Secret Ciphers with Python

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

'W': ['C'],
'X': ['N'],
'Y': ['F'],
'Z': ['Z']}


In the above example, the cipherletters A, C, I, J, L, M, N, O, P, Q, R, S, T, U, X, Y, and Z all
have one and only one potential decryption letter. These cipher letters have been solved. The
decryptWithCipherletterMapping() function, explained later, will print underscores
for any cipherletters that have not been solved (that is, B, D, E, F, G, H, K, and V.)


simpleSubHacker.py
2 4. print('Original ciphertext:')
2 5. print(message)
2 6. print()


First the original encrypted message is displayed on the screen so the programmer can compare it
to the decryption.


simpleSubHacker.py
2 7. print('Copying hacked message to clipboard:')
2 8. hackedMessage = decryptWithCipherletterMapping(message, letterMapping)
2 9. pyperclip.copy(hackedMessage)
3 0. print(hackedMessage)


Next the decrypted message is returned from the decryptWithCipherletterMapping()
function on line 28. This hacked message is copied to the clipboard on line 29 and printed to the
screen on line 30.


Next, let’s look at all the functions that are called by main().


Blank Cipherletter Mappings


simpleSubHacker.py
3 3. def getBlankCipherletterMapping():
3 4. # Returns a dictionary value that is a blank cipherletter mapping.
3 5. return {'A': [], 'B': [], 'C': [], 'D': [], 'E': [], 'F': [], 'G': [],
'H': [], 'I': [], 'J': [], 'K': [], 'L': [], 'M': [], 'N': [], 'O': [], 'P':
[], 'Q': [], 'R': [], 'S': [], 'T': [], 'U': [], 'V': [], 'W': [], 'X': [],
'Y': [], 'Z': []}


Our program will need a cipherletter mapping for each cipherword in the ciphertext, so we will
create the getBlankCipherletterMapping() function which can return a new, blank
mapping when called.

Free download pdf