4 <title>Breaking keyword ciphers
</title>
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8"/>
6 <style type=
"text/css">
15 h1 { font-size:
3em; }
16 h2 { font-size:
2em; }
17 h3 { font-size:
1.6em; }
19 text-decoration: none;
22 -moz-border-radius:
5px;
23 -web-border-radius:
5px;
31 text-shadow:
0 0 20px #
333;
37 text-shadow:
0 0 20px #
333;
51 <textarea id=
"source">
53 # Breaking keyword ciphers
55 a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z
56 --|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|--
57 k | e | y | w | o | r | d | a | b | c | f | g | h | i | j | l | m | n | p | q | s | t | u | v | x | z
63 .indexlink[[Index](index.html)]
67 # Duplicate and extend your `affine_break()` function
69 * How to cycle through all the keys? What _are_ all the keys?
77 * `
2013/
4a.ciphertext`
78 * `
2013/
4b.ciphertext`
80 This will take a while. Fire up a system monitor. What's wrong?
84 # Python, threads, and the GIL
86 Thread-safe shared-memory code is hard.
88 Python's Global Interpreter Lock prevents shooting yourself in the foot.
90 Where you want true parallelism, need different threads (Python processes).
92 * Thread-safe shared-memory code is hard.
94 The `multiprocessing` library makes this easier.
96 But before we get there, a couple of diversions...
102 Three cipher breaking tasks so far.
104 All working on the same principle:
107 find a way to enumerate all the possible keys
108 initialise 'best so far'
110 decipher message with this key
112 if it's better than the best so far:
116 Repetition of code is a bad smell.
122 * find the key with the best score
128 A common task is to apply a function to each item in a sequence, returning a sequence of the results.
134 >>> map(double, [
1,
2,
3])
138 * `map()` is a higher-order function: its first argument is the function that's applied.
140 How can we use this for keyword cipher breaking?
144 # Mapping keyword decipherings
146 Define a function that takes a possible key (keyword and cipher type) and returns the key and its fitness.
148 * (Also pass in the message and the fitness function)
150 Use `map()` and `max()` to find the best key
156 How many arguments does this take?
158 How do you write a function that takes this many arguments?
164 ## Positional, keyword
166 * Common or garden parameters, as you're used to.
167 * `def keyword_encipher(message, keyword, Keyword_wrap_alphabet.from_a):`
170 * `def mean(x, *xs):`
172 First number goes in `x`, remaining go in the tuple `xs`
176 * `def myfunc(arg1=
0, **kwargs):`
178 `kwargs` will be a Dict of the remaining keywords (not `arg1`)
182 # Back to `multiprocessing`
184 What does `Pool.starmap()` do?
189 from multiprocessing import Pool
191 def keyword_break_mp(message, wordlist=keywords, fitness=Pletters, chunksize=
500):
192 helper_args = [??? for word in wordlist] # One tuple for each possible key
194 breaks = pool.starmap(keyword_break_worker, helper_args, chunksize)
195 return max(breaks, key=lambda k: k[
1])
197 def keyword_break_worker(???):
199 return (key, fitness)
202 * Gotcha: the function in `Pool.starmap()` must be defined at the top level
203 * This is definitely a
"feature"
207 # Performance and chunksize
209 Try the multiprocessing keyword break. Is it using all the resources?
211 Setting `chunksize` is an art.
213 ## Map-reduce as a general pattern for multiprocessing
216 <script src=
"http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type=
"text/javascript">
219 <script type=
"text/javascript"
220 src=
"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
222 <script type=
"text/javascript">
223 var slideshow = remark.create({ ratio:
"16:9" });
228 skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
231 MathJax.Hub.Queue(function() {
232 $(MathJax.Hub.getAllJax()).map(function(index, elem) {
233 return(elem.SourceElement());
234 }).parent().addClass('has-jax');
236 MathJax.Hub.Configured();