His method was later published by Friedrich Kasiski, an early 20th-century mathematician who became the namesake of the method. Charles Babbage was the first person to have broken the Vigenère cipher using this process, but he never published his results. We can then use frequency analysis to break each of the subkeys independently. Kasiski examination is a process that we can use to determine the length of the Vigenère key used to encrypt a ciphertext. Using Kasiski Examination to Find the Key’s Length
Now that we’ve written a program that hacks the Vigenère cipher using a dictionary attack, let’s look at how to hack the Vigenère cipher even when the key is a random group of letters rather than a dictionary word. A for loop will iterate over each word in the words list, decrypt the message with the word as the key, and then call detectEnglish.isEnglish() to see whether the result is understandable English text. The rest of the program, from lines 23 to 36, works similarly to the transposition cipher–hacking program in Chapter 12. Since there is one word in each line of the dictionary file, the words variable contains a list of every English word from Aarhus to Zurich.
Unlike the read() method, which returns the full contents of the file as a single string, the readlines() method returns a list of strings, where each string is a single line from the file. Note that this program uses the readlines() method on file objects returned from open(): Briefly, the hackVigenereDictionary() function attempts to use each word in the dictionary file to decrypt the ciphertext, and when the decrypted text looks like English (according to the detectEnglish module), it prints the decryption and prompts the user to quit or continue.
About the Vigenère Dictionary Hacker Programīecause the source code for the vigenereDictionaryHacker.py program is similar to previous hacking programs in this book, I won’t explain it line by line. The first keyword the program suggests ( ASTROLOGY) doesn’t work, so the user presses enter to let the hacking program continue until it finds the correct decryption key ( ASTRONOMY). The real secrets are not the ones I tell. Key ASTRONOMY: The real secrets are not the ones I tell. Key ASTROLOGY: The recl yecrets crk not the qnks I tell.Įnter D for done, or just press Enter to continue breaking: When you run the vigenereDictionaryHacker.py program, the output should look like this: main() Sample Run of the Vigenère Dictionary Hacker Program
print('Enter D for done, or just press Enter to continueģ9. print('Key ' + str(word) + ': ' + decryptedText)ģ2. # Check with user to see if the decrypted key has been found:ģ0. if detectEnglish.isEnglish(decryptedText, wordPercentage=40):Ģ7. decryptedText = cryptMessage(word, ciphertext)Ģ6. word = word.strip() # Remove the newline at the end.Ģ5.
print('Copying hacked message to clipboard:')ġ8. hackedMessage = hackVigenereDictionary(ciphertext)ġ1. ciphertext = """Tzx isnz eccjxkg nfq lol mys bbqq I lxcz."""Ĩ. import detectEnglish, vigenereCipher, pyperclipħ. Be sure to place the detectEnglish.py, vigenereCipher.py, and pyperclip.py files in the same directory as the vigenereDictionaryHacker.py file. Enter the following code into the file editor, and then save it as vigenereDictionaryHacker.py. Open a new file editor window by selecting File ▸ New File. Source Code for the Vigenère Dictionary Hacker Program Let’s look at the source code for a program that uses a dictionary attack to hack the Vigenère cipher. This means that if an English word is used to encrypt a Vigenère ciphertext, the ciphertext is vulnerable to a dictionary attack. It takes less than five minutes for my computer to run through all these decryptions for a message the size of a long paragraph. The dictionary file dictionary.txt (available on this book’s website at ) has approximately 45,000 English words. We’ll first use the dictionary attack to hack the Vigenère cipher. Using a Dictionary Attack to Brute-Force the Vigenère Cipher