Chapter 21 – Hacking the Vigenère Cipher 351
- decryptedText = ''.join(origCase)
Now that we have a complete Vigenère key, lines 197 to 208 will decrypt the ciphertext and
check if the decrypted text is readable English. If it is, then it is printed to the screen for the user
to confirm it is English (since isEnglish() might produce a false positive).
But decryptedText is in all uppercase letters. The code on lines 201 to 207 builds a new
string by appending the origCase list with an uppercase or lowercase form of the letters in
decryptedText. The for loop on line 202 goes through each of the indexes in the
ciphertext string (which, unlike ciphertextUp, has the original casing of the
ciphertext). If ciphertext[i] is uppercase, then the uppercase form of
decryptedText[i] is appended to origCase. Otherwise, the lowercase form of
decryptedText[i] is appended. The list in origCase is then joined together on line 207 to
become the new value of decryptedText.
This table shows how the ciphertext and decryptedText values produce the strings that
go into origCase:
ciphertext Adiz Avtzqeci Tmzubb wsa m Pmilqev halpqavtakuoi
decryptedText ALAN MATHISON TURING WAS A BRITISH MATHEMATICIAN
''.join(origCase) Alan Mathison Turing was a British mathematician
vigenereHacker.py
209. # Check with user to see if the key has been found.
210. print('Possible encryption hack with key %s:' % (possibleKey))
211. print(decryptedText[:200]) # only show first 200 characters
212. print()
213. print('Enter D for done, or just press Enter to continue
hacking:')
214. response = input('> ')
215.
216. if response.strip().upper().startswith('D'):
217. return decryptedText
The correctly-cased decrypted text is printed to the screen for the user to confirm it is English. If
the user enters 'D' then the function returns the decryptedText string.
vigenereHacker.py
219. # No English-looking decryption found, so return None.
220. return None