354 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
That’s the full Vigenère hacking program. Whether it is successful or not depends on the
characteristics of the ciphertext. Also, the closer the original plaintext’s letter frequency is to
regular English’s letter frequency and the longer the plaintext, the more likely our hacking
program will work.
Practice Exercises, Chapter 21, Set A
Practice exercises can be found at http://invpy.com/hackingpractice 21 A.
Modifying the Constants of the Hacking Program
There are a few things we can modify if the hacking program doesn’t work though. There are
three constants we set on lines 8 to 10 that affect how our hacking program runs:
vigenereHacker.py
- MAX_KEY_LENGTH = 16 # will not attempt keys longer than this
If the Vigenère key was longer than the integer in MAX_KEY_LENGTH, there is no possible way
the hacking program will find the correct key. However, if we have MAX_KEY_LENGTH set very
high and the kasiskiExamination() function mistakenly thinks that the key length could
be a very large integer, the program could be spending hours (or days or months) attempting to
hack the ciphertext with wrong key lengths.
Trying to hack the wrong key length that is small is not that big of a problem: it will only take
seconds or minutes to go through the likely keys of that length. If the hacking program fails to
hack the ciphertext, try increasing this value and running the program again.
vigenereHacker.py
- NUM_MOST_FREQ_LETTERS = 3 # attempts this many letters per subkey
The NUM_MOST_FREQ_LETTERS limits the number of possible letters tried for each subkey.
By increasing this value, the hacking program tries many more keys (which is needed if the
freqAnalysis.englishFreqMatchScore() was inaccurate for the original plaintext
message), but this will also cause the program to slow down. And setting
NUM_MOST_FREQ_LETTERS to 26 will cause the program to not narrow down the number of
possible letters for each subkey at all!
Table 21-5. Tradeoffs for the MAX_KEY_LENGTH and NUM_MOST_FREQ_LETTERS.
Smaller value: Larger value:
Faster to execute. Slower to execute.
Less likely to hack. More likely to hack.