You’ll learn about this trick, which is called a one-time pad, in Answers to the practice questions can be found on the book’s website at What does Kasiski examination of a ciphertext reveal?What two changes happen when converting a list value to a set value with the decryptedMessage = vigenereCipher.decryptMessage(subkey,allFreqScores = [[('A', 9), ('E', 5), ('O', 4), ('P', 4)], [('S', 10),('D', 4), ('G', 4), ('H', 4)], [('I', 11), ('V', 4), ('X', 4), ('B', 3)],[('M', 10), ('Z', 5), ('Q', 4), ('A', 3)], [('O', 11), ('B', 4), ('Z', 4),('A', 3)], [('V', 10), ('I', 5), ('K', 5), ('Z', 4)]]itertools.product(range(NUM_MOST_FREQ_LETTERS), repeat=mostLikelyKeyLength) This difference becomes even greater if the Vigenère key had factor is most likely going to be the length of the Vigenère key.The first step is to find the spacings between repeated sequences English frequency match score for each the keys. For the Vigenère cipher, instead of using a numeric key, we will use a letter key. mathematically impossible to break, no matter how powerful your computer or how None.# First, we need to do Kasiski Examination to # Look for this sequence in the rest of the message: 46. factors of each of these numbers (not including one):So the spacings of 8, 8, 24, 32, and 48 expand to this list logician, cryptanalyst, and computer scientist. Higher score [2, 3, 4, 6, 9, 12,# 18, 23, 36, 46, 69, 92, 138, 207], 'ALW': [2, But the hacking program in this book does a pretty good job of reducing billions or trillions of possible keys to mere thousands.However, there is one trick that makes the Vigenère cipher mathematically impossible to break, no matter how powerful your computer or how clever your hacking program is. the development of computerhis death was accidental. 74.     return list(set(factors)) # Remove duplicate factors.To find the most common factors, which are the most likely key lengths, we need to write the {'VRA': [8, 2, 4, 2, 3, 4, 6, 8, 12, 16, 8, 2, 4], 'AZU': [2, 3, 4, 6, 8, 12,[(3, 556), (2, 541), (6, 529), (4, 331), (12, 325), (8, 171), (9, 156), (16,106.     factorsByCount.sort(key=getItemAtIndexOne, reverse=True)Before we can figure out what the possible subkeys are for a ciphertext, we need to know how many subkeys there are. for the user to confirm it is English. decryptedText = vigenereCipher.decryptMessage(possibleKey,The next lines of code print the decryption output to the user to check whether the key has been found:210.             print('Possible encryption hack with key %s:' % (possibleKey))Otherwise, if none of the decryptions look like English, the hacking has failed and the Finally, all of the functions we’ve defined will be used by the The likely key lengths are printed to the screen if Next, we need to find likely subkey letters for each key length. number of lists.# List is sorted by match score. at a time. getNthSubkeysLetters(1, 3, 'ABCABCABC') in the 19If the Vigenère key is an English word it is very easy to [2, 3, 4, 6, 9, 12,# 18, 23, 36, 46, 69, 92, 138, 207], 'ALW': [2, repeated set of letters at least three letters long in the ciphertext. things like sinking convoys in the fog, they will never receive any clear and returns 'CCC'#      getNthSubkeysLetters(1, 5, 'ABCDEFGHI') 'PAEBABANZIAHAKDXAAAKIU'We By We’ll learn about these “one-time pads” in the Its through to mere thousands.However, there is one trick to make the Vigenère cipher Choose whether to encrypt or decrypt (with or without key). He was highly influential in letters for each position# Create a possible key from the letters in The reason is that in the ciphertext, LWM is the plaintext word THE encrypted using the same letters of the key—SPI—because the key happens to repeat at the second THE. But the hacking program in this book does a pretty There are three constants we set on lines 8 to 10 that The key in a Vigenère cipher is a series of letters, such as a single English word. For example, if you encrypted the plaintext THE CAT IS OUT OF THE BAG with the key SPILLTHEBEANS, you’d get:Notice that the letters LWM repeat twice.

from the message.# Compile a list of seqLen-letter sequences found
getUsefulFactors(144)# returns [2, 72, 3, 48, 4, 36, 6, 24, 8, 18, 9, frequency match to English are the ones that are most likely to be the real next chapter.Alan says, “When we want to sink a convey, we send out an freqScores.sort(key=getItemAtIndexOne, reverse=True)176.         allFreqScores.append(freqScores[:NUM_MOST_FREQ_LETTERS])Originally, if we wanted to brute-force through the full Vigenère key, the number of possible keys would be 26 raised to the power of key length.