This program can hack messages encrypted with the Caesar cipher from Project 6, even if you don’t know the key. There are only 26 possible keys for the Caesar cipher, so a computer can easily try all possible decryptions and display the results to the user. In cryptography, we call this technique a brute-force attack. If you’d like to learn more about ciphers and code breaking, you can read my book Cracking Codes with Python (No Starch Press, 2018; https://nostarch.com/crackingcodes/).
When you run caesarhacker.py, the output will look like this:
Caesar Cipher Hacker, by Al Sweigart [email protected]
Enter the encrypted Caesar cipher message to hack.
> QIIX QI FC XLI VSWI FYWLIW XSRMKLX.
Key #0: QIIX QI FC XLI VSWI FYWLIW XSRMKLX.
Key #1: PHHW PH EB WKH URVH EXVKHV WRQLJKW.
Key #2: OGGV OG DA VJG TQUG DWUJGU VQPKIJV.
Key #3: NFFU NF CZ UIF SPTF CVTIFT UPOJHIU.
Key #4: MEET ME BY THE ROSE BUSHES TONIGHT.
Key #5: LDDS LD AX SGD QNRD ATRGDR SNMHFGS.
Key #6: KCCR KC ZW RFC PMQC ZSQFCQ RMLGEFR.
--snip--
Note that lines 20 to 36 in this program are nearly identical to lines 55 to 78 in the Caesar cipher program. The hacking program implements the same decryption code, except that it does so in a for
loop, which runs the code for every possible key.
Unfortunately, the hacking program isn’t sophisticated enough to identify when it has found the correct key. It relies on a human to read the output and identify which decryption produced the original English (or whichever written language was encrypted). Chapter 11 of the book Cracking Codes with Python (No Starch Press, 2018) details how you can write Python code to detect English messages.
1. """Caesar Cipher Hacker, by Al Sweigart [email protected]
2. This programs hacks messages encrypted with the Caesar cipher by doing
3. a brute force attack against every possible key.
4. More info at:
5. https://en.wikipedia.org/wiki/Caesar_cipher#Breaking_the_cipher
6. This code is available at https://nostarch.com/big-book-small-python-programming
7. Tags: tiny, beginner, cryptography, math"""
8.
9. print('Caesar Cipher Hacker, by Al Sweigart [email protected]')
10.
11. # Let the user specify the message to hack:
12. print('Enter the encrypted Caesar cipher message to hack.')
13. message = input('> ')
14.
15. # Every possible symbol that can be encrypted/decrypted:
16. # (This must match the SYMBOLS used when encrypting the message.)
17. SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
18.
19. for key in range(len(SYMBOLS)): # Loop through every possible key.
20. translated = ''
21.
22. # Decrypt each symbol in the message:
23. for symbol in message:
24. if symbol in SYMBOLS:
25. num = SYMBOLS.find(symbol) # Get the number of the symbol.
26. num = num - key # Decrypt the number.
27.
28. # Handle the wrap-around if num is less than 0:
29. if num < 0:
30. num = num + len(SYMBOLS)
31.
32. # Add decrypted number's symbol to translated:
33. translated = translated + SYMBOLS[num]
34. else:
35. # Just add the symbol without decrypting:
36. translated = translated + symbol
37.
38. # Display the key being tested, along with its decrypted text:
39. print('Key #{}: {}'.format(key, translated))
After entering the source code and running it a few times, try making experimental changes to it. Keep in mind that the string stored in the SYMBOLS
variable must match the SYMBOLS
variable in the Caesar cipher program that produced the encrypted text.
Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.
translated = ''
on line 20?translated = translated + SYMBOLS[num]
on line 33 to translated = translated + symbol
?