Done 2021 challenge 6
[cipher-challenge.git] / 2021 / 2021-challenge6.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 18,
6 "id": "5c19999b",
7 "metadata": {
8 "Collapsed": "false"
9 },
10 "outputs": [],
11 "source": [
12 "from szyfrow.caesar import *\n",
13 "from szyfrow.affine import *\n",
14 "from szyfrow.keyword_cipher import *\n",
15 "from szyfrow.column_transposition import *\n",
16 "from szyfrow.vigenere import *\n",
17 "from szyfrow.support.text_prettify import *\n",
18 "\n",
19 "import numpy as np\n",
20 "import pandas as pd\n",
21 "import matplotlib.pyplot as plt\n",
22 "\n",
23 "import collections\n",
24 "%matplotlib inline"
25 ]
26 },
27 {
28 "cell_type": "code",
29 "execution_count": 3,
30 "id": "d9dd1b5e",
31 "metadata": {
32 "Collapsed": "false"
33 },
34 "outputs": [],
35 "source": [
36 "challenge_number = 6\n",
37 "plaintext_a_filename = f'plaintext.{challenge_number}a.txt'\n",
38 "plaintext_b_filename = f'plaintext.{challenge_number}b.txt'\n",
39 "ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'\n",
40 "ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'"
41 ]
42 },
43 {
44 "cell_type": "code",
45 "execution_count": 20,
46 "id": "0f1f792a",
47 "metadata": {
48 "Collapsed": "false"
49 },
50 "outputs": [],
51 "source": [
52 "ca = open(ciphertext_a_filename).read()\n",
53 "cb = open(ciphertext_b_filename).read()\n",
54 "\n",
55 "sca = sanitise(ca)\n",
56 "rsca = cat(reversed(sca))\n",
57 "scb = sanitise(cb)\n",
58 "rscb = cat(reversed(scb))"
59 ]
60 },
61 {
62 "cell_type": "code",
63 "execution_count": 5,
64 "id": "b8d5f9ec-27f1-498b-8e64-c5eb2424e581",
65 "metadata": {},
66 "outputs": [
67 {
68 "data": {
69 "text/plain": [
70 "Counter({'r': 124,\n",
71 " 'm': 42,\n",
72 " 't': 227,\n",
73 " 'u': 65,\n",
74 " 'v': 24,\n",
75 " 'a': 163,\n",
76 " 's': 138,\n",
77 " 'w': 41,\n",
78 " 'c': 59,\n",
79 " 'i': 158,\n",
80 " 'f': 35,\n",
81 " 'd': 66,\n",
82 " 'e': 279,\n",
83 " 'l': 44,\n",
84 " 'j': 2,\n",
85 " 'h': 142,\n",
86 " 'o': 130,\n",
87 " 'n': 144,\n",
88 " 'b': 36,\n",
89 " 'g': 63,\n",
90 " 'y': 37,\n",
91 " 'p': 29,\n",
92 " 'k': 12,\n",
93 " 'q': 3,\n",
94 " 'x': 2})"
95 ]
96 },
97 "execution_count": 5,
98 "metadata": {},
99 "output_type": "execute_result"
100 }
101 ],
102 "source": [
103 "sca_counts = collections.Counter(sca)\n",
104 "sca_counts"
105 ]
106 },
107 {
108 "cell_type": "code",
109 "execution_count": 6,
110 "id": "b5cabba1-ac75-46ea-ac05-8699cc88d986",
111 "metadata": {},
112 "outputs": [
113 {
114 "data": {
115 "text/plain": [
116 "<AxesSubplot:>"
117 ]
118 },
119 "execution_count": 6,
120 "metadata": {},
121 "output_type": "execute_result"
122 },
123 {
124 "data": {
125 "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8rg+JYAAAACXBIWXMAAAsTAAALEwEAmpwYAAASH0lEQVR4nO3dfbBcdX3H8fdHtGgBLZSAKaBBJz5AVdSIdsQprVXwadBWbGiHUqtGLbTaWitYW63TtDj1Yaa2UFHUqCimKoUZrYipHaQ+YILIU2CMghpBiM8ZR9HEb//YE9le9unuvTf35pf3a2Zn9/z2/M753nPP/ezZ39k9N1WFJKkt91jsAiRJ889wl6QGGe6S1CDDXZIaZLhLUoMMd0lq0D0XuwCAgw8+uFasWLHYZUjSHmXTpk3frqplg55bEuG+YsUKNm7cuNhlSNIeJcnXhj3nsIwkNchwl6QGGe6S1CDDXZIaZLhLUoMMd0lqkOE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/QO+c0CmDnh9nxov/PsAyYNx5jpcCv9/N+yHgRRP87V8DPBb49a7e7yf5bFX9eESfL9Abrnsc8KvA25I8t6qeO6a+PwdOo3fBsHcAr6yqnyW5B/BlYGy4L9kTqrsk+U3gfvTePv50wPMPHNV/yh1zkroew10nwS6vqi9O0OdhwJPp/dFvqKrNC1Fbt67H0gub0BtiGDpWv1jbcClLcm1VPWKWfQZux3Hbr9uXdv2uJt2XpvqdTbPfLnUztsUO4Paq2jGmz9nAhTXiAwYj+u5P78X/r+h9KGLfEfOumvm3l+TUqhp5fiTJ64HzB/0ekzx8kuxY8uEuLYYkbwfeMuE7Oe0Fuk+wPIne0fvXuOuTM/+9qIUNYbhLAyTZDDyY3km9pTREpUWS5JX0An3TuHcGS4HhLg0w7RCLtFQY7pLUoCX5JSZJ0twY7pLUIMNdkhpkuEtSgwx3SWrQ/wGJhqR+GErYRAAAAABJRU5ErkJggg==\n",
126 "text/plain": [
127 "<Figure size 432x288 with 1 Axes>"
128 ]
129 },
130 "metadata": {
131 "needs_background": "light"
132 },
133 "output_type": "display_data"
134 }
135 ],
136 "source": [
137 "pd.Series(sca_counts).sort_index().plot.bar()"
138 ]
139 },
140 {
141 "cell_type": "code",
142 "execution_count": 7,
143 "id": "61ce2102-294e-4b1a-842c-1607aa7a4b05",
144 "metadata": {},
145 "outputs": [
146 {
147 "name": "stdout",
148 "output_type": "stream",
149 "text": [
150 "6 \n",
151 "\n",
152 "rnhsheesramriuewtasdtitnaceiglucseeeocervciainrorfaerbtlishirbsaenshatsasituehetwetsqniofncastwohaswiigndhfthraeraettotefrhthmithgdsrtayuackerwdhrahetlnitmsofmoasuoaurgeeugeeoovesrjulithtetrmattrehearihsinonecmhepuaownietserwysrcafntghhhecseiytuefesllcahonrsbeebgaatesethshtecaabbogweboantimdcurseoitiafsderinmoamndnaemhsiscasbveeosatrinhitwnlfsrucennhdedeeloebetnpbohethfefdarcenggnieeedintscutecsoshmrardyoloertoeaeoubnhsitmierhutetlhrdinstohaautahywsenetnsrgecnciethnpieneetntesetnhrugiysnhwviahwcataerfbttuedfihlfenrhegncbithehmgiiihembsehtomacoascdiiteetccuhghapharoimapsrdiastoonomhicaotindfthreaekeeourocanitoeisseudlhisthyttehatvtitirpentohateenesydblubnrceeyedteignudageihrweniiashsodneevysrrpelittstieetniviitnicuabmshatohertfwofedoarfiwleiouggieethtltehanyflettnoeawtmamhbtiseinoaniggevthnrwaeasrowneynitbgavondarowingiisipsacnbonhueytsdtnclgihnhouhicnosesraiuristitnkitsohwnuchdogoeaguwnomootpdcsslrhvaoeeitwneudcipatohnpsriotiiicpionhisregseuetudoshseocaneettmixreouhfyuogecldunyosttmhitneslnoekeifoylaobssueaerqejiedhyrhaeaisuyihreemssthrebenhyrdioyfosauikhhtnottahaswerlrsagooirodnnebutactwntnetictthnoldohduuroegenhevtgetneceneonsotheettrlereasiketwgewavisaetuusesmishetvnlvimoedohewarrondsokthosaatfsiavkasinrdihatratceildogatesotcaltyalyanathehtinttarothtveetaheiemisgbmrorleftrgithnceoubnopinoyraocfuruehseesinetirxppneedstnagsoshaghgtmvbtdevrelietabundetsteorebotfechttreytinergupttianmpsssmteibwahthoreeamcinsnaeweorladievyietsgaitgtieehonfarsptycnduahermctdeteenafevyttighihtneeitlmaskasonilnashglytefditertmeniearigwetiwvicttatuubwahttatyesragtseqrituegnofwnehhyodydnlratseteeinllnttgincthtewesuntraehertnmereiitttdtrrabacikchrtahousaghaeocsrudrosgtfeeieheiatysatihgtnggosasetheehateimhtebehateeadtelouegantnihnopgnenaentioidthrelefharsetaldaebgtiacinoldewowgnthnisthaatearhcdvgnietrsgmmtfaaroethettiriiairuaiwnishthekamgehtneahhesutnuerekessanithrybebsolstnhsonnwentdehoneurantepdpgsrepddeoryhttswsahetpassdreenmnohepnatitcisyconoutswenibrevhaodetuddaoesthparatregyahyasaeotisnaeapgarcoagnrdlotherrorepstedvtoidierppuofeauorerntrfooeemohwsnt\n"
153 ]
154 }
155 ],
156 "source": [
157 "key_a, score_a = railfence_break(sca, fitness=Ptrigrams)\n",
158 "print(key_a, '\\n')\n",
159 "pa = railfence_decipher(sca, key_a)\n",
160 "print(pa)"
161 ]
162 },
163 {
164 "cell_type": "code",
165 "execution_count": 25,
166 "id": "605792ee-8bbf-44da-b33f-6bd2321f449e",
167 "metadata": {},
168 "outputs": [
169 {
170 "name": "stdout",
171 "output_type": "stream",
172 "text": [
173 "(2, 4, 3, 0, 1) False True \n",
174 "\n",
175 "harryiamsurethismustbeveryhardforyouasiknowthatcharliewasagoodfriendbutwecantletthatcloudourjudgementgiventhecontentofherletterlastweekwehavetoassumesheisinvolvedsomehowandworkonthatbasishavingsaidthatcharliedoesntactuallysayanythingthatprovesthatsheisamemberofthelighthouseconspiracyofcoursesheisanexperiencedagentsothatmightbedeliberatebutshedoesrefertothetrinitygroupanditspossiblethattheamericanswerealreadyinvestigatingtheconspiracyunderthatcodenameeverythingintheemailtakesonaslightlydifferentmeaningifweviewitthatwaybutthatraisesthequestionofwhytheydidntsharetheintelligencewithusinthemeantimeitriedtotrackbackthroughagathasrecordtoseeifthereisanythingtosuggestthatshemighthavebeenadoubleagentnothingmentionedinthefileraisedaflagbutinoticedtwothingsintheattachedvettingsummaryafterherinitialruninwiththegermantreasurehuntersseekingthebabylonstoneshewentontherunandpapersreportedthatshewasapassengeronthetitanicasyouknowsheneverboardedandusedthattragedyasawaytodisappearaccordingtothereportshedidnotreappearforoverfourteenmonthswhenshewasrecruitedasanintelligencesourceincairoforthebritishembassythatraisesthequestionofwhatshewasdoingfortheyeartheotherthingthatstruckachordwasthementionofmassouriegeorgeeverestbuiltaretreatthereandhisnamecameupwheniwasresearchingthesecurityfilesofcharlesbabbageitseemsthatbabbagewasintroducedtoideasfromindianmathematicsbyeverestandthisinfluencedthedevelopmentofthedifferenceengineanditssuccessorsmaryboolewroteaboutthisinherletterindianthoughtandwesternscienceinthenineteenthcenturygivenwhatchwroteaboutthedifferenceenginethismightbemorethanacoincidencethoughperhapsiamreadingtoomuchintoitforthesakeofourtraineesishouldsaythatthevettingreporthasbeendoubleencryptedusingavigenerewithasecondverysimpletwistimentionitbecausethatthrewmeoffforawhileisuggestthattheyreflectonwhatmightbegoingongiventhatwearenowtryingtoavoidraisingsuspicionbytheusandthelighthouseconspiratorsithinkweshouldchangeourownprotocolssoihaveswitcheduptoatranspositioncipherisuggestyoudothesamenexttimethoughyoucouldtrysomethingelselikeapolybiussquarejodie\n"
176 ]
177 }
178 ],
179 "source": [
180 "(word_a, fill_a, empty_a), score_a = column_transposition_break(sca, fitness=Ptrigrams)\n",
181 "print(word_a, fill_a, empty_a, '\\n')\n",
182 "pa = column_transposition_decipher(sca, word_a, fillcolumnwise=fill_a, emptycolumnwise=empty_a)\n",
183 "print(pa)"
184 ]
185 },
186 {
187 "cell_type": "code",
188 "execution_count": 27,
189 "id": "b66bf02f-9e8b-421d-8549-ab00774465ce",
190 "metadata": {},
191 "outputs": [
192 {
193 "data": {
194 "text/plain": [
195 "(2, 4, 3, 0, 1)"
196 ]
197 },
198 "execution_count": 27,
199 "metadata": {},
200 "output_type": "execute_result"
201 }
202 ],
203 "source": [
204 "transpositions_of('deacb')"
205 ]
206 },
207 {
208 "cell_type": "code",
209 "execution_count": 29,
210 "id": "b9fa44e0-fd0c-4ff3-b62b-81db5c4b6109",
211 "metadata": {},
212 "outputs": [
213 {
214 "data": {
215 "text/plain": [
216 "['gibed',\n",
217 " 'gybed',\n",
218 " 'hodge',\n",
219 " 'image',\n",
220 " 'imbed',\n",
221 " 'jodie',\n",
222 " 'judge',\n",
223 " 'lobed',\n",
224 " 'locke',\n",
225 " 'lodge',\n",
226 " 'lubed',\n",
227 " 'lucid',\n",
228 " 'noble',\n",
229 " 'noemi',\n",
230 " 'nudge',\n",
231 " 'osage',\n",
232 " 'prank',\n",
233 " 'pubic',\n",
234 " 'pylon',\n",
235 " 'quake',\n",
236 " 'ruble',\n",
237 " 'stage',\n",
238 " 'staid',\n",
239 " 'stake',\n",
240 " 'stale',\n",
241 " 'stalk',\n",
242 " 'stand',\n",
243 " 'stank',\n",
244 " 'staph',\n",
245 " 'stare',\n",
246 " 'stark',\n",
247 " 'stern',\n",
248 " 'stink',\n",
249 " 'sucre',\n",
250 " 'swage',\n",
251 " 'swami',\n",
252 " 'swank',\n",
253 " 'sward',\n",
254 " 'swarm',\n",
255 " 'swirl',\n",
256 " 'tubed',\n",
257 " 'twang',\n",
258 " 'twerp',\n",
259 " 'twirl',\n",
260 " 'wyeth',\n",
261 " 'fibbed',\n",
262 " 'fobbed',\n",
263 " 'gobbed',\n",
264 " 'jobbed',\n",
265 " 'judged',\n",
266 " 'lobbed',\n",
267 " 'lodged',\n",
268 " 'lucile',\n",
269 " 'lychee',\n",
270 " 'mobbed',\n",
271 " 'muddle',\n",
272 " 'mumble',\n",
273 " 'noodle',\n",
274 " 'nudged',\n",
275 " 'osages',\n",
276 " 'puddle',\n",
277 " 'ruanda',\n",
278 " 'rubbed',\n",
279 " 'rubble',\n",
280 " 'rubier',\n",
281 " 'rubric',\n",
282 " 'rwanda',\n",
283 " 'stages',\n",
284 " 'stakes',\n",
285 " 'stales',\n",
286 " 'stalks',\n",
287 " 'stands',\n",
288 " 'staphs',\n",
289 " 'starer',\n",
290 " 'stares',\n",
291 " 'stated',\n",
292 " 'static',\n",
293 " 'stereo',\n",
294 " 'sterne',\n",
295 " 'sterns',\n",
296 " 'stinks',\n",
297 " 'subbed',\n",
298 " 'sudoku',\n",
299 " 'summon',\n",
300 " 'swamis',\n",
301 " 'swanee',\n",
302 " 'swanks',\n",
303 " 'swards',\n",
304 " 'swarms',\n",
305 " 'swirls',\n",
306 " 'sydney',\n",
307 " 'tubule',\n",
308 " 'tycoon',\n",
309 " 'imbibed',\n",
310 " 'inching',\n",
311 " 'kneeing',\n",
312 " 'lucille',\n",
313 " 'muddied',\n",
314 " 'muddled',\n",
315 " 'nodding',\n",
316 " 'noodled',\n",
317 " 'prairie',\n",
318 " 'premier',\n",
319 " 'puddled',\n",
320 " 'quahaug',\n",
321 " 'ruddier',\n",
322 " 'rwandan',\n",
323 " 'stalest',\n",
324 " 'starter',\n",
325 " 'stashes',\n",
326 " 'statics',\n",
327 " 'steepen',\n",
328 " 'steeple',\n",
329 " 'stereos',\n",
330 " 'sterner',\n",
331 " 'sternes',\n",
332 " 'stetson',\n",
333 " 'subsume',\n",
334 " 'succeed',\n",
335 " 'sudokus',\n",
336 " 'summons',\n",
337 " 'susanne',\n",
338 " 'swansea',\n",
339 " 'swashes',\n",
340 " 'sydneys',\n",
341 " 'tuition',\n",
342 " 'imbedded',\n",
343 " 'indigene',\n",
344 " 'premiere',\n",
345 " 'starters',\n",
346 " 'steeples',\n",
347 " 'sternest',\n",
348 " 'stetsons',\n",
349 " 'subsumes',\n",
350 " 'succeeds',\n",
351 " 'summonss',\n",
352 " 'susannes',\n",
353 " 'swanseas',\n",
354 " 'statistic',\n",
355 " 'steeliest',\n",
356 " 'steepness',\n",
357 " 'sternness',\n",
358 " 'succeeded',\n",
359 " 'statistics',\n",
360 " 'steepnesss',\n",
361 " 'sternnesss',\n",
362 " 'ouagadougou']"
363 ]
364 },
365 "execution_count": 29,
366 "metadata": {},
367 "output_type": "execute_result"
368 }
369 ],
370 "source": [
371 "transpositions[word_a]"
372 ]
373 },
374 {
375 "cell_type": "code",
376 "execution_count": 16,
377 "id": "c7f2e770-8062-4d18-b90a-cb2e410ef88c",
378 "metadata": {},
379 "outputs": [
380 {
381 "name": "stdout",
382 "output_type": "stream",
383 "text": [
384 "harry i am sure this must be very hard for you as i know that charlie was a good friend but we cant\n",
385 "let that cloud our judgement given the content of her letter last week we have to assumes he is\n",
386 "involved somehow and work on that basis having said that charlie doesnt actually say anything that\n",
387 "proves that she is a member of the lighthouse conspiracy of course she is an experienced agent so\n",
388 "that might be deliberate but she does refer to the trinity group and its possible that the americans\n",
389 "were already investigating the conspiracy under that codename everything in the email takes on a\n",
390 "slightly different meaning if we view it that way but that raises the question of why they didnt\n",
391 "share the intelligence with us in the meantime i tried to trackback through agatha s record to see\n",
392 "if there is anything to suggest that she might have been a double agent nothing mentioned in the\n",
393 "file raised a flag but i noticed two things in the attached vetting summary after her initial run in\n",
394 "with the german treasure hunters seeking the babylon stone she went on the run and papers reported\n",
395 "that she was a passenger on the titanic as you know she never boarded and used that tragedy as away\n",
396 "to disappear according to the report she did not reappear for over fourteen months when she was\n",
397 "recruited as an intelligence source in cairo for the british embassy that raises the question of\n",
398 "what she was doing for the year the other thing that struck a chord was the mention of mass our ie\n",
399 "george everest built a retreat there and his name came up when i was researching the security files\n",
400 "of charles babbage it seems that babbage was introduced to ideas from indian mathematics by everest\n",
401 "and this influenced the development of the difference engine and its successors mary boole wrote\n",
402 "about this in her letter indian thought and western science in the nineteenth century given what ch\n",
403 "wrote about the difference engine this might be more than a coincidence though perhaps i am reading\n",
404 "too much into it for the sake of our trainees i should say that the vetting report has been double\n",
405 "encrypted using avi genere with a second very simple twist i mention it because that threw me off\n",
406 "for a while i suggest that they reflect on what might be going on given that we are now trying to\n",
407 "avoid raising suspicion by the us and the lighthouse conspirators i think we should change our own\n",
408 "protocols so i have switched up to a transposition cipher i suggest you do the same next time though\n",
409 "you could try something else like a polybius square jodie\n"
410 ]
411 }
412 ],
413 "source": [
414 "print(prettify(pa))"
415 ]
416 },
417 {
418 "cell_type": "code",
419 "execution_count": 17,
420 "id": "6cecddd9",
421 "metadata": {
422 "Collapsed": "false"
423 },
424 "outputs": [
425 {
426 "data": {
427 "text/plain": [
428 "2525"
429 ]
430 },
431 "execution_count": 17,
432 "metadata": {},
433 "output_type": "execute_result"
434 }
435 ],
436 "source": [
437 "open(plaintext_a_filename, 'w').write(prettify(pa))"
438 ]
439 },
440 {
441 "cell_type": "code",
442 "execution_count": 21,
443 "id": "16210c33-3bcb-43c8-9189-d82eea7cf53b",
444 "metadata": {},
445 "outputs": [
446 {
447 "name": "stdout",
448 "output_type": "stream",
449 "text": [
450 "nittevg \n",
451 "\n",
452 "yllaunnaibdesiverebdluohsharofecnaraelcehttessarojamasatubnrecnocrofsdnuorgtnerruconeraerehtmsicafybdesoptaerhtetaidemmieromehtmorftcartsidotylekilyrevdnasmiadetatsstinideeccusotylekilnusatidedragerohwyratercesngierofehtybdetcejersawlasoporpehtmehtllatserofotredronistaerhterutufgniniltuostroperegnargnoleraperpotsawmiadetatsehtsrethgilpmalehtpuorgsihtdemankcinehsgninnalpdrawrofotdetacidedycnegaehtfohcnarbwenafohcnualehtgnidnemmocerrepapadetfardhahcinumrofgnivaelerofebevitaitininworehtadehcnualsawhcihwnetsimehclaeidnoitagitsevnitsetalrehotdeltahwsisihtylbamuserpdralizstsitneicsnairagnuhehtdnatforkcockciwdahcgnidulcnidoirepsihtgnirudsrotisivforebmunllamsadahehsnagebynamregninoissimtnerrucrehlitnudevilehserehwhciwneergnitnemtrapanakoothakuehtotnrutertnecerrehnosnoitareponeewtebdnanohtobsecruosernwonkrehhtiwtnetsisnocelytsefiladnasdneirfwefsahehseiruossamnigniyatsaidniniyadilohgniklawakootehsnoissimtnecertsomrehfodneehttaaisadnaacirfanisdoireptnacifingisdnepsotnwonksidnakuehtniesabtnenamreponsahhacilbuperramiewehtnignisilaicepseciffoeporueruonievitarepodaelaneebsahehsselliasrevecnisadnagaporpdnaegatobasgnitanidroocelihwsnoitisopsidpoortnostroperelbaulavdnaralugerdedivorpehserehwecnarfniseitudotdengissaersawehstcilfnocehtfokaerbtuoehtnotnavelehtniesitrepxerehfoesuaceboriacotdengissayllaitinisawdnarawtaergehtfokaerbtuoehterofebhtnomenonoitasinagroehtdeniojhaecnaraelcrehfoweivercidoirepdradnatsasisihtevobadnatercespottasseccallufsahdnasesacrojamnohhtiwylevisnetxedekrowsahehsasuehtdnakuehtnisnoitcennocgnortshtiwytinummocecnegilletniehtforebmemdedragerllewasihatnegaiiixxxmcmenujyrammusdleifhgihahtagaetadpugnittev\n"
453 ]
454 }
455 ],
456 "source": [
457 "word_b, score_b = vigenere_frequency_break(scb, fitness=Ptrigrams)\n",
458 "print(word_b, '\\n')\n",
459 "pb = vigenere_decipher(scb, word_b)\n",
460 "print(pb)"
461 ]
462 },
463 {
464 "cell_type": "code",
465 "execution_count": 22,
466 "id": "595593e0-9b09-4ef8-974f-9969ff327941",
467 "metadata": {},
468 "outputs": [
469 {
470 "name": "stdout",
471 "output_type": "stream",
472 "text": [
473 "vetting \n",
474 "\n",
475 "vettingupdateagathahighfieldsummaryjunemcmxxxiiiagentahisawellregardedmemberoftheintelligencecommunitywithstrongconnectionsintheukandtheusashehasworkedextensivelywithhonmajorcasesandhasfullaccessattopsecretandabovethisisastandardperiodicreviewofherclearanceahjoinedtheorganisationonemonthbeforetheoutbreakofthegreatwarandwasinitiallyassignedtocairobecauseofherexpertiseinthelevantontheoutbreakoftheconflictshewasreassignedtodutiesinfrancewheresheprovidedregularandvaluablereportsontroopdispositionswhilecoordinatingsabotageandpropagandasinceversaillesshehasbeenaleadoperativeinoureuropeofficespecialisingintheweimarrepublicahhasnopermanentbaseintheukandisknowntospendsignificantperiodsinafricaandasiaattheendofhermostrecentmissionshetookawalkingholidayinindiastayinginmassourieshehasfewfriendsandalifestyleconsistentwithherknownresourcesbothonandbetweenoperationsonherrecentreturntotheukahtookanapartmentingreenwichwheresheliveduntilhercurrentmissioningermanybeganshehadasmallnumberofvisitorsduringthisperiodincludingchadwickcockroftandthehungarianscientistszilardpresumablythisiswhatledtoherlatestinvestigationdiealchemistenwhichwaslaunchedatherowninitiativebeforeleavingformunichahdraftedapaperrecommendingthelaunchofanewbranchoftheagencydedicatedtoforwardplanningshenicknamedthisgroupthelamplightersthestatedaimwastopreparelongrangereportsoutliningfuturethreatsinordertoforestallthemtheproposalwasrejectedbytheforeignsecretarywhoregardeditasunlikelytosucceedinitsstatedaimsandverylikelytodistractfromthemoreimmediatethreatposedbyfacismtherearenocurrentgroundsforconcernbutasamajorassettheclearanceforahshouldberevisedbiannually\n"
476 ]
477 }
478 ],
479 "source": [
480 "word_b, score_b = vigenere_frequency_break(rscb, fitness=Ptrigrams)\n",
481 "print(word_b, '\\n')\n",
482 "pb = vigenere_decipher(rscb, word_b)\n",
483 "print(pb)"
484 ]
485 },
486 {
487 "cell_type": "code",
488 "execution_count": 23,
489 "id": "fe094361-7c85-40d9-952e-0c84abbcf903",
490 "metadata": {},
491 "outputs": [
492 {
493 "name": "stdout",
494 "output_type": "stream",
495 "text": [
496 "vetting update agatha highfield summary june mcm xxxiii agent a his a well regarded member of the\n",
497 "intelligence community with strong connections in the uk and the usa she has worked extensively with\n",
498 "hon major cases and has full access at top secret and above this is a standard periodic review of\n",
499 "her clearance ah joined the organisation one month before the outbreak of the great war and was\n",
500 "initially assigned to cairo because of her expertise in the levant on the outbreak of the conflict\n",
501 "she was reassigned to duties in france where she provided regular and valuable reports on troop\n",
502 "dispositions while coordinating sabotage and propaganda since versailles she has been a lead\n",
503 "operative in our europe office specialising in the weimar republic ah has no permanent base in the\n",
504 "uk and is known to spend significant periods in africa and asia at the end of her most recent\n",
505 "mission she took a walking holiday in india staying in mass our ie she has few friends and a\n",
506 "lifestyle consistent with her known resources both on and between operations on her recent return to\n",
507 "the uk ah took an apartment in greenwich where she lived until her current mission in germany began\n",
508 "she had a small number of visitors during this period including chadwick cockroft and the hungarian\n",
509 "scientist szilard presumably this is what led to her latest investigation die alchemist en which was\n",
510 "launched at her own initiative before leaving for munich ah drafted a paper recommending the launch\n",
511 "of a new branch of the agency dedicated to forward planning she nicknamed this group the\n",
512 "lamplighters the stated aim was to prepare long range reports outlining future threats in order to\n",
513 "forestall them the proposal was rejected by the foreign secretary who regarded it as unlikely to\n",
514 "succeed in its stated aims and very likely to distract from the more immediate threat posed by\n",
515 "facism there are no current grounds for concern but as a major asset the clearance for ah should be\n",
516 "revised biannually\n"
517 ]
518 }
519 ],
520 "source": [
521 "pb = vigenere_decipher(rscb, word_b)\n",
522 "print(prettify(pb))"
523 ]
524 },
525 {
526 "cell_type": "code",
527 "execution_count": 24,
528 "id": "d12a663c",
529 "metadata": {
530 "Collapsed": "false"
531 },
532 "outputs": [
533 {
534 "data": {
535 "text/plain": [
536 "1967"
537 ]
538 },
539 "execution_count": 24,
540 "metadata": {},
541 "output_type": "execute_result"
542 }
543 ],
544 "source": [
545 "open(plaintext_b_filename, 'w').write(prettify(pb))"
546 ]
547 },
548 {
549 "cell_type": "code",
550 "execution_count": null,
551 "id": "504ec2e2",
552 "metadata": {
553 "Collapsed": "false"
554 },
555 "outputs": [],
556 "source": []
557 }
558 ],
559 "metadata": {
560 "jupytext": {
561 "formats": "ipynb,md"
562 },
563 "kernelspec": {
564 "display_name": "Python 3 (ipykernel)",
565 "language": "python",
566 "name": "python3"
567 },
568 "language_info": {
569 "codemirror_mode": {
570 "name": "ipython",
571 "version": 3
572 },
573 "file_extension": ".py",
574 "mimetype": "text/x-python",
575 "name": "python",
576 "nbconvert_exporter": "python",
577 "pygments_lexer": "ipython3",
578 "version": "3.8.8"
579 }
580 },
581 "nbformat": 4,
582 "nbformat_minor": 5
583 }