332 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
- print('Kasiski Examination results say the most likely key lengths
are: ' + keyLengthStr + '\n')
- for keyLength in allLikelyKeyLengths:
- if not SILENT_MODE:
- print('Attempting hack with key length %s (%s possible
keys)...' % (keyLength, NUM_MOST_FREQ_LETTERS ** keyLength)) - hackedMessage = attemptHackWithKeyLength(ciphertext, keyLength)
- if hackedMessage != None:
- break
If none of the key lengths we found using Kasiski Examination
worked, start brute-forcing through key lengths.
- if hackedMessage == None:
- if not SILENT_MODE:
- print('Unable to hack message with likely key length(s).
Brute-forcing key length...') - for keyLength in range(1, MAX_KEY_LENGTH + 1):
don't re-check key lengths already tried from Kasiski
- if keyLength not in allLikelyKeyLengths:
- if not SILENT_MODE:
- print('Attempting hack with key length %s (%s possible
keys)...' % (keyLength, NUM_MOST_FREQ_LETTERS ** keyLength)) - hackedMessage = attemptHackWithKeyLength(ciphertext,
keyLength) - if hackedMessage != None:
- break
- return hackedMessage
If vigenereHacker.py is run (instead of imported as a module) call
the main() function.
- if name == 'main':
- main()
Sample Run of the Vigenère Hacking Program
When you run the vigenereHacker.py program, the output will look like this:
Kasiski Examination results say the most likely key lengths are: 3 2 6 4 12
Attempting hack with key length 3 (27 possible keys)...
Possible letters for letter 1 of the key: A L M
Possible letters for letter 2 of the key: S N O
Possible letters for letter 3 of the key: V I Z