Done challenge 7
[cipher-challenge.git] / 2021 / 2021-challenge7.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
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.polybius import *\n",
18 "from szyfrow.support.text_prettify import *\n",
19 "\n",
20 "import numpy as np\n",
21 "import pandas as pd\n",
22 "import matplotlib.pyplot as plt\n",
23 "\n",
24 "import collections\n",
25 "%matplotlib inline"
26 ]
27 },
28 {
29 "cell_type": "code",
30 "execution_count": 2,
31 "id": "d9dd1b5e",
32 "metadata": {
33 "Collapsed": "false"
34 },
35 "outputs": [],
36 "source": [
37 "challenge_number = 7\n",
38 "plaintext_a_filename = f'plaintext.{challenge_number}a.txt'\n",
39 "plaintext_b_filename = f'plaintext.{challenge_number}b.txt'\n",
40 "ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'\n",
41 "ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'"
42 ]
43 },
44 {
45 "cell_type": "code",
46 "execution_count": 7,
47 "id": "0f1f792a",
48 "metadata": {
49 "Collapsed": "false"
50 },
51 "outputs": [],
52 "source": [
53 "nca = open(ciphertext_a_filename).read()\n",
54 "cb = open(ciphertext_b_filename).read()\n",
55 "\n",
56 "numtrans = ''.maketrans('12345', 'abcde')\n",
57 "ca = nca.translate(numtrans)\n",
58 "\n",
59 "sca = sanitise(ca)\n",
60 "rsca = cat(reversed(sca))\n",
61 "scb = sanitise(cb)\n",
62 "rscb = cat(reversed(scb))"
63 ]
64 },
65 {
66 "cell_type": "code",
67 "execution_count": 9,
68 "id": "b8d5f9ec-27f1-498b-8e64-c5eb2424e581",
69 "metadata": {},
70 "outputs": [
71 {
72 "data": {
73 "text/plain": [
74 "Counter({'e': 632, 'c': 619, 'a': 714, 'b': 410, 'd': 787})"
75 ]
76 },
77 "execution_count": 9,
78 "metadata": {},
79 "output_type": "execute_result"
80 }
81 ],
82 "source": [
83 "sca_counts = collections.Counter(sca)\n",
84 "sca_counts"
85 ]
86 },
87 {
88 "cell_type": "code",
89 "execution_count": 10,
90 "id": "b5cabba1-ac75-46ea-ac05-8699cc88d986",
91 "metadata": {},
92 "outputs": [
93 {
94 "data": {
95 "text/plain": [
96 "<AxesSubplot:>"
97 ]
98 },
99 "execution_count": 10,
100 "metadata": {},
101 "output_type": "execute_result"
102 },
103 {
104 "data": {
105 "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD1CAYAAACrz7WZAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8rg+JYAAAACXBIWXMAAAsTAAALEwEAmpwYAAARfklEQVR4nO3dfYxd+V3f8fcnduKQ8BCbHRtje2MjTRe8EXnQ4GwbHmNgjdLiBWE0EaQW3ciV6pTwIBW7qhSCNNL+AYgHsUgWoR0eGmsIbNciUhpjiCIgiTOb3TSxN9ZO4409sWsPIYFAFQcv3/4xZ+sb7x3PtWeur/Pz+yVZ55zv+Z1zv3Nkf+7xmXPuTVUhSWrLi0bdgCRp9RnuktQgw12SGmS4S1KDDHdJapDhLkkNWjvqBgDuueee2r59+6jbkKSvKk888cTfVNVYv3V3RLhv376d2dnZUbchSV9VknxmqXVelpGkBg0U7kl+NsmpJJ9M8u4kL02yIcnxJM900/U94w8nmUtyJsmDw2tfktTPsuGeZAvw08BEVb0KWANMAoeAE1U1Dpzolkmys1t/P7AHeDTJmuG0L0nqZ9DLMmuBr0myFngZcAHYC0x366eBh7r5vcDRqrpSVWeBOWDXqnUsSVrWsuFeVZ8Ffhk4B1wE/q6q3g9sqqqL3ZiLwMZuky3A+Z5dzHc1SdJtMshlmfUsno3vAL4ZeHmSn7zRJn1qL/joySQH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ofVV95nb1ojtPktcB39UtfrCqnhxlP6PUXbrdzeKb/omqenrELS3JcJekBnmfuyQ1yHCXpAYZ7pLUIMNdkhpkuEtSg/4fZamgIRhtg7UAAAAASUVORK5CYII=\n",
106 "text/plain": [
107 "<Figure size 432x288 with 1 Axes>"
108 ]
109 },
110 "metadata": {
111 "needs_background": "light"
112 },
113 "output_type": "display_data"
114 }
115 ],
116 "source": [
117 "pd.Series(sca_counts).sort_index().plot.bar()"
118 ]
119 },
120 {
121 "cell_type": "code",
122 "execution_count": 11,
123 "id": "583312cf-a973-4671-9ba8-85fe10cb9e00",
124 "metadata": {},
125 "outputs": [
126 {
127 "data": {
128 "text/plain": [
129 "(('alchemy', <KeywordWrapAlphabet.from_a: 1>, 'abcde', 'abcde', False),\n",
130 " -6776.263168839725)"
131 ]
132 },
133 "execution_count": 11,
134 "metadata": {},
135 "output_type": "execute_result"
136 }
137 ],
138 "source": [
139 "key_a, score_a = polybius_break(sca, column_labels='abcde', row_labels='abcde',\n",
140 " fitness=Ptrigrams)\n",
141 "key_a, score_a"
142 ]
143 },
144 {
145 "cell_type": "code",
146 "execution_count": 13,
147 "id": "4262216b-efed-491e-ade1-d7e8dffe4454",
148 "metadata": {},
149 "outputs": [
150 {
151 "data": {
152 "text/plain": [
153 "'wemounhtbeattacbehkessayebdhhendntbeunherizdnycoheomandkayesentdnfungkaditopeopieattbeuseklasszdtkdybtbavesidppehpastuslutiucgdizbarrzanhdbahaireahzmdyurehouttbattbezkdybttrztocokkundcatetbdswaztbeconspdraczbahavodhehhetectdonmoroveracenturzsotbezwereunidgeiztokagetbekdstageomsenhdnykessayesexpidcdtizahhressehtooneanotberdtdsaiwazslestnottokageidngsletweenkeklersomanetworganhaweiibdhhenkessayednfungkadiavodhstbatrdsgespecdaiizdmdtdswdheizhdstrdlutehlzoneomtbelotnetworgsidgetbdsonedwastoomocussehontbehecrzptatmdrstanhwasntreahdnywbatdtsadhuntdidmounhbernakestardnylacgatkeattbeenhperbapswesbouihbaveaireahzreaidsehtbattrdndtzcouihlednvoivehlutdtneveroccurrehtoketbatbernakewasanztbdnyotbertbanacodncdhencetbemactsbescbaridesndecesbouihbaveidtuptbeidybtsontbehasbloarhtbouybanhdakworrdehtbatbarrzhdhntradsedtperbapsbedsaireahzsbahowdnyberanhhdhntidgetosazsooutomiozaitzlutbehdhntteiiketbatanhsokebowdtbdngbewouihdtbdngdtskoreidgeiztbatsbeslezonhsuspdcdondnbdskdnhsbebaspiazehandkportantroiednbdsnetworgmormourteenzearsanhtbemacttbatsbekdybtleahoulieayentwdiileareailiowtbatwaslahlutdtwasnttbeworstomdttbehetaditrdndtzydvesontbeconspdraczsrecentactdvdtdesdstruizmrdybtendnywbatcouihtbezwantattbebeartomtbepanhekdcresponseyroupssureiztbezcantbaveleenresponsdliemortbeoutlreaganhwbatweretbezhodnyatcopdhesperateizwanttotagetbdstobarrztotaigtobdkaloutdtluttbemdnaiparayrapbstoppehkednkztracgswbzoneartbhoestrdndtztbdngdkdybtfodntbekwbatbavedsadhorhonetbatwouihkagetbektbdngdcouihleatradtoranhwdiitbatkagebarrzhdstrustketoodknotsuredcantagetbatrdsgdkdybtbavetomdndsbtbdskdssdonaione'"
154 ]
155 },
156 "execution_count": 13,
157 "metadata": {},
158 "output_type": "execute_result"
159 }
160 ],
161 "source": [
162 "word_a, wrap_a, col_a, row_a, col_first_a = key_a\n",
163 "polybius_decipher(sca, keyword=word_a, column_order=col_a, row_order=row_a,\n",
164 " column_first=col_first_a, wrap_alphabet=wrap_a)"
165 ]
166 },
167 {
168 "cell_type": "code",
169 "execution_count": 15,
170 "id": "bbe56403-8038-4659-9f3d-7056ef60e665",
171 "metadata": {},
172 "outputs": [
173 {
174 "data": {
175 "text/plain": [
176 "'xefpvoduheauuachednettagehiddeoiouhevodesmziogcpdepfaoinageteouiokvolnaimupqepqmeauuhevtenbattziunighuhawetmiqqedqatuvtbvumvclimzhasszaodihadamseadzfigvsedpvuuhauuheznighuuszupcpnnvoicaueuhitxazuhecpotqisaczhadawpideddeuecuipofpspwesaceouvsztpuhezxesevomilemzupnaleuhenitualepfteodiognettageteyqmiciumzaddsetteduppoeaopuhesiuitamxaztbetuopuupnalemioltbeuxeeonenbestpfaoeuxpslaodaxemmhiddeonettageiokvolnaimawpidtuhausitletqeciammzifiuitxidemzditusibvuedbzpoepfuhebpuoeuxpsltmileuhitpoeixatuppfpcvttedpouhedecszquaufistuaodxatouseadiogxhauiutaidvouimifpvodhesoanetuasiogbaclauneauuheeodqeshaqtxethpvmdhaweamseadzseamiteduhauusioiuzcpvmdbeiowpmwedbvuiuoewespccvssedupneuhauhesoanexataozuhiogpuhesuhaoacpiocideoceuhefacuthetchasmietoiecethpvmdhawemiuvquhemighutpouhedathbpasduhpvghaodianxpssieduhauhasszdidousaiteiuqeshaqtheitamseadzthadpxioghesaoddidoumileuptaztppvupfmpzamuzbvuhedidouuemmneuhauaodtpnehpxiuhiolhexpvmdiuhioliutnpsemilemzuhauthetbezpodtvtqicipoiohitniodthehatqmazedaoinqpsuaouspmeiohitoeuxpslfpsfpvsueeozeastaoduhefacuuhauthenighubeadpvbmeageouximmbeaseambmpxuhauxatbadbvuiuxatouuhexpstupfiuuhedeuaimusioiuzgiwetpouhecpotqisacztseceouacuiwiuietitusvmzfsighueoiogxhaucpvmduhezxaouauuheheasupfuheqaodenicsetqpotegspvqttvsemzuhezcaouhawebeeosetqpotibmefpsuhepvubsealaodxhauxeseuhezdpiogaucpqidetqesauemzxaouupualeuhituphasszupuamluphinabpvuiubvuuhefioamqasagsaqhtupqqedneionzusacltxhzpoeasuhdpetusioiuzuhiolinighukpiouhenxhauhaweitaidpsdpoeuhauxpvmdnaleuhenuhiolicpvmdbeausaiupsaodximmuhaunalehasszditusvtuneuppinoputvseicaoualeuhausitlinighuhaweupfioithuhitnittipoampoe'"
177 ]
178 },
179 "execution_count": 15,
180 "metadata": {},
181 "output_type": "execute_result"
182 }
183 ],
184 "source": [
185 "xca = polybius_decipher(sca, keyword='a', column_order=col_a, row_order=row_a,\n",
186 " column_first=col_first_a, wrap_alphabet=wrap_a)\n",
187 "xca"
188 ]
189 },
190 {
191 "cell_type": "code",
192 "execution_count": 18,
193 "id": "81c5bc01-b60b-4d26-9f19-231854f4f6de",
194 "metadata": {},
195 "outputs": [
196 {
197 "data": {
198 "text/plain": [
199 "'abcdefghiklmnopqrstuvwxyzj'"
200 ]
201 },
202 "execution_count": 18,
203 "metadata": {},
204 "output_type": "execute_result"
205 }
206 ],
207 "source": [
208 "word_a, _ = monoalphabetic_sa_break(xca)\n",
209 "word_a"
210 ]
211 },
212 {
213 "cell_type": "code",
214 "execution_count": 24,
215 "id": "7aa6dff2-f12c-4212-83df-3a9bb7b76334",
216 "metadata": {},
217 "outputs": [
218 {
219 "data": {
220 "text/plain": [
221 "'wefoundtheattachedmessagehiddenintheunderlyingcodeofanimagesentinjunkmailtopeopleattheusembassyitmighthaveslippedpastusbutluckilyharryandihadalreadyfiguredoutthattheymighttrytocommunicatethiswaytheconspiracyhadavoideddetectionforoveracenturysotheywereunlikelytomakethemistakeofsendingmessagesexplicitlyaddressedtooneanotheritisalwaysbestnottomakelinksbetweenmembersofanetworkandawellhiddenmessageinjunkmailavoidsthatriskespeciallyifitiswidelydistributedbyoneofthebotnetworkslikethisoneiwastoofocussedonthedecryptatfirstandwasntreadingwhatitsaiduntilifoundhernamestaringbackatmeattheendperhapsweshouldhavealreadyrealisedthattrinitycouldbeinvolvedbutitneveroccurredtomethathernamewasanythingotherthanacoincidencethefactshescharliesnieceshouldhavelitupthelightsonthedashboardthoughandiamworriedthatharrydidntraiseitperhapsheisalreadyshadowingheranddidntliketosaysooutofloyaltybuthedidnttellmethatandsomehowithinkhewouldithinkitsmorelikelythatshesbeyondsuspicioninhismindshehasplayedanimportantroleinhisnetworkforfourteenyearsandthefactthatshemightbeadoubleagentwillbearealblowthatwasbadbutitwasnttheworstofitthedetailtrinitygivesontheconspiracysrecentactivitiesistrulyfrighteningwhatcouldtheywantattheheartofthepandemicresponsegroupssurelytheycanthavebeenresponsiblefortheoutbreakandwhatweretheydoingatcopidesperatelywanttotakethistoharrytotalktohimaboutitbutthefinalparagraphstoppedmeinmytrackswhyonearthdoestrinitythinkimightjointhemwhathaveisaidordonethatwouldmakethemthinkicouldbeatraitorandwillthatmakeharrydistrustmetooimnotsureicantakethatriskimighthavetofinishthismissionalone'"
222 ]
223 },
224 "execution_count": 24,
225 "metadata": {},
226 "output_type": "execute_result"
227 }
228 ],
229 "source": [
230 "pa = keyword_decipher(xca, word_a)\n",
231 "pa"
232 ]
233 },
234 {
235 "cell_type": "code",
236 "execution_count": 27,
237 "id": "c7f2e770-8062-4d18-b90a-cb2e410ef88c",
238 "metadata": {},
239 "outputs": [
240 {
241 "name": "stdout",
242 "output_type": "stream",
243 "text": [
244 "we found the attached message hidden in the underlying code of an image sent in junkmail to people\n",
245 "at the us embassy it might have slipped past us but luckily harry and i had already figured out that\n",
246 "they might try to communicate this way the conspiracy had avoided detection for over a century so\n",
247 "they were unlikely to make the mistake of sending messages explicitly addressed to one another it is\n",
248 "always best not to make links between members of a network and a well hidden message in junkmail\n",
249 "avoids that risk especially if it is widely distributed by one of the bot networks like this one i\n",
250 "was too focussed on the decrypt at first and wasnt reading what it said until i found her name\n",
251 "staring back at meat the end perhaps we should have already realised that trinity could be involved\n",
252 "but it never occurred to me that her name was anything other than a coincidence the fact shes\n",
253 "charlies niece should have lit up the lights on the dashboard though and i am worried that harry\n",
254 "didnt raise it perhaps he is already shadowing her and didnt like to say so out of loyalty but he\n",
255 "didnt tell me that and somehow i think he would i think its more likely that shes beyond suspicion\n",
256 "in his mind she has played an important role in his network for fourteen years and the fact that she\n",
257 "might be a double agent will be a real blow that was bad but it wasnt the worst of it the detail\n",
258 "trinity gives on the conspiracy s recent activities is truly frightening what could they want at the\n",
259 "heart of the pandemic response groups surely they cant have been responsible for the outbreak and\n",
260 "what were they doing at copi desperately want to take this to harry to talk to him about it but the\n",
261 "final paragraph stopped me in my tracks why on earth does trinity think i might join them what have\n",
262 "i said or done that would make them think i could be a traitor and will that make harry distrust me\n",
263 "too im not sure i can take that risk i might have to finish this mission alone\n"
264 ]
265 }
266 ],
267 "source": [
268 "print(prettify(pa))"
269 ]
270 },
271 {
272 "cell_type": "code",
273 "execution_count": 28,
274 "id": "6cecddd9",
275 "metadata": {
276 "Collapsed": "false"
277 },
278 "outputs": [
279 {
280 "data": {
281 "text/plain": [
282 "1953"
283 ]
284 },
285 "execution_count": 28,
286 "metadata": {},
287 "output_type": "execute_result"
288 }
289 ],
290 "source": [
291 "open(plaintext_a_filename, 'w').write(prettify(pa))"
292 ]
293 },
294 {
295 "cell_type": "code",
296 "execution_count": 33,
297 "id": "07c92adb-74e2-476e-adff-887c0145617d",
298 "metadata": {},
299 "outputs": [
300 {
301 "data": {
302 "text/plain": [
303 "'10101 11000'"
304 ]
305 },
306 "execution_count": 33,
307 "metadata": {},
308 "output_type": "execute_result"
309 }
310 ],
311 "source": [
312 "cb[:11]"
313 ]
314 },
315 {
316 "cell_type": "code",
317 "execution_count": 34,
318 "id": "832dc153-c9e3-4134-9425-8014847c3870",
319 "metadata": {},
320 "outputs": [
321 {
322 "data": {
323 "text/plain": [
324 "21"
325 ]
326 },
327 "execution_count": 34,
328 "metadata": {},
329 "output_type": "execute_result"
330 }
331 ],
332 "source": [
333 "int('10101', 2)"
334 ]
335 },
336 {
337 "cell_type": "code",
338 "execution_count": 31,
339 "id": "d7002750-66fd-4435-8ee8-023ae8376f33",
340 "metadata": {},
341 "outputs": [
342 {
343 "data": {
344 "text/plain": [
345 "9865"
346 ]
347 },
348 "execution_count": 31,
349 "metadata": {},
350 "output_type": "execute_result"
351 }
352 ],
353 "source": [
354 "scb = cat(c for c in cb if c in '01')\n",
355 "len(scb)"
356 ]
357 },
358 {
359 "cell_type": "code",
360 "execution_count": 39,
361 "id": "f3c2587e-7b76-49fb-ba60-ed3efc148c85",
362 "metadata": {},
363 "outputs": [
364 {
365 "data": {
366 "text/plain": [
367 "(0, 25)"
368 ]
369 },
370 "execution_count": 39,
371 "metadata": {},
372 "output_type": "execute_result"
373 }
374 ],
375 "source": [
376 "ncb = [int(g, 2) for g in cb.split()]\n",
377 "min(ncb), max(ncb)"
378 ]
379 },
380 {
381 "cell_type": "code",
382 "execution_count": 40,
383 "id": "bf189676-3c25-420d-9330-b77cc57c7a82",
384 "metadata": {},
385 "outputs": [
386 {
387 "data": {
388 "text/plain": [
389 "'vyietbcbuwabmfbeslwnqafcylayovkbuxrhcwalhlpvknvmpbjszmxrbeonvwwhlznzkgortuilzxvvawmgvvlogumljprqzfmvvvvzqnjxvkbmgyiezrqmvizqlyvkqimewbedramgzrbvvzmniweotlbjigqhlteljmayovbblhcovzlbaggxerktlmfxhbmfxdwsnmfxjkrvmsgwwebnltkmygbkpfzxqgepzbudxprtwaamptzvrlkclfcekxqwvacqmctctbckntelrubaiimcikymzwaabraraomxlarzqehpdfdrzmfxcifbxgzybrmgkhebuatqiciavxbpvcfmwravkeqlglkwruucwdwemhnxiigqoclrbgpxfxrzgwyehmmevfcgklrkbqbfvzidggxcalxpmympwockfnfizctelgpxmmymevxuvfuzqmrxvajmgmpyiimtlxkebzdmyjkvmgrbjbfigbxeovvxckjqaeagmvpnteubkpgpxbbimpbxykfnfmgghikvdbjlvziigrlrvqxhjbkqpqtllkprgtjlfprticwlagwblyztgztrxtwcobtbeohawgkvkgivaxjagwtlndjrzhdhkprzzmovzauxlmjbuimftuzrutggvlomrmguwhzkcttpjpbjxnmjmkcgfbnjecmfnhtewwvtvdxphewhzijteaswkrarbrdxlmkprkhlgvkgqhllnmziwcpzbuahkxfngpxuhitqaeykxmfbvmfgiaqxqtelfutjevagamymvajqejllzrtrntpwsnblmymzmwgndbrzfmgkprlhugjqqmbftmmabaywkprktnttqggblxvlgwpmkbwabacufafqgtxjbvotrbfvvvmmhlzbzzygzanbbmgkpnbbquvqaoksgwzbumfxrzpptchcwtgwgozavwgfximnbzaahquiwfhgmqbaymrafqzlfvvguxygkbuimravknaxutjzrotpwvlnaempgzvwkgmpjhbaykignalgzemqpbquvagizcgkrblbcmfzhvbrteltiocavzntfmlkcatbkbkmqzxqhlzpmlqavpnamctdanteyviwfamfxlsgztuezvtbaphloubacirxrzluxcmsbucazvqimravtvoarafcfmtlwkzlqgemfebzdmnkeuimravgzmtlbrufwtlzignbfwlvtsnhpmyigjnrbyiimickjwaiejrtprkdcwrvqiejmymzicmkzbruldkfugpxdhlvqimgheiekagoverzxqazxcmwmnkbbutqlfceqxygupndxzxvvfmvskvlnbmfxgiesbyljqtvxbtkmnummfrsranpxkpnbmfxbmllhandmabluximczhnxitlmgakpxgmwggtifmcmwzmfbxyfwqallravuocmebmmabacmrtrvmqavqfehpdzvtebrazvbemfbesjmfgzybamxbmfbnsxdnibumkqmvxfigbbgtnvmmozavbmfxyynalmheifqvygxmgipyrnprbackfzawmhhuqrigbavzgmtkpfzxwnrpyigexykvlbqgemymomlrprggwfydvahzxrarbuikpruwragmmwqalhsmdqtpmzxkwpwgtbekrpxpgfbgwmcecpvubftmmomxlvrzegblzfcgahkxsipszphlvqkacvbanvwgtdebvwckzvtqyuxjpbcebclagbkwmfzrkksbkprzwgkvkgtrgyjpretqpfzxqgepzbuclravvvulskvaumpmnclfmxravvrmwdhiarkkcvpbumwgkvkgiinkfippbqkzaxgusmkprwketeqfimghepnagcovzfpbcwrengyphdbuimzxwwemhprfcnvwgpfcylgruvebzdggxnbzbrbkpvvdgmzanzbqdnwebartbqaormnitbdblzeqrkxrkzvvbr'"
390 ]
391 },
392 "execution_count": 40,
393 "metadata": {},
394 "output_type": "execute_result"
395 }
396 ],
397 "source": [
398 "tcb = cat(unpos(n) for n in ncb)\n",
399 "tcb"
400 ]
401 },
402 {
403 "cell_type": "code",
404 "execution_count": 41,
405 "id": "59f072b0-f8ca-4f08-922d-cfb5dfc68fe4",
406 "metadata": {},
407 "outputs": [
408 {
409 "data": {
410 "text/plain": [
411 "'beuxmwnprjiztavlsfgkqdyhco'"
412 ]
413 },
414 "execution_count": 41,
415 "metadata": {},
416 "output_type": "execute_result"
417 }
418 ],
419 "source": [
420 "word_b, score_b = monoalphabetic_sa_break(tcb)\n",
421 "word_b"
422 ]
423 },
424 {
425 "cell_type": "code",
426 "execution_count": 43,
427 "id": "16210c33-3bcb-43c8-9189-d82eea7cf53b",
428 "metadata": {},
429 "outputs": [
430 {
431 "name": "stdout",
432 "output_type": "stream",
433 "text": [
434 "trinity \n",
435 "\n",
436 "charlieidontthinkyoushouldhavecometolondonwecantriskmeetingandyourarrivalhasbeennoticedbybossheighteningsuspicionharrysteamisactivelyinvestigatingourorganisationandwehavetodoeverythingwecantoputthemoffthescentunfortunatelyimworkingwithveryconstrainedresourcesdespiteallourpandemicpreparationsithasbeenhardworkoverthelasteighteenmonthsasplannedweusedthecrisistoembedmoreoperativesattheheartofgovernmentdecisionmakingunderthecoverofsageandtheothernewcommitteeswenowhaveanetworkofscientistsandengineersinwhitehallwiththedirectearofseniorcivilservantsandpoliticianstheyalsohelpedustoinfiltratecopgivingusdirectaccesstoanumberofothergovernmentsthathadremainedbeyondourreachwhilewewerenotabletofullydeliveronourplansforthateventtheconnectionswemadewithsomeoftheworldslargestcompaniesandsmalleststateswillsurelypayoffinthemediumtermonthedownsideihaventhadthecapacityineedtoworkonthebossinvestigationintoourorganisationthatisbeingrunfromthearchaeologydivisionhereatgchqihadhopedthatassignmentmeantthatthecasewasregardedaslowprioritybutharryassignedhisbestagentjodietorunitandgaveheralmostunlimitedresourcesshehasteamsallacrosstheuktrawlingthroughthepapersweleftbehindatthelighthouseandtryingtoworkoutwhattheymeaniamsoangryatmyselfforthatbutihavepersonallycheckedandallthemajoritemsfromthefoundationarchivewereshippedouttomassourieandhavebeensecuredattheparkiassignedateamtomakesurethatthekeydocumentswereproperlyencryptedincasejodiesteamfindsthembutgiventhetalentsheisworkingwithinowthinkwemightneedtotakefurtherstepsandiplantovisitthehqassoonasicangetawaywhetherornotjodieandherteamworkoutwhatwearedoingthebestwaytomakesurethatharrydoesnotfindoutmightbetoconvincehernottotellhimihavebeencarryingoutsomebackgroundchecksandiamwonderingifweshouldjusttrytorecruitherdirectlyifshewasworkingwithusthenimsureshewouldseetheneedforsecrecythedirectapproachisriskybuttheorganisationhasnevershiedawayfromthatbeforeoryouandiwouldntbeworkingforitithinkitisariskworthtakingyourlovingniecetrinity\n"
437 ]
438 }
439 ],
440 "source": [
441 "word_b, score_b = vigenere_frequency_break(tcb, fitness=Ptrigrams)\n",
442 "print(word_b, '\\n')\n",
443 "pb = vigenere_decipher(tcb, word_b)\n",
444 "print(pb)"
445 ]
446 },
447 {
448 "cell_type": "code",
449 "execution_count": 44,
450 "id": "fe094361-7c85-40d9-952e-0c84abbcf903",
451 "metadata": {},
452 "outputs": [
453 {
454 "name": "stdout",
455 "output_type": "stream",
456 "text": [
457 "charlie i dont think you should have come to london we cant risk meeting and your arrival has been\n",
458 "noticed by boss heightening suspicion harry steam is actively investigating our organisation and we\n",
459 "have to do everything we can to put them off the scent unfortunately im working with very\n",
460 "constrained resources despite all our pandemic preparations it has been hard work over the last\n",
461 "eighteen months as planned we used the crisis to embed more operatives at the heart of government\n",
462 "decisionmaking under the cover of sage and the other new committees we now have a network of\n",
463 "scientists and engineers in whitehall with the direct ear of senior civil servants and politicians\n",
464 "they also helped us to infiltrate cop giving us direct access to a number of other governments that\n",
465 "had remained beyond our reach while we were notable to fully deliver on our plans for that event the\n",
466 "connections we made with some of the worlds largest companies and smallest states will surely payoff\n",
467 "in the medium term on the downside i havent had the capacity i need to work on the boss\n",
468 "investigation into our organisation that is being run from the archaeology division here at gchq i\n",
469 "had hoped that assignment meant that the case was regarded as low priority but harry assigned his\n",
470 "best agent jodie to run it and gave her almost unlimited resources she has teams all across the uk\n",
471 "trawling through the papers we left behind at the lighthouse and trying to workout what they mean i\n",
472 "am so angry at myself for that but i have personally checked and all the major items from the\n",
473 "foundation archive were shipped out to mass our ie and have been secured at the park i assigned a\n",
474 "team to make sure that the key documents were properly encrypted in case jodie steam finds them but\n",
475 "given the talent she is working with i now think we might need to take further steps and i plan to\n",
476 "visit the hq as soon as i can getaway whether or not jodie and her teamwork out what we are doing\n",
477 "the best way to make sure that harry does not find out might be to convince her not to tell him i\n",
478 "have been carrying out some background checks and i am wondering if we should just try to recruit\n",
479 "her directly if she was working with us then im sure she would see the need for secrecy the direct\n",
480 "approach is risky but the organisation has never shied away from that before or you and i wouldnt be\n",
481 "working for it i think it is a risk worth taking your loving niece trinity\n"
482 ]
483 }
484 ],
485 "source": [
486 "print(prettify(pb))"
487 ]
488 },
489 {
490 "cell_type": "code",
491 "execution_count": 45,
492 "id": "d12a663c",
493 "metadata": {
494 "Collapsed": "false"
495 },
496 "outputs": [
497 {
498 "data": {
499 "text/plain": [
500 "2420"
501 ]
502 },
503 "execution_count": 45,
504 "metadata": {},
505 "output_type": "execute_result"
506 }
507 ],
508 "source": [
509 "open(plaintext_b_filename, 'w').write(prettify(pb))"
510 ]
511 },
512 {
513 "cell_type": "code",
514 "execution_count": null,
515 "id": "504ec2e2",
516 "metadata": {
517 "Collapsed": "false"
518 },
519 "outputs": [],
520 "source": []
521 }
522 ],
523 "metadata": {
524 "jupytext": {
525 "formats": "ipynb,md"
526 },
527 "kernelspec": {
528 "display_name": "Python 3 (ipykernel)",
529 "language": "python",
530 "name": "python3"
531 },
532 "language_info": {
533 "codemirror_mode": {
534 "name": "ipython",
535 "version": 3
536 },
537 "file_extension": ".py",
538 "mimetype": "text/x-python",
539 "name": "python",
540 "nbconvert_exporter": "python",
541 "pygments_lexer": "ipython3",
542 "version": "3.8.8"
543 }
544 },
545 "nbformat": 4,
546 "nbformat_minor": 5
547 }