Chapter 21 – Hacking the Vigenère Cipher 319
There are two different methods to hack the Vigenère cipher. The first is a brute-force attack that
tries every word in the dictionary file as the Vigenère key. This method will only work if an
English word like “RAVEN” or “DESK” was used for the key instead of a random key like
“VUWFE” or “PNFJ”. The second is a more sophisticated method that works even if a random
key was used. The earliest record of its use was by the mathematician Charles Babbage in the 19th
century.
The Dictionary Attack
If the Vigenère key is an English word it is very easy to memorize. But never use an English
word for the encryption key. This makes your ciphertext vulnerable to a dictionary attack.
A dictionary attack is a brute-force technique where a hacker attempts to decrypt the ciphertext
using the words from a dictionary file as the keys. The dictionary.txt dictionary file available on
this book’s website (at http://invpy.com/dictionary.txt)) has about 45 ,000 English words. It takes
less than 5 minutes for my computer to run through all of these decryptions for a message the size
of a long paragraph.
Source Code for a Vigenère Dictionary Attack Program
Open a new file editor window by clicking on File ► New Window. Type in the following code
into the file editor, and then save it as vigenereDictionaryHacker.py. Press F5 to run the program.
Note that first you will need to download the pyperclip.py module and place this file in the same
directory as the vigenereDictionaryHacker.py file. You can download this file from
http://invpy.com/pyperclip.py.
Source code for vigenereDictionaryHacker.py
Vigenere Cipher Dictionary Hacker
http://inventwithpython.com/hacking (BSD Licensed)
- import detectEnglish, vigenereCipher, pyperclip
- def main():
- ciphertext = """Tzx isnz eccjxkg nfq lol mys bbqq I lxcz."""
- hackedMessage = hackVigenere(ciphertext)
- if hackedMessage != None:
- print('Copying hacked message to clipboard:')
- print(hackedMessage)
- pyperclip.copy(hackedMessage)
- else:
- print('Failed to hack encryption.')