Chapter 21 – Hacking the Vigenère Cipher 335
...skipped for brevity...
at Haq 2012 i bfdvsbq azmtmd'g widt ion bwnafz tzm Tcpsw wr Zjrva ivdcz eaigd
yzmbo Tmzubb a kbmhptgzk dvrvwz wa efiohzd."""
18. hackedMessage = hackVigenere(ciphertext)
19.
20. if hackedMessage != None:
21. print('Copying hacked message to clipboard:')
22. print(hackedMessage)
23. pyperclip.copy(hackedMessage)
24. else:
25. print('Failed to hack encryption.')
The main() function of the hacking program is similar to the main() functions of previous
hacking functions. The ciphertext is passed to the hackVigenere() cipher, which either
returns the decrypted string (if the hack was successful) or the None value (if the hack failed). If
successful, the hacked message is printed to the screen and copied to the clipboard.
Finding Repeated Sequences
vigenereHacker.py
28. def findRepeatSequencesSpacings(message):
29. # Goes through the message and finds any 3 to 5 letter sequences
30. # that are repeated. Returns a dict with the keys of the sequence and
31. # values of a list of spacings (num of letters between the repeats).
32.
33. # Use a regular expression to remove non-letters from the message.
34. message = NONLETTERS_PATTERN.sub('', message.upper())
35.
36. # Compile a list of seqLen-letter sequences found in the message.
37. seqSpacings = {} # keys are sequences, values are list of int spacings
38. for seqLen in range(3, 6):
The findRepeatSequencesSpacings() locates all the repeated sequences of letters in the
message string and counts the spacings (that is, the number of letters) between the sequences.
First, line 3 4 converts the message to uppercase and removes any non-letter characters from
message using the sub() regular expression method.
The seqSpacings dictionary will have keys of the sequence strings and values of a list with
the integer number of letters between all the occurrences of that sequence in the key. The
previous “PPQCAXQV...” example string from earlier in the “Kasiski Examination, Step 1”
section, if passed as message, would cause findRepeatSequenceSpacings() to return
{'VRA': [8, 24, 32], 'AZU': [48], 'YBN': [8]}.