350 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
Table 21-4. Value of indexes on each iteration.
On the 1st iteration, indexes is set to: (0, 0, 0, 0, 0)^
On the 2nd iteration, indexes is set to: (0, 0, 0, 0, 1)^
On the 3rd iteration, indexes is set to: (0, 0, 0, 0, 2)^
On the 4th iteration, indexes is set to: (0, 0, 0, 1, 0)^
On the 5th iteration, indexes is set to: (0, 0, 0, 1, 1)^
And so on...
vigenereHacker.py
189. # Create a possible key from the letters in allFreqScores
190. possibleKey = ''
191. for i in range(mostLikelyKeyLength):
192. possibleKey += allFreqScores[i][indexes[i]][0]
The full Vigenère key will be constructed from the subkeys in allFreqScores using the
indexes supplied by indexes. The key starts off as a blank string on line 190, and the for loop
on line 191 will iterate through the integers from 0 up to, but not including,
mostLikelyKeyLength.
As the i variable changes for each iteration of the for loop, the value at indexes[i] will be
the index of the tuple we want to use in allFreqScores[i]. This is why
allFreqScores[i][indexes[i]] evaluates to the correct tuple we want (and the subkey
we want is at index 0 in that tuple).
vigenereHacker.py
194. if not SILENT_MODE:
195. print('Attempting with key: %s' % (possibleKey))
If SILENT_MODE is False, the key created by the for loop on line 191 is printed to the
screen.
vigenereHacker.py
197. decryptedText = vigenereCipher.decryptMessage(possibleKey,
ciphertextUp)
198.
- if detectEnglish.isEnglish(decryptedText):
Set the hacked ciphertext to the original casing.
- origCase = []
- for i in range(len(ciphertext)):
- if ciphertext[i].isupper():
- origCase.append(decryptedText[i].upper())
- else:
- origCase.append(decryptedText[i].lower())