2 from itertools
import starmap
, cycle
4 from cipher
.caesar
import *
5 from support
.utilities
import *
6 from support
.language_models
import *
8 from logger
import logger
10 def vigenere_encipher(message
, keyword
):
13 >>> vigenere_encipher('hello', 'abc')
16 shifts
= [pos(l
) for l
in sanitise(keyword
)]
17 pairs
= zip(message
, cycle(shifts
))
18 return cat([caesar_encipher_letter(l
, k
) for l
, k
in pairs
])
20 def vigenere_decipher(message
, keyword
):
23 >>> vigenere_decipher('hfnlp', 'abc')
26 shifts
= [pos(l
) for l
in sanitise(keyword
)]
27 pairs
= zip(message
, cycle(shifts
))
28 return cat([caesar_decipher_letter(l
, k
) for l
, k
in pairs
])
31 def beaufort_encipher(message
, keyword
):
34 >>> beaufort_encipher('inhisjournaldatedtheidesofoctober', 'arcanaimperii')
35 'sevsvrusyrrxfayyxuteemazudmpjmmwr'
37 shifts
= [pos(l
) for l
in sanitise(keyword
)]
38 pairs
= zip(message
, cycle(shifts
))
39 return cat([unpos(k
- pos(l
)) for l
, k
in pairs
])
41 beaufort_decipher
= beaufort_encipher
43 beaufort_variant_encipher
=vigenere_decipher
44 beaufort_variant_decipher
=vigenere_encipher
47 def vigenere_keyword_break_mp(message
, wordlist
=keywords
, fitness
=Pletters
,
49 """Breaks a vigenere cipher using a dictionary and frequency analysis.
51 >>> vigenere_keyword_break_mp(vigenere_encipher(sanitise('this is a test ' \
52 'message for the vigenere decipherment'), 'cat'), \
53 wordlist=['cat', 'elephant', 'kangaroo']) # doctest: +ELLIPSIS
54 ('cat', -52.9472712...)
56 with multiprocessing
.Pool() as pool
:
57 helper_args
= [(message
, word
, fitness
)
59 # Gotcha: the helper function here needs to be defined at the top level
60 # (limitation of Pool.starmap)
61 breaks
= pool
.starmap(vigenere_keyword_break_worker
, helper_args
,
63 return max(breaks
, key
=lambda k
: k
[1])
64 vigenere_keyword_break
= vigenere_keyword_break_mp
66 def vigenere_keyword_break_worker(message
, keyword
, fitness
):
67 plaintext
= vigenere_decipher(message
, keyword
)
68 fit
= fitness(plaintext
)
69 logger
.debug('Vigenere keyword break attempt using key {0} gives fit of '
70 '{1} and decrypt starting: {2}'.format(keyword
,
71 fit
, sanitise(plaintext
)[:50]))
75 def vigenere_frequency_break(message
, max_key_length
=20, fitness
=Pletters
):
76 """Breaks a Vigenere cipher with frequency analysis
78 >>> vigenere_frequency_break(vigenere_encipher(sanitise("It is time to " \
79 "run. She is ready and so am I. I stole Daniel's pocketbook this " \
80 "afternoon when he left his jacket hanging on the easel in the " \
81 "attic. I jump every time I hear a footstep on the stairs, " \
82 "certain that the theft has been discovered and that I will " \
83 "be caught. The SS officer visits less often now that he is " \
84 "sure"), 'florence')) # doctest: +ELLIPSIS
85 ('florence', -307.5473096...)
87 def worker(message
, key_length
, fitness
):
88 splits
= every_nth(sanitised_message
, key_length
)
89 key
= cat([unpos(caesar_break(s
)[0]) for s
in splits
])
90 plaintext
= vigenere_decipher(message
, key
)
91 fit
= fitness(plaintext
)
93 sanitised_message
= sanitise(message
)
94 results
= starmap(worker
, [(sanitised_message
, i
, fitness
)
95 for i
in range(1, max_key_length
+1)])
96 return max(results
, key
=lambda k
: k
[1])
99 def beaufort_sub_break(message
, fitness
=Pletters
):
100 """Breaks one chunk of a Beaufort cipher with frequency analysis
102 >>> beaufort_sub_break('samwpplggnnmmyaazgympjapopnwiywwomwspgpjmefwmawx' \
103 'jafjhxwwwdigxshnlywiamhyshtasxptwueahhytjwsn') # doctest: +ELLIPSIS
105 >>> beaufort_sub_break('eyprzjjzznxymrygryjqmqhznjrjjapenejznawngnnezgza' \
106 'dgndknaogpdjneadadazlhkhxkryevrronrmdjnndjlo') # doctest: +ELLIPSIS
110 best_fit
= float('-inf')
111 for key
in range(26):
112 plaintext
= [unpos(key
- pos(l
)) for l
in message
]
113 fit
= fitness(plaintext
)
114 logger
.debug('Beaufort sub break attempt using key {0} gives fit of {1} '
115 'and decrypt starting: {2}'.format(key
, fit
,
120 logger
.info('Beaufort sub break best fit: key {0} gives fit of {1} and '
121 'decrypt starting: {2}'.format(best_key
, best_fit
,
122 cat([unpos(best_key
- pos(l
)) for l
in message
[:50]])))
123 return best_key
, best_fit
126 def beaufort_frequency_break(message
, max_key_length
=20, fitness
=Pletters
):
127 """Breaks a Beaufort cipher with frequency analysis
129 >>> beaufort_frequency_break(beaufort_encipher(sanitise("It is time to " \
130 "run. She is ready and so am I. I stole Daniel's pocketbook this " \
131 "afternoon when he left his jacket hanging on the easel in the " \
132 "attic. I jump every time I hear a footstep on the stairs, " \
133 "certain that the theft has been discovered and that I will " \
134 "be caught. The SS officer visits less often now " \
135 "that he is sure"), 'florence')) # doctest: +ELLIPSIS
136 ('florence', -307.5473096791...)
138 def worker(message
, key_length
, fitness
):
139 splits
= every_nth(message
, key_length
)
140 key
= cat([unpos(beaufort_sub_break(s
)[0]) for s
in splits
])
141 plaintext
= beaufort_decipher(message
, key
)
142 fit
= fitness(plaintext
)
144 sanitised_message
= sanitise(message
)
145 results
= starmap(worker
, [(sanitised_message
, i
, fitness
)
146 for i
in range(1, max_key_length
+1)])
147 return max(results
, key
=lambda k
: k
[1])
150 def beaufort_variant_frequency_break(message
, max_key_length
=20, fitness
=Pletters
):
151 """Breaks a Beaufort cipher with frequency analysis
153 >>> beaufort_variant_frequency_break(beaufort_variant_encipher(sanitise("It is time to " \
154 "run. She is ready and so am I. I stole Daniel's pocketbook this " \
155 "afternoon when he left his jacket hanging on the easel in the " \
156 "attic. I jump every time I hear a footstep on the stairs, " \
157 "certain that the theft has been discovered and that I will " \
158 "be caught. The SS officer visits less often now " \
159 "that he is sure"), 'florence')) # doctest: +ELLIPSIS
160 ('florence', -307.5473096791...)
162 def worker(message
, key_length
, fitness
):
163 splits
= every_nth(sanitised_message
, key_length
)
164 key
= cat([unpos(-caesar_break(s
)[0]) for s
in splits
])
165 plaintext
= beaufort_variant_decipher(message
, key
)
166 fit
= fitness(plaintext
)
168 sanitised_message
= sanitise(message
)
169 results
= starmap(worker
, [(sanitised_message
, i
, fitness
)
170 for i
in range(1, max_key_length
+1)])
171 return max(results
, key
=lambda k
: k
[1])
173 if __name__
== "__main__":