2021 challenge 1
[cipher-challenge.git] / 2020 / 2020-challenge7.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 220,
6 "metadata": {
7 "Collapsed": "false"
8 },
9 "outputs": [],
10 "source": [
11 "from szyfrow.keyword_cipher import *\n",
12 "from szyfrow.column_transposition import *\n",
13 "from szyfrow.vigenere import *\n",
14 "from szyfrow.support.text_prettify import *\n",
15 "from szyfrow.polybius import *\n",
16 "from szyfrow.caesar import *\n",
17 "import collections\n",
18 "from itertools import *"
19 ]
20 },
21 {
22 "cell_type": "code",
23 "execution_count": 150,
24 "metadata": {
25 "Collapsed": "false"
26 },
27 "outputs": [],
28 "source": [
29 "challenge_number = 7\n",
30 "plaintext_a_filename = f'plaintext.{challenge_number}a.txt'\n",
31 "plaintext_b_filename = f'plaintext.{challenge_number}b.txt'\n",
32 "ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'\n",
33 "ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'"
34 ]
35 },
36 {
37 "cell_type": "code",
38 "execution_count": 151,
39 "metadata": {
40 "Collapsed": "false"
41 },
42 "outputs": [],
43 "source": [
44 "ca = open(ciphertext_a_filename).read()\n",
45 "sca = sanitise(ca)\n",
46 "rsca = cat(reversed(sca))\n",
47 "cb = open(ciphertext_b_filename).read()\n",
48 "scb = cat(c for c in cb if c in (string.ascii_letters + '#+'))"
49 ]
50 },
51 {
52 "cell_type": "code",
53 "execution_count": 6,
54 "metadata": {
55 "Collapsed": "false"
56 },
57 "outputs": [
58 {
59 "name": "stdout",
60 "output_type": "stream",
61 "text": [
62 "triangle \n",
63 "\n",
64 "theattacheddocumentwasinterceptedbyharrysteamwhohavemanagedtoplantabugonthecommunicationlinesoutoftirpitzuferourcryptanalystshaveexaminedtheciphertextandthecasefilesandbelievethatitmayberelatedtothewheatstoneclockcipherdeputyheadofthedivisionhaswrittenabossguidetotheseciphermachinesandwehaveaddedthattothecasefilesharrysteamalsorecoveredascrapofburntpaperfromtheabwehrhqwastewhichcarriesthefollowingmessagewhichwethinkmightbepartoftheplaintexttentialstopthetripwireoptionisalsodeprecatedsinceitismorelikelythattheexplthenavyreportedthatadutchfishingvesselwasimpoundedbythenaziauthoritiesinbremerhaventhecrewweredetainedandnegotiationsareunderwaybetweenthedutchandgermanauthoritiestotrytogetthefishermenandtheirvesselbackbutinthemeantimernpatrolswillwatchoutfortheboatincaseitisinvolvedintheoperationtheairministryhavebeenbriefedonourfindingssofarandhaveemphasisedtheimportanceofprotectingtherdfsystemwhichiamtoldtheycallchainhomevkissympatheticbutthethreeofusarestillworriedaboutthenewagentsembeddedinlondonandwearenotsurehowmuchofathreattheyposeitiscrucialthatwebreaktheattachedciphertextasfastaspossiblesowecanputeffectivecountermeasuresinplaceitistemptingtoarrestalltwentythreespyclistsandaskthemdirectlybutwithoutrevealingtheextentofoursurveillanceandcodebreakingabilityitwillnotbepossibletoprovethattheyhavebrokenthelawsowewouldnotbeabletodetainordeportthemitwouldusuallybebettertowatchandwaitbuttheairministryfeelsthattherisktoourairdefenceistoogreatsoweneedtoactfastifweknewexactlywhenandwheretheyintendedtoattackchainhomethenwemightbeabletoincreasesecuritytopreventitbutagainweriskrevealingourcounterintelligencecapabilitiesharrysuggestedthatwecouldobscureourtracksbyfakingavisitbydignitariestothetargetsitesasacoverstoryforincreasedsecuritybutwecantmaintaintheincreaseforlongwithoutraisingsuspicionsoitiscrucialtodeterminetheirplanswhatevertheabwehrareintendingtheywontwanttheiragentstofallintoourhandsespeciallyafterademolitionsincethatwouldgiveustheexcuseweneedtoarrestthemitwouldbegoodtoknowwhattheyhaveinmindfortheirexfiltrationthingsareveryfinelybalancedifweacttoosoonwegiveawayourcrucialadvantageintheinterceptwarifweacttoolateourenemiescouldseriouslydamageourairdefencecapabilityitisclearthatthingsarecomingtoaheadanditiscrucialthatwecracktheclockcipherassoonaspossiblethefateofthecountryliesinyourhandspearl\n"
65 ]
66 }
67 ],
68 "source": [
69 "key_a, score_a = vigenere_frequency_break(sca, fitness=Ptrigrams)\n",
70 "print(key_a, '\\n')\n",
71 "pa = vigenere_decipher(sca, key_a)\n",
72 "print(pa)"
73 ]
74 },
75 {
76 "cell_type": "code",
77 "execution_count": 7,
78 "metadata": {
79 "Collapsed": "false"
80 },
81 "outputs": [
82 {
83 "name": "stdout",
84 "output_type": "stream",
85 "text": [
86 "the attached document was intercepted by harry steam who have managed to plant a bug on the\n",
87 "communication lines out of tirpitz ufer our crypt analysts have examined the ciphertext and the case\n",
88 "files and believe that it maybe related to the wheatstone clock cipher deputy head of the division\n",
89 "has written a boss guide to these cipher machines and we have added that to the case files harry\n",
90 "steam also recovered a scrap of burnt paper from the abwehr hq waste which carries the following\n",
91 "message which we think might be part of the plaintext tential stop the tripwire option is also\n",
92 "deprecated since it is more likely that the expl the navy reported that a dutch fishing vessel was\n",
93 "impounded by the nazi authorities in bremerhaven the crew were detained and negotiations are\n",
94 "underway between the dutch and german authorities to try to get the fishermen and their vessel back\n",
95 "but in the meantime rn patrols will watch out for the boat in case it is involved in the operation\n",
96 "the air ministry have been briefed on our findings so far and have emphasised the importance of\n",
97 "protecting the rdf system which i am told they call chain home vk is sympathetic but the three of us\n",
98 "are still worried about the new agents embedded in london and we are not sure how much of a threat\n",
99 "they pose it is crucial that we break the attached ciphertext as fast as possible so we can put\n",
100 "effective countermeasures in place it is tempting to arrest all twenty three spy c lists and ask\n",
101 "them directly but without revealing the extent of our surveillance and code breaking ability it will\n",
102 "not be possible to prove that they have broken the law so we would not be able to detain or deport\n",
103 "them it would usually be better to watch and wait but the air ministry feels that the risk to our\n",
104 "air defence is too great so we need to act fast if we knew exactly when and where they intended to\n",
105 "attack chain home then we might be able to increase security to prevent it but again we risk\n",
106 "revealing our counterintelligence capabilities harry suggested that we could obscure our tracks by\n",
107 "faking a visit by dignitaries to the target sites as a coverstory for increased security but we cant\n",
108 "maintain the increase for long without raising suspicion so it is crucial to determine their plans\n",
109 "whatever the abwehr are intending they wont want their agents to fall into our hands especially\n",
110 "after a demolition since that would give us the excuse we need to arrest them it would be good to\n",
111 "know what they have in mind for their exfiltration things are very finely balanced if we act too\n",
112 "soon we giveaway our crucial advantage in the intercept war if we act too late our enemies could\n",
113 "seriously damage our air defence capability it is clear that things are coming to ahead and it is\n",
114 "crucial that we crack the clock cipher as soon as possible the fate of the country lies in your\n",
115 "hands pearl\n"
116 ]
117 }
118 ],
119 "source": [
120 "pa = prettify(vigenere_decipher(sca, key_a))\n",
121 "print(pa)"
122 ]
123 },
124 {
125 "cell_type": "code",
126 "execution_count": 236,
127 "metadata": {
128 "Collapsed": "false"
129 },
130 "outputs": [
131 {
132 "data": {
133 "text/plain": [
134 "2843"
135 ]
136 },
137 "execution_count": 236,
138 "metadata": {},
139 "output_type": "execute_result"
140 }
141 ],
142 "source": [
143 "open(plaintext_a_filename, 'w').write(pa)"
144 ]
145 },
146 {
147 "cell_type": "code",
148 "execution_count": 152,
149 "metadata": {
150 "Collapsed": "false"
151 },
152 "outputs": [
153 {
154 "data": {
155 "text/plain": [
156 "'tentialstopthetripwireoptionisalsodeprecatedsinceitismorelikelythattheexpl'"
157 ]
158 },
159 "execution_count": 152,
160 "metadata": {},
161 "output_type": "execute_result"
162 }
163 ],
164 "source": [
165 "crib = sanitise(\"tential stop the tripwire option is also deprecated since it is more likely that the expl\")\n",
166 "crib"
167 ]
168 },
169 {
170 "cell_type": "code",
171 "execution_count": 153,
172 "metadata": {
173 "Collapsed": "false"
174 },
175 "outputs": [
176 {
177 "data": {
178 "text/plain": [
179 "'#+ABCDEFGHIJKLMNOPQRSTUVWXYZ'"
180 ]
181 },
182 "execution_count": 153,
183 "metadata": {},
184 "output_type": "execute_result"
185 }
186 ],
187 "source": [
188 "cat(sorted(set(scb)))"
189 ]
190 },
191 {
192 "cell_type": "code",
193 "execution_count": 192,
194 "metadata": {
195 "Collapsed": "false"
196 },
197 "outputs": [],
198 "source": [
199 "p_alpha = string.ascii_lowercase\n",
200 "c_alpha = string.ascii_uppercase + '#+'"
201 ]
202 },
203 {
204 "cell_type": "code",
205 "execution_count": 193,
206 "metadata": {
207 "Collapsed": "false"
208 },
209 "outputs": [],
210 "source": [
211 "cipherstream = zip(cycle(p_alpha), cycle(c_alpha))"
212 ]
213 },
214 {
215 "cell_type": "code",
216 "execution_count": 197,
217 "metadata": {
218 "Collapsed": "false"
219 },
220 "outputs": [],
221 "source": [
222 "def decipher_letter(cipher_letter, stream):\n",
223 " stream = dropwhile(lambda p: p[1] != cipher_letter, stream)\n",
224 " stream, temp_stream = tee(stream, 2)\n",
225 " (plain_letter, c) = list(islice(temp_stream, 1))[0]\n",
226 " return (plain_letter, islice(stream, 1, None))"
227 ]
228 },
229 {
230 "cell_type": "code",
231 "execution_count": 198,
232 "metadata": {
233 "Collapsed": "false"
234 },
235 "outputs": [],
236 "source": [
237 "def encipher_letter(plain_letter, stream):\n",
238 " stream = dropwhile(lambda p: p[0] != plain_letter, stream)\n",
239 " stream, temp_stream = tee(stream, 2)\n",
240 " (p, cipher_letter) = list(islice(temp_stream, 1))[0]\n",
241 " return (cipher_letter, islice(stream, 1, None))"
242 ]
243 },
244 {
245 "cell_type": "code",
246 "execution_count": 199,
247 "metadata": {
248 "Collapsed": "false"
249 },
250 "outputs": [
251 {
252 "data": {
253 "text/plain": [
254 "('b', 'd', 'z', 'b', 'd', 'f', 'h')"
255 ]
256 },
257 "execution_count": 199,
258 "metadata": {},
259 "output_type": "execute_result"
260 }
261 ],
262 "source": [
263 "cipherstream = zip(cycle(p_alpha), cycle(c_alpha))\n",
264 "p1, s1 = decipher_letter('B', cipherstream)\n",
265 "p2, s2 = decipher_letter('D', s1)\n",
266 "p3, s3 = decipher_letter('Z', s2)\n",
267 "p4, s4 = decipher_letter('+', s3)\n",
268 "p5, s5 = decipher_letter('B', s4)\n",
269 "p6, s6 = decipher_letter('D', s5)\n",
270 "p7, s7 = decipher_letter('D', s6)\n",
271 "\n",
272 "p1, p2, p3, p4, p5, p6, p7"
273 ]
274 },
275 {
276 "cell_type": "code",
277 "execution_count": 200,
278 "metadata": {
279 "Collapsed": "false"
280 },
281 "outputs": [
282 {
283 "data": {
284 "text/plain": [
285 "('b', 'd')"
286 ]
287 },
288 "execution_count": 200,
289 "metadata": {},
290 "output_type": "execute_result"
291 }
292 ],
293 "source": [
294 "cipherstream = zip(cycle(p_alpha), cycle(c_alpha))\n",
295 "p1, s1 = decipher_letter('B', cipherstream)\n",
296 "p2, s2 = decipher_letter('B', s1)\n",
297 "\n",
298 "p1, p2"
299 ]
300 },
301 {
302 "cell_type": "code",
303 "execution_count": 201,
304 "metadata": {
305 "Collapsed": "false"
306 },
307 "outputs": [],
308 "source": [
309 "def decipher_message(ciphertext, stream):\n",
310 " plaintext = ''\n",
311 " for l in ciphertext:\n",
312 " p, stream = decipher_letter(l, stream)\n",
313 " plaintext += p\n",
314 " return plaintext, stream "
315 ]
316 },
317 {
318 "cell_type": "code",
319 "execution_count": 202,
320 "metadata": {
321 "Collapsed": "false"
322 },
323 "outputs": [],
324 "source": [
325 "def encipher_message(plaintext, stream):\n",
326 " ciphertext = ''\n",
327 " for l in plaintext:\n",
328 " c, stream = encipher_letter(l, stream)\n",
329 " ciphertext += c\n",
330 " return ciphertext, stream "
331 ]
332 },
333 {
334 "cell_type": "code",
335 "execution_count": 203,
336 "metadata": {
337 "Collapsed": "false"
338 },
339 "outputs": [
340 {
341 "data": {
342 "text/plain": [
343 "'vxnozddfahharfgoxiyg'"
344 ]
345 },
346 "execution_count": 203,
347 "metadata": {},
348 "output_type": "execute_result"
349 }
350 ],
351 "source": [
352 "cipherstream = zip(cycle(p_alpha), cycle(c_alpha))\n",
353 "pb, s = decipher_message(scb[:20], cipherstream)\n",
354 "pb"
355 ]
356 },
357 {
358 "cell_type": "code",
359 "execution_count": 204,
360 "metadata": {
361 "Collapsed": "false"
362 },
363 "outputs": [
364 {
365 "data": {
366 "text/plain": [
367 "'TCLREWFMNGHLZUHDU+GS+MWX+OURKUALSM+ALN#WSJUREUZMOSBQ#SUXIPKMELYRDWNLZUSJ+V'"
368 ]
369 },
370 "execution_count": 204,
371 "metadata": {},
372 "output_type": "execute_result"
373 }
374 ],
375 "source": [
376 "cipherstream = zip(cycle(p_alpha), cycle(c_alpha))\n",
377 "c_gen, s = encipher_message(crib, cipherstream)\n",
378 "c_gen"
379 ]
380 },
381 {
382 "cell_type": "code",
383 "execution_count": 205,
384 "metadata": {
385 "Collapsed": "false",
386 "scrolled": true
387 },
388 "outputs": [
389 {
390 "data": {
391 "text/plain": [
392 "[(19, 'T'),\n",
393 " (2, 'C'),\n",
394 " (11, 'L'),\n",
395 " (17, 'R'),\n",
396 " (4, 'E'),\n",
397 " (22, 'W'),\n",
398 " (5, 'F'),\n",
399 " (12, 'M'),\n",
400 " (13, 'N'),\n",
401 " (6, 'G'),\n",
402 " (7, 'H'),\n",
403 " (11, 'L'),\n",
404 " (25, 'Z'),\n",
405 " (20, 'U'),\n",
406 " (7, 'H'),\n",
407 " (3, 'D'),\n",
408 " (20, 'U'),\n",
409 " (27, '+'),\n",
410 " (6, 'G'),\n",
411 " (18, 'S'),\n",
412 " (27, '+'),\n",
413 " (12, 'M'),\n",
414 " (22, 'W'),\n",
415 " (23, 'X'),\n",
416 " (27, '+'),\n",
417 " (14, 'O'),\n",
418 " (20, 'U'),\n",
419 " (17, 'R'),\n",
420 " (10, 'K'),\n",
421 " (20, 'U'),\n",
422 " (0, 'A'),\n",
423 " (11, 'L'),\n",
424 " (18, 'S'),\n",
425 " (12, 'M'),\n",
426 " (27, '+'),\n",
427 " (0, 'A'),\n",
428 " (11, 'L'),\n",
429 " (13, 'N'),\n",
430 " (26, '#'),\n",
431 " (22, 'W'),\n",
432 " (18, 'S'),\n",
433 " (9, 'J'),\n",
434 " (20, 'U'),\n",
435 " (17, 'R'),\n",
436 " (4, 'E'),\n",
437 " (20, 'U'),\n",
438 " (25, 'Z'),\n",
439 " (12, 'M'),\n",
440 " (14, 'O'),\n",
441 " (18, 'S'),\n",
442 " (1, 'B'),\n",
443 " (16, 'Q'),\n",
444 " (26, '#'),\n",
445 " (18, 'S'),\n",
446 " (20, 'U'),\n",
447 " (23, 'X'),\n",
448 " (8, 'I'),\n",
449 " (15, 'P'),\n",
450 " (10, 'K'),\n",
451 " (12, 'M'),\n",
452 " (4, 'E'),\n",
453 " (11, 'L'),\n",
454 " (24, 'Y'),\n",
455 " (17, 'R'),\n",
456 " (3, 'D'),\n",
457 " (22, 'W'),\n",
458 " (13, 'N'),\n",
459 " (11, 'L'),\n",
460 " (25, 'Z'),\n",
461 " (20, 'U'),\n",
462 " (18, 'S'),\n",
463 " (9, 'J'),\n",
464 " (27, '+'),\n",
465 " (21, 'V')]"
466 ]
467 },
468 "execution_count": 205,
469 "metadata": {},
470 "output_type": "execute_result"
471 }
472 ],
473 "source": [
474 "co = [(c_alpha.find(l), l) for i, l in enumerate(c_gen)]\n",
475 "co"
476 ]
477 },
478 {
479 "cell_type": "code",
480 "execution_count": 206,
481 "metadata": {
482 "Collapsed": "false"
483 },
484 "outputs": [
485 {
486 "data": {
487 "text/plain": [
488 "'VXLKVZXZSZXODRQYFQEM'"
489 ]
490 },
491 "execution_count": 206,
492 "metadata": {},
493 "output_type": "execute_result"
494 }
495 ],
496 "source": [
497 "scb[:20]"
498 ]
499 },
500 {
501 "cell_type": "code",
502 "execution_count": 207,
503 "metadata": {
504 "Collapsed": "false"
505 },
506 "outputs": [],
507 "source": [
508 "def offsets(ciphertext):\n",
509 " mappings = {}\n",
510 " letters_seen = 0\n",
511 " offsets = []\n",
512 " for l in ciphertext:\n",
513 " if l not in mappings:\n",
514 " mappings[l] = letters_seen\n",
515 " letters_seen += 1\n",
516 " offsets.append(mappings[l])\n",
517 " return offsets"
518 ]
519 },
520 {
521 "cell_type": "code",
522 "execution_count": 208,
523 "metadata": {
524 "Collapsed": "false"
525 },
526 "outputs": [
527 {
528 "data": {
529 "text/plain": [
530 "74"
531 ]
532 },
533 "execution_count": 208,
534 "metadata": {},
535 "output_type": "execute_result"
536 }
537 ],
538 "source": [
539 "crib_offsets = offsets(c_gen)\n",
540 "len(crib_offsets)"
541 ]
542 },
543 {
544 "cell_type": "code",
545 "execution_count": 209,
546 "metadata": {
547 "Collapsed": "false"
548 },
549 "outputs": [],
550 "source": [
551 "def plausible_samples(generated_text):\n",
552 " plausible_samples = {}\n",
553 " for i in range(len(scb)):\n",
554 " sample = scb[i:(i + len(crib))]\n",
555 " if len(sample) == len(crib):\n",
556 " ofs = offsets(sample)\n",
557 " if ofs == crib_offsets:\n",
558 " plausible_samples[i] = sample\n",
559 " return plausible_samples"
560 ]
561 },
562 {
563 "cell_type": "code",
564 "execution_count": 210,
565 "metadata": {
566 "Collapsed": "false"
567 },
568 "outputs": [
569 {
570 "data": {
571 "text/plain": [
572 "'TCLREWFMNGHLZUHDU+GS+MWX+OURKUALSM+ALN#WSJUREUZMOSBQ#SUXIPKMELYRDWNLZUSJ+V'"
573 ]
574 },
575 "execution_count": 210,
576 "metadata": {},
577 "output_type": "execute_result"
578 }
579 ],
580 "source": [
581 "c_gen"
582 ]
583 },
584 {
585 "cell_type": "code",
586 "execution_count": 211,
587 "metadata": {
588 "Collapsed": "false"
589 },
590 "outputs": [],
591 "source": [
592 "def plausible_mapping(crib_output):\n",
593 " mapping = {}\n",
594 " for n, l in crib_output:\n",
595 " if n in mapping:\n",
596 " if mapping[n] != l:\n",
597 " return {}\n",
598 " else:\n",
599 " mapping[n] = l\n",
600 " return mapping"
601 ]
602 },
603 {
604 "cell_type": "code",
605 "execution_count": null,
606 "metadata": {
607 "Collapsed": "false"
608 },
609 "outputs": [],
610 "source": []
611 },
612 {
613 "cell_type": "code",
614 "execution_count": 212,
615 "metadata": {
616 "Collapsed": "false"
617 },
618 "outputs": [
619 {
620 "data": {
621 "text/plain": [
622 "{19: 'T', 2: 'C', 11: 'L'}"
623 ]
624 },
625 "execution_count": 212,
626 "metadata": {},
627 "output_type": "execute_result"
628 }
629 ],
630 "source": [
631 "plausible_mapping(co[:3])"
632 ]
633 },
634 {
635 "cell_type": "code",
636 "execution_count": 213,
637 "metadata": {
638 "Collapsed": "false"
639 },
640 "outputs": [
641 {
642 "data": {
643 "text/plain": [
644 "{0: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
645 " 1: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
646 " 2: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
647 " 3: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
648 " 4: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
649 " 5: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
650 " 6: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
651 " 7: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
652 " 8: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
653 " 9: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
654 " 10: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
655 " 11: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
656 " 12: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
657 " 13: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
658 " 14: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
659 " 15: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
660 " 16: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
661 " 17: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
662 " 18: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
663 " 19: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
664 " 20: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
665 " 21: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
666 " 22: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
667 " 23: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
668 " 24: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'},\n",
669 " 25: {1026: 'KW+ASVCNDJM+BPM#PLJHLNVZLUPAXPR+HNLR+DIVHGPASPBNUHFOIHPZTYXNS+QA#VD+BPHGLE'}}"
670 ]
671 },
672 "execution_count": 213,
673 "metadata": {},
674 "output_type": "execute_result"
675 }
676 ],
677 "source": [
678 "solutions = {}\n",
679 "for i in range(26):\n",
680 " cipherstream = zip(islice(cycle(p_alpha), i, None),\n",
681 " cycle(c_alpha))\n",
682 " c_gen, s = encipher_message(crib, cipherstream)\n",
683 " solutions[i] = plausible_samples(c_gen)\n",
684 "solutions"
685 ]
686 },
687 {
688 "cell_type": "code",
689 "execution_count": 225,
690 "metadata": {
691 "Collapsed": "false"
692 },
693 "outputs": [
694 {
695 "data": {
696 "text/plain": [
697 "12"
698 ]
699 },
700 "execution_count": 225,
701 "metadata": {},
702 "output_type": "execute_result"
703 }
704 ],
705 "source": [
706 "1026 % 26"
707 ]
708 },
709 {
710 "cell_type": "code",
711 "execution_count": 214,
712 "metadata": {
713 "Collapsed": "false"
714 },
715 "outputs": [
716 {
717 "data": {
718 "text/plain": [
719 "'TCLREWFMNGHLZUHDU+GS+MWX+OURKUALSM+ALN#WSJUREUZMOSBQ#SUXIPKMELYRDWNLZUSJ+V'"
720 ]
721 },
722 "execution_count": 214,
723 "metadata": {},
724 "output_type": "execute_result"
725 }
726 ],
727 "source": [
728 "cipherstream = zip(cycle(p_alpha), cycle(c_alpha))\n",
729 "c_gen0, s = encipher_message(crib, cipherstream)\n",
730 "c_gen0"
731 ]
732 },
733 {
734 "cell_type": "code",
735 "execution_count": 215,
736 "metadata": {
737 "Collapsed": "false"
738 },
739 "outputs": [
740 {
741 "data": {
742 "text/plain": [
743 "'ABCDEFGHIJKLMNOPQRSTUVWXYZ#+'"
744 ]
745 },
746 "execution_count": 215,
747 "metadata": {},
748 "output_type": "execute_result"
749 }
750 ],
751 "source": [
752 "c_alpha"
753 ]
754 },
755 {
756 "cell_type": "code",
757 "execution_count": 216,
758 "metadata": {
759 "Collapsed": "false"
760 },
761 "outputs": [
762 {
763 "data": {
764 "text/plain": [
765 "{19: 'K',\n",
766 " 2: 'W',\n",
767 " 11: '+',\n",
768 " 17: 'A',\n",
769 " 4: 'S',\n",
770 " 22: 'V',\n",
771 " 5: 'C',\n",
772 " 12: 'N',\n",
773 " 13: 'D',\n",
774 " 6: 'J',\n",
775 " 7: 'M',\n",
776 " 25: 'B',\n",
777 " 20: 'P',\n",
778 " 3: '#',\n",
779 " 27: 'L',\n",
780 " 18: 'H',\n",
781 " 23: 'Z',\n",
782 " 14: 'U',\n",
783 " 10: 'X',\n",
784 " 0: 'R',\n",
785 " 26: 'I',\n",
786 " 9: 'G',\n",
787 " 1: 'F',\n",
788 " 16: 'O',\n",
789 " 8: 'T',\n",
790 " 15: 'Y',\n",
791 " 24: 'Q',\n",
792 " 21: 'E'}"
793 ]
794 },
795 "execution_count": 216,
796 "metadata": {},
797 "output_type": "execute_result"
798 }
799 ],
800 "source": [
801 "soln = solutions[0][1026]\n",
802 "alpha_pos = [c_alpha.find(c) for c in c_gen0]\n",
803 "c_alpha_mapped = {}\n",
804 "\n",
805 "for p, c in zip(alpha_pos, soln):\n",
806 " c_alpha_mapped[p] = c\n",
807 "c_alpha_mapped"
808 ]
809 },
810 {
811 "cell_type": "code",
812 "execution_count": 217,
813 "metadata": {
814 "Collapsed": "false"
815 },
816 "outputs": [
817 {
818 "data": {
819 "text/plain": [
820 "'RFW#SCJMTGX+NDUYOAHKPEVZQBIL'"
821 ]
822 },
823 "execution_count": 217,
824 "metadata": {},
825 "output_type": "execute_result"
826 }
827 ],
828 "source": [
829 "discovered_c_alpha = cat(c_alpha_mapped[i] if i in c_alpha_mapped else ' ' for i in range(28))\n",
830 "discovered_c_alpha"
831 ]
832 },
833 {
834 "cell_type": "code",
835 "execution_count": 218,
836 "metadata": {
837 "Collapsed": "false"
838 },
839 "outputs": [
840 {
841 "data": {
842 "text/plain": [
843 "28"
844 ]
845 },
846 "execution_count": 218,
847 "metadata": {},
848 "output_type": "execute_result"
849 }
850 ],
851 "source": [
852 "len(c_alpha_mapped)"
853 ]
854 },
855 {
856 "cell_type": "code",
857 "execution_count": 226,
858 "metadata": {
859 "Collapsed": "false"
860 },
861 "outputs": [
862 {
863 "data": {
864 "text/plain": [
865 "'NDUYOAHKPEVZQBILRFW#SCJMTGX+'"
866 ]
867 },
868 "execution_count": 226,
869 "metadata": {},
870 "output_type": "execute_result"
871 }
872 ],
873 "source": [
874 "rotated_c_alpha = discovered_c_alpha[12:] + discovered_c_alpha[:12]\n",
875 "rotated_c_alpha"
876 ]
877 },
878 {
879 "cell_type": "code",
880 "execution_count": 219,
881 "metadata": {
882 "Collapsed": "false"
883 },
884 "outputs": [
885 {
886 "data": {
887 "text/plain": [
888 "'wmdxabqdmfuazomftqpdmxfaiqdtmenqqzmbbdahqpmzpagdmsqzfemdqmddmzsuzsfabdahupqkagiuftftqfzfkagdqcgqefqpuzkagdxmefyqeemsqefabftueiuxxnqetubbqpazmpgfotruetuzshqeeqxituotiuxxxmzpmfftqymdetqearraxpxqustmffiamyazrdupmkpqoqynqdfqzftefabftqkiuxxeuszmxftqudmdduhmxnkrxmetuzsftqudzmhusmfuazxustfeuzyadeqoapqfaebqxxagfftqiadprxmfmzpkagiuxxdqbxkfkdqefaburkagnqxuqhqftmfkagmdqnquzsimfotqpftqzmnmzpazftqdqzpqlhagemzpfdkmsmuzmfuzfqdhmxearfiapmkeftdqqpmkeruhqpmkemzpeqhqzpmkeefabpazafmffqybffaymwqoazfmofiuftftqnamfadufeodqiuzmzkaftqdimkefabftqqjbxaeuhqueefmnxqngfetagxpnqfdqmfqpiuftomdqefabftqrgeqemdqftqyaefdqoqzfbdapgofaragddqeqmdotmzpfqotzaxaskpuhueuazmzpomddkfdubxqfdussqdeefabftqktmhqmfuyqdoudogufituotymknqeqfradmzkbqduaprdayftdqqyuzgfqefaeqhqzfqqztagdeefabftqkomzmxeanqfdussqdqpnkmfdubiudqmeeqynxkadftqrgeqyqotmzueyomznqdqbxmoqpiuftmtmzpsdqzmpqyqotmzueyefabeuzoqftqeqotmdsqetmhqnqqzetmbqpradpqyaxufuazgeqiqpazafdqoayyqzpgeuzsftqeqpqhuoqeuzoaynmfeuzoqkagtmhqzaoqdfmuzfkftmfftqotmdsqiuxxxmzpuzmzaduqzfmfuazftmfiuxxymjuyuequfepqefdgofuhqbafqzfumxefabftqfdubiudqabfuazuemxeapqbdqomfqpeuzoqufueyadqxuwqxkftmfftqqjbxaeuhqeiuxxnqpueoahqdqpurftqkmdqmffmotqpfaegotmfdussqdmzpyadqahqdufiagxpnqpurruogxffaoaadpuzmfqftqotmdsqegeuzsegotmpqhuoqefabftqotmdsqeftqyeqxhqemdqeymxxngfftqsqayqfdkarftqbmowmsqiuxxraogeftqnxmefeaufueuybqdmfuhqftmfkaguzefmxxftqeqotmdsqeuzmooadpmzoqiuftftqymzgrmofgdqdeuzefdgofuazeefabuzadpqdfaymjuyueqftqpmymsqfaftqfaiqdeftqqzsuzqqdedqoayyqzpftmfkageqfotmdsqeazftdqqarftqxqsearqmotfaiqdiuftotmdsqefiamzpftdqqeqffaqjbxapquzeqcgqzoqmrfqdotmdsqazqefabeqfmzmppufuazmxpqxmkarftudfkeqoazperadotmdsqfiamzpazqyuzgfqfiqzfkruhqeqoazperadotmdsqftdqqefabmeftqrudefxqsngowxqeufiuxxefdqeeftqrdmyqeaftmfftqetaowimhqarftqeqoazpotmdsqiuxxpuefadfftqegbbadfuzsyqynqdeefabiuftftqftudpqjbxaeuazftqrdmyqetagxpngowxqdqegxfuzsuzftqfafmxpqefdgofuazarftqfaiqdengfftueeqcgqzoqiuxxymwqeufqoxqmdmzoqmzpdqoazefdgofuaztmdpqdfamooaybxuetefabftqpqxmkiuftotmdsqftdqqmxeauzodqmeqeftqxuwqxutaapftmfeaxpuqdeiuxxtmhqmdduhqpazeufquzfuyqfanqomgstfuzftqftudpnxmefuzodqmeuzsoazrgeuazmzpxaeearyadmxqefaburufuebaeeunxqfaoxuynftqfaiqduzmphmzoqarftqpqyaxufuazfasqfyadqpqfmuxqpuymsqearftqmqdumxmzpmybxuruqdoudogufdkftqzftueetagxpnqmffqybfqptaiqhqdurftqdquemzksgmdpbdqeqzoqftqzzadueweetagxpnqfmwqzuzftuedqsmdpefabiqygefxqmhquffakagfapqfqdyuzqftqyaefqrrqofuhqfmdsqferadftqabqdmfuazngfftqpqyaxufuazygeftmbbqzeuygxfmzqagexkmfmxxeufqeradfiadqmeazeefabrudefuzadpqdfabdqhqzfagdqzqykrdaypuedgbfuzsftqabqdmfuazazoqftqkdqmxueqitmfuetmbbqzuzsefabeqoazpiqygefxqmhqzapagnfftmfftueimemzmofarimdefabftqdquemduewftmfftqndufuetsahqdzyqzfymkfdkfadqmotmzmooayyapmfuaziuftftqrgtdqdpuhupuzsftqiadxpnqfiqqzftqudpkuzsqybudqmzpagdevgefmeufuenadzefabftqazxkimkfaqzegdqftmfiqomzfdgxkegbbxmzfftqyuefapdmiftqyuzfamimdftqkomzzafiuzmzpftuebdavqofiuxxnqftqbdahaomfuazftmfqzegdqeftmfftueiuxxtmbbqzefabftqbxmzzuzspuhueuaztmepqfqdyuzqpftmfvmzgmdkrudefiuxxnqftqabfuymxzustfradftqmffmowmeufoauzoupqeiuftmzqiyaazefabftqkegssqefftmfkagebxufuzfafiafqmyefamffmowftqqpygzpefomuefadmzpomzqipazeufqeabqzuzsmoaddupadmxazsftqftmyqeradagdradftoayuzsmuddmupeefabftqegbbadffqmyeetagxpeqfgbxaawagfbaefemfftqfmdsqfeuzmphmzoqarftqmffmoweaftmfkagomzyazufadsgmdpyahqyqzfemzpymwqmruzmxmeeqeeyqzfarftqeqogdufkefabraxxaiuzsftqabqdmfuazftqemnafmsqfqmyeetagxpdqzpqlhagefasqftqdiuftftqegbbadffqmyemfftqtayqarftqxaomxadfesdgbbqotuqruzeagftymxpazefabmgnamfiuxxefmzpnkfabuowkagmxxgbmfftdqqradfkeqhqzvmzgmdkeqoazpiuftmrmxxnmowdqzpqlhagearazqftudfqqzazftqftudfqqzftitqzftqeqmdotetagxptmhqpuqppaizefabkagiuxxzqqpfasmuzmooqeefamnamfombmnxqaromddkuzskagmxxagffayqqfufefabftqdqmdqhqdkymzkruetuzshqeeqxeuzftuezqustnagdtaapmzpiqdqoayyqzpefqmxuzsazqmrfqdftqabqdmfuazefabufyustfrqqxxqeeeqogdqftmfanfmuzuzsazquzmphmzoqtaiqhqdiqygefpaqhqdkftuzsiqomzfabdqhqzfegebuouazuzmphmzoqarftqabqdmfuazefaburftueyueeuazegooqqpeftqzufiuxxnqazxkmymffqdarpmkenqradqftqimdnqsuzemzpkagdftdqqoaydmpqeuzxazpaziuxxbxmkmodgoumxdaxquzoaadpuzmfuzsagduzfqxxusqzoqqrradfenqtuzpqzqykxuzqeefabiqtmhqnqqzimfotuzsmzpxuefqzuzsomdqrgxxkradeuszeftmfftqabqdmfuazoagxptmhqnqqzoaybdayueqpmzpmemdqegxfarftaeqqzcguduqeiqtmhqpueoahqdqpmzqifabeqodqfmsqzokwzaizazxknkftqmodazkynaeeefabiqtmhqnqqzgzmnxqfauzruxfdmfqftuesdagbngfufuewzaizftmfftqxqmpqdueeayqazqiuftftquzufumxtmzpftmfftqkmdqfmwuzsftqxqmpazeuszmxeuzfqxxusqzoqefabftqtqmparftqoagzfqduzfqxxusqzoqndmzotgeqeftqoapqzmyqbqmdxmzpiqiuxxnqymwuzsqhqdkqrradffapueoahqdtqdupqzfufkefabituxqftqpqoxmdmfuazarimdmzpftqpqefdgofuazarftqdprekefqymdqftqoqzfdmxsamxearftueabqdmfuazftqxazpazfdumzsxqiuxxnqwqkfaqzegduzsmrxaiartustcgmxufkuzfqxxusqzoqfauzradyagdnmffxqbxmzeefabiuftagfftqudoayygzuomfuazeiqiuxxnqgzmnxqfayazufadndufuetbxmzebxmouzssqdymzxuhqemfgzzqoqeemdkduewefaburftqdquemzkotmzoqftmfyqynqdearkagdsdagboagxpnqombfgdqpmzpuzfqddasmfqpftqzkagetagxpmnmzpazftqemnafmsqabqdmfuazmzpdqfgdzfafudbuflgrqdefabbdafqofuzsftqupqzfufkarftqfdumzsxquearrmdsdqmfqdxazsfqdyuybadfmzoqfageefabiqomzruzpaftqdimkefabdahawqftqimdngfiqiagxpruzpufyadqpurruogxffaqzegdqftmfiqomziuzufiuftagfmzqfiadwarmsqzfeuzftqombufmxefabftqyueeuaziuxxnqdqsmdpqpmemegooqeeazxkuriqomzsgmdmzfqqftqudemrqfkefabfauzodqmeqftqeqogdufkarftqxazpazfdumzsxqufueodgoumxftmfftqoxaowoubtqdpqhuoqmzpftqmeeaoumfqpoapqnaawedqmotagdxazpazmsqzfeefabftqpqhuoqomzzafnqmxxaiqpfarmxxuzfaqzqyktmzpemzpftueygefnqftqfabbduadufkefaburkagmdqombfgdqpftqzftqoapqnaawoazfmuzuzsftqwqkefaftqoubtqdygefnqpqefdakqpefabufuebduzfqpazebqoumxbmbqdftmfetagxpqmeuxkpueeaxhqngfufomzmxeanquzsqefqpurzqoqeemdkefabitqzgeuzsftqymotuzqfmwqomdqfaqdmeqftqoubtqdmxbtmnqfmrfqdgeqmzpfapqxqfqftqwqkrdayftqoapqnaawfaqzegdqftmfzaazqgeqeftqemyqwqkfiuoqefabftueiuxxmxeadqpgoqftqduewarftqwqkqpoubtqdrmxxuzsuzfaqzqyktmzpeefaburombfgdqmbbqmdeuyyuzqzfftqzgeqazqarftqetmbqpotmdsqefapqefdakftqpqhuoqbdqrqdmnxkngdkuzsuffasqftqdiuftftqotmdsqfauzodqmeqftqpqefdgofuhqbaiqdmzpymwquftmdpqdfadqoazefdgofefabqhqdkazquzftqyahqyqzfiuxxnqbdmkuzsradkagdegooqeemzpkagdoagdmsqmzpoayyufyqzffaftqrmftqdxmzpiuxxnqbdmueqpradqhqdefabkagmdqmzuzebudmfuazfagemxxefabiuetuzskagxgowuzftqruzmxbtmeqarftqabqdmfuazefabkagdegzoxqiuxtqxy'"
889 ]
890 },
891 "execution_count": 219,
892 "metadata": {},
893 "output_type": "execute_result"
894 }
895 ],
896 "source": [
897 "cipherstream = zip(cycle(p_alpha), cycle(discovered_c_alpha))\n",
898 "c_gen, s = decipher_message(scb, cipherstream)\n",
899 "c_gen"
900 ]
901 },
902 {
903 "cell_type": "code",
904 "execution_count": 221,
905 "metadata": {
906 "Collapsed": "false"
907 },
908 "outputs": [
909 {
910 "data": {
911 "text/plain": [
912 "(12, -7510.337448308099)"
913 ]
914 },
915 "execution_count": 221,
916 "metadata": {},
917 "output_type": "execute_result"
918 }
919 ],
920 "source": [
921 "caesar_break(c_gen)"
922 ]
923 },
924 {
925 "cell_type": "code",
926 "execution_count": 222,
927 "metadata": {
928 "Collapsed": "false"
929 },
930 "outputs": [
931 {
932 "data": {
933 "text/plain": [
934 "'karloperationcathedraltowerhasbeenapprovedandouragentsarearrangingtoprovideyouwiththetntyourequestedinyourlastmessagestopthiswillbeshippedonadutchfishingvesselwhichwilllandatthemarshesoffoldleighattwoamonfridaydecembertenthstoptheywillsignaltheirarrivalbyflashingtheirnavigationlightsinmorsecodetospelloutthewordflatandyouwillreplytyrestopifyoubelievethatyouarebeingwatchedthenabandontherendezvousandtryagainatintervalsoftwodaysthreedaysfivedaysandsevendaysstopdonotattempttomakecontactwiththeboatoritscrewinanyotherwaystoptheexplosiveisstablebutshouldbetreatedwithcarestopthefusesarethemostrecentproductofourresearchandtechnologydivisionandcarrytripletriggersstoptheyhaveatimercircuitwhichmaybesetforanyperiodfromthreeminutestoseventeenhoursstoptheycanalsobetriggeredbyatripwireassemblyorthefusemechanismcanbereplacedwithahandgrenademechanismstopsincethesechargeshavebeenshapedfordemolitionusewedonotrecommendusingthesedevicesincombatsinceyouhavenocertaintythatthechargewilllandinanorientationthatwillmaximiseitsdestructivepotentialstopthetripwireoptionisalsodeprecatedsinceitismorelikelythattheexplosiveswillbediscoverediftheyareattachedtosuchatriggerandmoreoveritwouldbedifficulttocoordinatethechargesusingsuchadevicestopthechargesthemselvesaresmallbutthegeometryofthepackagewillfocustheblastsoitisimperativethatyouinstallthesechargesinaccordancewiththemanufacturersinstructionsstopinordertomaximisethedamagetothetowerstheengineersrecommendthatyousetchargesonthreeofthelegsofeachtowerwithchargestwoandthreesettoexplodeinsequenceafterchargeonestopsetanadditionaldelayofthirtysecondsforchargetwoandoneminutetwentyfivesecondsforchargethreestopasthefirstlegbucklesitwillstresstheframesothattheshockwaveofthesecondchargewilldistortthesupportingmembersstopwiththethirdexplosiontheframeshouldbuckleresultinginthetotaldestructionofthetowersbutthissequencewillmakesiteclearanceandreconstructionhardertoaccomplishstopthedelaywithchargethreealsoincreasesthelikelihoodthatsoldierswillhavearrivedonsiteintimetobecaughtinthethirdblastincreasingconfusionandlossofmoralestopifitispossibletoclimbthetowerinadvanceofthedemolitiontogetmoredetailedimagesoftheaerialandamplifiercircuitrythenthisshouldbeattemptedhoweverifthereisanyguardpresencethennorisksshouldbetakeninthisregardstopwemustleaveittoyoutodeterminethemosteffectivetargetsfortheoperationbutthedemolitionmusthappensimultaneouslyatallsitesfortworeasonsstopfirstinordertopreventourenemyfromdisruptingtheoperationoncetheyrealisewhatishappeningstopsecondwemustleavenodoubtthatthiswasanactofwarstopthereisariskthatthebritishgovernmentmaytrytoreachanaccommodationwiththefuhrerdividingtheworldbetweentheirdyingempireandoursjustasitisbornstoptheonlywaytoensurethatwecantrulysupplantthemistodrawthemintoawartheycannotwinandthisprojectwillbetheprovocationthatensuresthatthiswillhappenstoptheplanningdivisionhasdeterminedthatjanuaryfirstwillbetheoptimalnightfortheattackasitcoincideswithanewmoonstoptheysuggestthatyousplitintotwoteamstoattacktheedmundstcaistorandcanewdonsitesopeningacorridoralongthethamesforourforthcomingairraidsstopthesupportteamsshouldsetuplookoutpostsatthetargetsinadvanceoftheattacksothatyoucanmonitorguardmovementsandmakeafinalassessmentofthesecuritystopfollowingtheoperationthesabotageteamsshouldrendezvoustogetherwiththesupportteamsatthehomeofthelocalortsgruppechiefinsouthmaldonstopauboatwillstandbytopickyouallupatthreefortysevenjanuarysecondwithafallbackrendezvousofonethirteenonthethirteenthwhenthesearchshouldhavedieddownstopyouwillneedtogainaccesstoaboatcapableofcarryingyouallouttomeetitstopthereareverymanyfishingvesselsinthisneighbourhoodandwerecommendstealingoneaftertheoperationstopitmightfeellesssecurethatobtainingoneinadvancehoweverwemustdoeverythingwecantopreventsuspicioninadvanceoftheoperationstopifthismissionsucceedsthenitwillbeonlyamatterofdaysbeforethewarbeginsandyourthreecomradesinlondonwillplayacrucialroleincoordinatingourintelligenceeffortsbehindenemylinesstopwehavebeenwatchingandlisteningcarefullyforsignsthattheoperationcouldhavebeencompromisedandasaresultofthoseenquirieswehavediscoveredanewtopsecretagencyknownonlybytheacronymbossstopwehavebeenunabletoinfiltratethisgroupbutitisknownthattheleaderissomeonewiththeinitialhandthattheyaretakingtheleadonsignalsintelligencestoptheheadofthecounterintelligencebranchusesthecodenamepearlandwewillbemakingeveryefforttodiscoverheridentitystopwhilethedeclarationofwarandthedestructionoftherdfsystemarethecentralgoalsofthisoperationthelondontrianglewillbekeytoensuringaflowofhighqualityintelligencetoinformourbattleplansstopwithouttheircommunicationswewillbeunabletomonitorbritishplansplacinggermanlivesatunnecessaryriskstopifthereisanychancethatmembersofyourgroupcouldbecapturedandinterrogatedthenyoushouldabandonthesabotageoperationandreturntotirpitzuferstopprotectingtheidentityofthetriangleisoffargreaterlongtermimportancetousstopwecanfindotherwaystoprovokethewarbutwewouldfinditmoredifficulttoensurethatwecanwinitwithoutanetworkofagentsinthecapitalstopthemissionwillberegardedasasuccessonlyifwecanguaranteetheirsafetystoptoincreasethesecurityofthelondontriangleitiscrucialthattheclockcipherdeviceandtheassociatedcodebooksreachourlondonagentsstopthedevicecannotbeallowedtofallintoenemyhandsandthismustbethetopprioritystopifyouarecapturedthenthecodebookcontainingthekeystotheciphermustbedestroyedstopitisprintedonspecialpaperthatshouldeasilydissolvebutitcanalsobeingestedifnecessarystopwhenusingthemachinetakecaretoerasethecipheralphabetafteruseandtodeletethekeyfromthecodebooktoensurethatnooneusesthesamekeytwicestopthiswillalsoreducetheriskofthekeyedcipherfallingintoenemyhandsstopifcaptureappearsimminentthenuseoneoftheshapedchargestodestroythedevicepreferablyburyingittogetherwiththechargetoincreasethedestructivepowerandmakeithardertoreconstructstopeveryoneinthemovementwillbeprayingforyoursuccessandyourcourageandcommitmenttothefatherlandwillbepraisedforeverstopyouareaninspirationtousallstopwishingyouluckinthefinalphaseoftheoperationstopyoursunclewilhelm'"
935 ]
936 },
937 "execution_count": 222,
938 "metadata": {},
939 "output_type": "execute_result"
940 }
941 ],
942 "source": [
943 "caesar_decipher(c_gen, 12)"
944 ]
945 },
946 {
947 "cell_type": "code",
948 "execution_count": 229,
949 "metadata": {
950 "Collapsed": "false"
951 },
952 "outputs": [
953 {
954 "data": {
955 "text/plain": [
956 "'karloperationcathedraltowerhasbeenapprovedandouragentsarearrangingtoprovideyouwiththetntyourequestedinyourlastmessagestopthiswillbeshippedonadutchfishingvesselwhichwilllandatthemarshesoffoldleighattwoamonfridaydecembertenthstoptheywillsignaltheirarrivalbyflashingtheirnavigationlightsinmorsecodetospelloutthewordflatandyouwillreplytyrestopifyoubelievethatyouarebeingwatchedthenabandontherendezvousandtryagainatintervalsoftwodaysthreedaysfivedaysandsevendaysstopdonotattempttomakecontactwiththeboatoritscrewinanyotherwaystoptheexplosiveisstablebutshouldbetreatedwithcarestopthefusesarethemostrecentproductofourresearchandtechnologydivisionandcarrytripletriggersstoptheyhaveatimercircuitwhichmaybesetforanyperiodfromthreeminutestoseventeenhoursstoptheycanalsobetriggeredbyatripwireassemblyorthefusemechanismcanbereplacedwithahandgrenademechanismstopsincethesechargeshavebeenshapedfordemolitionusewedonotrecommendusingthesedevicesincombatsinceyouhavenocertaintythatthechargewilllandinanorientationthatwillmaximiseitsdestructivepotentialstopthetripwireoptionisalsodeprecatedsinceitismorelikelythattheexplosiveswillbediscoverediftheyareattachedtosuchatriggerandmoreoveritwouldbedifficulttocoordinatethechargesusingsuchadevicestopthechargesthemselvesaresmallbutthegeometryofthepackagewillfocustheblastsoitisimperativethatyouinstallthesechargesinaccordancewiththemanufacturersinstructionsstopinordertomaximisethedamagetothetowerstheengineersrecommendthatyousetchargesonthreeofthelegsofeachtowerwithchargestwoandthreesettoexplodeinsequenceafterchargeonestopsetanadditionaldelayofthirtysecondsforchargetwoandoneminutetwentyfivesecondsforchargethreestopasthefirstlegbucklesitwillstresstheframesothattheshockwaveofthesecondchargewilldistortthesupportingmembersstopwiththethirdexplosiontheframeshouldbuckleresultinginthetotaldestructionofthetowersbutthissequencewillmakesiteclearanceandreconstructionhardertoaccomplishstopthedelaywithchargethreealsoincreasesthelikelihoodthatsoldierswillhavearrivedonsiteintimetobecaughtinthethirdblastincreasingconfusionandlossofmoralestopifitispossibletoclimbthetowerinadvanceofthedemolitiontogetmoredetailedimagesoftheaerialandamplifiercircuitrythenthisshouldbeattemptedhoweverifthereisanyguardpresencethennorisksshouldbetakeninthisregardstopwemustleaveittoyoutodeterminethemosteffectivetargetsfortheoperationbutthedemolitionmusthappensimultaneouslyatallsitesfortworeasonsstopfirstinordertopreventourenemyfromdisruptingtheoperationoncetheyrealisewhatishappeningstopsecondwemustleavenodoubtthatthiswasanactofwarstopthereisariskthatthebritishgovernmentmaytrytoreachanaccommodationwiththefuhrerdividingtheworldbetweentheirdyingempireandoursjustasitisbornstoptheonlywaytoensurethatwecantrulysupplantthemistodrawthemintoawartheycannotwinandthisprojectwillbetheprovocationthatensuresthatthiswillhappenstoptheplanningdivisionhasdeterminedthatjanuaryfirstwillbetheoptimalnightfortheattackasitcoincideswithanewmoonstoptheysuggestthatyousplitintotwoteamstoattacktheedmundstcaistorandcanewdonsitesopeningacorridoralongthethamesforourforthcomingairraidsstopthesupportteamsshouldsetuplookoutpostsatthetargetsinadvanceoftheattacksothatyoucanmonitorguardmovementsandmakeafinalassessmentofthesecuritystopfollowingtheoperationthesabotageteamsshouldrendezvoustogetherwiththesupportteamsatthehomeofthelocalortsgruppechiefinsouthmaldonstopauboatwillstandbytopickyouallupatthreefortysevenjanuarysecondwithafallbackrendezvousofonethirteenonthethirteenthwhenthesearchshouldhavedieddownstopyouwillneedtogainaccesstoaboatcapableofcarryingyouallouttomeetitstopthereareverymanyfishingvesselsinthisneighbourhoodandwerecommendstealingoneaftertheoperationstopitmightfeellesssecurethatobtainingoneinadvancehoweverwemustdoeverythingwecantopreventsuspicioninadvanceoftheoperationstopifthismissionsucceedsthenitwillbeonlyamatterofdaysbeforethewarbeginsandyourthreecomradesinlondonwillplayacrucialroleincoordinatingourintelligenceeffortsbehindenemylinesstopwehavebeenwatchingandlisteningcarefullyforsignsthattheoperationcouldhavebeencompromisedandasaresultofthoseenquirieswehavediscoveredanewtopsecretagencyknownonlybytheacronymbossstopwehavebeenunabletoinfiltratethisgroupbutitisknownthattheleaderissomeonewiththeinitialhandthattheyaretakingtheleadonsignalsintelligencestoptheheadofthecounterintelligencebranchusesthecodenamepearlandwewillbemakingeveryefforttodiscoverheridentitystopwhilethedeclarationofwarandthedestructionoftherdfsystemarethecentralgoalsofthisoperationthelondontrianglewillbekeytoensuringaflowofhighqualityintelligencetoinformourbattleplansstopwithouttheircommunicationswewillbeunabletomonitorbritishplansplacinggermanlivesatunnecessaryriskstopifthereisanychancethatmembersofyourgroupcouldbecapturedandinterrogatedthenyoushouldabandonthesabotageoperationandreturntotirpitzuferstopprotectingtheidentityofthetriangleisoffargreaterlongtermimportancetousstopwecanfindotherwaystoprovokethewarbutwewouldfinditmoredifficulttoensurethatwecanwinitwithoutanetworkofagentsinthecapitalstopthemissionwillberegardedasasuccessonlyifwecanguaranteetheirsafetystoptoincreasethesecurityofthelondontriangleitiscrucialthattheclockcipherdeviceandtheassociatedcodebooksreachourlondonagentsstopthedevicecannotbeallowedtofallintoenemyhandsandthismustbethetopprioritystopifyouarecapturedthenthecodebookcontainingthekeystotheciphermustbedestroyedstopitisprintedonspecialpaperthatshouldeasilydissolvebutitcanalsobeingestedifnecessarystopwhenusingthemachinetakecaretoerasethecipheralphabetafteruseandtodeletethekeyfromthecodebooktoensurethatnooneusesthesamekeytwicestopthiswillalsoreducetheriskofthekeyedcipherfallingintoenemyhandsstopifcaptureappearsimminentthenuseoneoftheshapedchargestodestroythedevicepreferablyburyingittogetherwiththechargetoincreasethedestructivepowerandmakeithardertoreconstructstopeveryoneinthemovementwillbeprayingforyoursuccessandyourcourageandcommitmenttothefatherlandwillbepraisedforeverstopyouareaninspirationtousallstopwishingyouluckinthefinalphaseoftheoperationstopyoursunclewilhelm'"
957 ]
958 },
959 "execution_count": 229,
960 "metadata": {},
961 "output_type": "execute_result"
962 }
963 ],
964 "source": [
965 "cipherstream = zip(cycle(p_alpha), cycle(rotated_c_alpha))\n",
966 "p_gen, s = decipher_message(scb, cipherstream)\n",
967 "p_gen"
968 ]
969 },
970 {
971 "cell_type": "code",
972 "execution_count": 233,
973 "metadata": {
974 "Collapsed": "false"
975 },
976 "outputs": [],
977 "source": [
978 "pb = prettify(p_gen)"
979 ]
980 },
981 {
982 "cell_type": "code",
983 "execution_count": 235,
984 "metadata": {
985 "Collapsed": "false"
986 },
987 "outputs": [
988 {
989 "data": {
990 "text/plain": [
991 "7298"
992 ]
993 },
994 "execution_count": 235,
995 "metadata": {},
996 "output_type": "execute_result"
997 }
998 ],
999 "source": [
1000 "open(plaintext_b_filename, 'w').write(pb)"
1001 ]
1002 },
1003 {
1004 "cell_type": "code",
1005 "execution_count": null,
1006 "metadata": {
1007 "Collapsed": "false"
1008 },
1009 "outputs": [],
1010 "source": []
1011 }
1012 ],
1013 "metadata": {
1014 "jupytext": {
1015 "formats": "ipynb,md"
1016 },
1017 "kernelspec": {
1018 "display_name": "Python 3",
1019 "language": "python",
1020 "name": "python3"
1021 },
1022 "language_info": {
1023 "codemirror_mode": {
1024 "name": "ipython",
1025 "version": 3
1026 },
1027 "file_extension": ".py",
1028 "mimetype": "text/x-python",
1029 "name": "python",
1030 "nbconvert_exporter": "python",
1031 "pygments_lexer": "ipython3",
1032 "version": "3.7.4"
1033 }
1034 },
1035 "nbformat": 4,
1036 "nbformat_minor": 4
1037 }