Hacking Secret Ciphers with Python

(Ann) #1

320 http://inventwithpython.com/hacking


Email questions to the author: [email protected]







  1. def hackVigenere(ciphertext):

  2. fo = open('dictionary.txt')

  3. words = fo.readlines()

  4. fo.close()



  5. for word in words:

  6. word = word.strip() # remove the newline at the end

  7. decryptedText = vigenereCipher.decryptMessage(word, ciphertext)

  8. if detectEnglish.isEnglish(decryptedText, wordPercentage=40):


  9. Check with user to see if the decrypted key has been found.



  10. print()

  11. print('Possible encryption break:')

  12. print('Key ' + str(word) + ': ' + decryptedText[:100])

  13. print()

  14. print('Enter D for done, or just press Enter to continue
    breaking:')

  15. response = input('> ')



  16. if response.upper().startswith('D'):

  17. return decryptedText



  18. if name == 'main':

  19. main()


Sample Run of the Vigenère Dictionary Hacker Program


When you run this program the output will look like this:


Possible encryption break:
Key ASTROLOGY: The recl yecrets crk not the qnks I tell.


Enter D for done, or just press Enter to continue breaking:




Possible encryption break:
Key ASTRONOMY: The real secrets are not the ones I tell.


Enter D for done, or just press Enter to continue breaking:



d
Copying hacked message to clipboard:
The real secrets are not the ones I tell.



The first keyword it suggests (“ASTROLOGY”) doesn’t quite work, so the user presses Enter to
let the hacking program continue until it gets the correct decryption key (“ASTRONOMY”).

Free download pdf