Hacking Secret Ciphers with Python

(Ann) #1

170 http://inventwithpython.com/hacking


Email questions to the author: [email protected]







  1. if possibleWords == []:

  2. return 0.0 # no words at all, so return 0.0



  3. matches = 0

  4. for word in possibleWords:

  5. if word in ENGLISH_WORDS:

  6. matches += 1

  7. return float(matches) / len(possibleWords)





  8. def removeNonLetters(message):

  9. lettersOnly = []

  10. for symbol in message:

  11. if symbol in LETTERS_AND_SPACE:

  12. lettersOnly.append(symbol)

  13. return ''.join(lettersOnly)





  14. def isEnglish(message, wordPercentage=20, letterPercentage= 85 ):


  15. By default, 20% of the words must exist in the dictionary file, and




  16. 85% of all the characters in the message must be letters or spaces




  17. (not punctuation or numbers).



  18. wordsMatch = getEnglishCount(message) * 100 >= wordPercentage

  19. numLetters = len(removeNonLetters(message))

  20. messageLettersPercentage = float(numLetters) / len(message) * 100

  21. lettersMatch = messageLettersPercentage >= letterPercentage

  22. return wordsMatch and lettersMatch


How the Program Works..........................................................................................................................................


detectEnglish.py



  1. Detect English module




  2. http://inventwithpython.com/hacking (BSD Licensed)






  3. To use, type this code:




  4. import detectEnglish




  5. detectEnglish.isEnglish(someString) # returns True or False




  6. (There must be a "dictionary.txt" file in this directory with all English




  7. words in it, one word per line. You can download this from




  8. http://invpy.com/dictionary.txt))




These comments at the top of the file give instructions to programmers on how to use this
module. They give the important reminder that if there is no file named dictionary.txt in the same

Free download pdf