Hacking Secret Ciphers with Python

(Ann) #1
Chapter 20 – Frequency Analysis 309



  1. frequently occurring in the message parameter.






  2. first, get a dictionary of each letter and its frequency count



  3. letterToFreq = getLetterCount(message)


The getFrequencyOrder() function will return a string with the 26 uppercase letters of the
alphabet arranged in order of how frequently they appear in the message parameter. If
message is readable English instead of random gibberish, this string will most likely be similar
(if not identical to) the string in the ETAOIN constant.


For example, if the “Alan Mathison Turing was a British mathematician...” text from Chapter
19’s vigenereCipher.py program was passed as a string to getFrequencyOrder(), the
function would return the string 'ETIANORSHCLMDGFUPBWYVKXQJZ' because E is the most
common letter in that paragraph, followed by T, then I, then A, and so on.


This function is somewhat complicated, but it breaks down to five simple steps.


The first step of getFrequencyOrder(), line 34 gets a dictionary value of the letter
frequency count from getLetterCount() for the string in the message parameter. (The
getLetterCount() function was described previously.)


If the “Alan Mathison Turing...” text was passed as a string value for the message parameter,
then line 34 would assign letterToFreq the dictionary value, {'A': 135, 'C': 74,
'B': 30, 'E': 196, 'D': 58, 'G': 39, 'F': 37, 'I': 139, 'H': 87, 'K': 8,
'J': 2, 'M': 58, 'L': 62, 'O': 113, 'N': 122, 'Q': 2, 'P': 36, 'S': 89,
'R': 106, 'U': 37, 'T': 140, 'W': 30, 'V': 14, 'Y': 21, 'X': 3, 'Z': 1}.


freqAnalysis.py
36. # second, make a dictionary of each frequency count to each letter(s)
37. # with that frequency
38. freqToLetter = {}
39. for letter in LETTERS:
40. if letterToFreq[letter] not in freqToLetter:
41. freqToLetter[letterToFreq[letter]] = [letter]
42. else:
43. freqToLetter[letterToFreq[letter]].append(letter)


For the second step of getFrequencyOrder(), while the letterToFreq dictionary has
keys of each of the 26 letters and values of their frequency count, what we need is a dictionary
value that maps the opposite: a dictionary where the keys are the frequency count and values are a
list of letters that appear that many times. While the letterToFreq dictionary maps letter keys

Free download pdf