Hacking Secret Ciphers with Python

(Ann) #1

306 http://inventwithpython.com/hacking


Email questions to the author: [email protected]




  1. third, put each list of letters in reverse "ETAOIN" order, and then




  2. convert it to a string



  3. for freq in freqToLetter:

  4. freqToLetter[freq].sort(key=ETAOIN.find, reverse=True)

  5. freqToLetter[freq] = ''.join(freqToLetter[freq])




  6. fourth, convert the freqToLetter dictionary to a list of tuple




  7. pairs (key, value), then sort them



  8. freqPairs = list(freqToLetter.items())

  9. freqPairs.sort(key=getItemAtIndexZero, reverse=True)




  10. fifth, now that the letters are ordered by frequency, extract all




  11. the letters for the final string



  12. freqOrder = []

  13. for freqPair in freqPairs:

  14. freqOrder.append(freqPair[1])



  15. return ''.join(freqOrder)





  16. def englishFreqMatchScore(message):


  17. Return the number of matches that the string in the message




  18. parameter has when its letter frequency is compared to English




  19. letter frequency. A "match" is how many of its six most frequent




  20. and six least frequent letters is among the six most frequent and




  21. six least frequent letters for English.



  22. freqOrder = getFrequencyOrder(message)



  23. matchScore = 0


  24. Find how many matches for the six most common letters there are.



  25. for commonLetter in ETAOIN[:6]:

  26. if commonLetter in freqOrder[:6]:

  27. matchScore += 1


  28. Find how many matches for the six least common letters there are.



  29. for uncommonLetter in ETAOIN[-6:]:

  30. if uncommonLetter in freqOrder[-6:]:

  31. matchScore += 1



  32. return matchScore


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


freqAnalysis.py



  1. Frequency Finder




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





Free download pdf