Hacking Secret Ciphers with Python

(Ann) #1

332 http://inventwithpython.com/hacking


Email questions to the author: [email protected]



  1. print('Kasiski Examination results say the most likely key lengths
    are: ' + keyLengthStr + '\n')



  2. for keyLength in allLikelyKeyLengths:

  3. if not SILENT_MODE:

  4. print('Attempting hack with key length %s (%s possible
    keys)...' % (keyLength, NUM_MOST_FREQ_LETTERS ** keyLength))

  5. hackedMessage = attemptHackWithKeyLength(ciphertext, keyLength)

  6. if hackedMessage != None:

  7. break




  8. If none of the key lengths we found using Kasiski Examination




  9. worked, start brute-forcing through key lengths.



  10. if hackedMessage == None:

  11. if not SILENT_MODE:

  12. print('Unable to hack message with likely key length(s).
    Brute-forcing key length...')

  13. for keyLength in range(1, MAX_KEY_LENGTH + 1):


  14. don't re-check key lengths already tried from Kasiski



  15. if keyLength not in allLikelyKeyLengths:

  16. if not SILENT_MODE:

  17. print('Attempting hack with key length %s (%s possible
    keys)...' % (keyLength, NUM_MOST_FREQ_LETTERS ** keyLength))

  18. hackedMessage = attemptHackWithKeyLength(ciphertext,
    keyLength)

  19. if hackedMessage != None:

  20. break

  21. return hackedMessage






  22. If vigenereHacker.py is run (instead of imported as a module) call




  23. the main() function.



  24. if name == 'main':

  25. 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

Free download pdf