90 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
- LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
loop through every possible key
- for key in range(len(LETTERS)):
It is important to set translated to the blank string so that the
previous iteration's value for translated is cleared.
- translated = ''
The rest of the program is the same as the original Caesar program:
run the encryption/decryption code on each symbol in the message
- for symbol in message:
- if symbol in LETTERS:
- num = LETTERS.find(symbol) # get the number of the symbol
- num = num - key
handle the wrap-around if num is 26 or larger or less than 0
- if num < 0:
- num = num + len(LETTERS)
add number's symbol at the end of translated
- translated = translated + LETTERS[num]
- else:
just add the symbol without encrypting/decrypting
- translated = translated + symbol
display the current key being tested, along with its decryption
- print('Key #%s: %s' % (key, translated))
You will see that much of this code is the same as the code in the original Caesar cipher program.
This is because the Caesar cipher hacker program does the same steps to decrypt the key.
Sample Run of the Caesar Cipher Hacker Program
Here is what the Caesar cipher program looks like when you run it. It is trying to break the
ciphertext, “GUVF VF ZL FRPERG ZRFFNTR.” Notice that the decrypted output for key 13 is
plain English, so the original encryption key must have been 13.
Key #0: GUVF VF ZL FRPERG ZRFFNTR.
Key #1: FTUE UE YK EQODQF YQEEMSQ.
Key #2: ESTD TD XJ DPNCPE XPDDLRP.
Key #3: DRSC SC WI COMBOD WOCCKQO.
Key #4: CQRB RB VH BNLANC VNBBJPN.