Hacking Secret Ciphers with Python

(Ann) #1
Chapter 21 – Hacking the Vigenère Cipher 341

For the second step of getMostCommonFactors(), we need to sort the values in the
factorCounts dictionary by their count. But dictionaries do not have an order, so we must
first convert the dictionary into a list of two-integer tuples. (We did something similar in Chapter
20 in the getFrequencyOrder() function of the freqAnalaysis.py module.) This list value
will be stored in a variable named factorsByCount, which starts as an empty list on line 9 7.


The for loop on line 98 goes through each of the factors in factorCounts and appends this
(factor, factorCounts[factor]) tuple to the factorsByCount list as long as the
factor is less than or equal to MAX_KEY_LENGTH.


vigenereHacker.py
105. # Sort the list by the factor count.
106. factorsByCount.sort(key=getItemAtIndexOne, reverse=True)
107.
108. return factorsByCount


After the for loop finishes adding all the tuples to factorsByCount, the last step of
getMostCommonFactors() is that the factorsByCount list is sorted on line 106.
Because the getItemAtIndexOne function is passed for the key keyword argument and
True is passed for the reverse keyword argument, the list is sorted in descending order by the
factor counts.


After being sorted, the list in factorsByCount is returned on line 108.


The Kasiski Examination Algorithm........................................................................................................................


vigenereHacker.py
111. def kasiskiExamination(ciphertext):
112. # Find out the sequences of 3 to 5 letters that occur multiple times
113. # in the ciphertext. repeatedSeqSpacings has a value like:
114. # {'EXG': [192], 'NAF': [339, 972, 633], ... }
115. repeatedSeqSpacings = findRepeatSequencesSpacings(ciphertext)


The kasiskiExamination() function returns a list of the most likely key lengths for the
given ciphertext argument. The key lengths are integers in a list, with the first integer in the
list being the most likely key length, the second integer the second most likely, and so on.


The first step is to find the spacings between repeated sequences in the ciphertext. This is
returned from findRepeatSequencesSpacings() as a dictionary with keys of the
sequence strings and values of a list with the spacings as integers.

Free download pdf