Updated for challenge 9
[cipher-tools.git] / 2018 / 2018-challenge9.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Biolerplate"
8 ]
9 },
10 {
11 "cell_type": "code",
12 "execution_count": 1,
13 "metadata": {},
14 "outputs": [],
15 "source": [
16 "import os,sys,inspect\n",
17 "currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))\n",
18 "parentdir = os.path.dirname(currentdir)\n",
19 "sys.path.insert(0,parentdir) "
20 ]
21 },
22 {
23 "cell_type": "code",
24 "execution_count": 2,
25 "metadata": {},
26 "outputs": [],
27 "source": [
28 "from cipher.caesar import *\n",
29 "from cipher.affine import *\n",
30 "from cipher.keyword_cipher import *\n",
31 "from cipher.vigenere import *\n",
32 "from cipher.playfair import *\n",
33 "from cipher.column_transposition import *\n",
34 "from support.text_prettify import *\n",
35 "from support.plot_frequency_histogram import *"
36 ]
37 },
38 {
39 "cell_type": "code",
40 "execution_count": 13,
41 "metadata": {},
42 "outputs": [],
43 "source": [
44 "ca = open('9a.ciphertext').read()\n",
45 "cb = open('9b.ciphertext').read()\n",
46 "sca = sanitise(ca)\n",
47 "scb = sanitise(cb)\n",
48 "pca = letters(ca)\n",
49 "pcb = letters(cb)"
50 ]
51 },
52 {
53 "cell_type": "code",
54 "execution_count": 5,
55 "metadata": {},
56 "outputs": [
57 {
58 "data": {
59 "text/plain": [
60 "8197"
61 ]
62 },
63 "execution_count": 5,
64 "metadata": {},
65 "output_type": "execute_result"
66 }
67 ],
68 "source": [
69 "history_words = [w.strip() for w in open('history-words.txt')]\n",
70 "len(history_words)"
71 ]
72 },
73 {
74 "cell_type": "code",
75 "execution_count": 6,
76 "metadata": {},
77 "outputs": [],
78 "source": [
79 "history_transpositions = collections.defaultdict(list)\n",
80 "for word in history_words:\n",
81 " history_transpositions[transpositions_of(word)] += [word]"
82 ]
83 },
84 {
85 "cell_type": "markdown",
86 "metadata": {},
87 "source": [
88 "# Part A"
89 ]
90 },
91 {
92 "cell_type": "code",
93 "execution_count": 7,
94 "metadata": {},
95 "outputs": [
96 {
97 "name": "stderr",
98 "output_type": "stream",
99 "text": [
100 "/usr/local/lib/python3.6/dist-packages/matplotlib/figure.py:445: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.\n",
101 " % get_backend())\n"
102 ]
103 },
104 {
105 "data": {
106 "image/png": "iVBORw0KGgoAAAANSUhEUgAAAbAAAAEmCAYAAAADccV0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAFH1JREFUeJzt3XvQZHV95/H3J4w3MBGBR0IY8MFImSVmXXBkcUlSBjSLioGtJS5GA2RJTYy4ahJjICaFZWkVRmtdt2rjZlQW3LAIEg0kaJQaMOAFdGYAGS7KLBeZKS6PNyJSEUa++aMPqZ6L0/eZ+U2/X1Vdzzmnf78+3z7PefrTv9Onz5OqQpKk1vzUri5AkqRxGGCSpCYZYJKkJhlgkqQmGWCSpCYZYJKkJhlgkqQmGWCSpCYZYJKkJhlgkqQmLdvVBQAccMABtbi4uKvLkCTtBtauXfvtqloY1G5ggCU5HzgReKiqXrjVfX8EfABYqKpvJwnwIeBVwKPAGVW1btA6FhcXWbNmzaBmkqQ5kOTeYdoNcwjxAuCE7azgEODXgW/1LX4lcHh3Wwl8eJgiJEka1cAAq6prge9u564PAu8A+i9nfxLw8eq5Htg3yUFTqVSSpD5jncSR5CRgU1XdvNVdBwP39c1v7JZJkjRVI5/EkWRv4E/pHT4cW5KV9A4zcuihh07yUJKkOTTOCOzngcOAm5PcAywH1iX5WWATcEhf2+Xdsm1U1aqqWlFVKxYWBp5sIknSFkYOsKq6paqeU1WLVbVI7zDhUVX1AHAFcFp6jgEerqr7p1uyJElDBFiSi4GvAC9Is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
107 "text/plain": [
108 "<Figure size 432x288 with 1 Axes>"
109 ]
110 },
111 "metadata": {
112 "needs_background": "light"
113 },
114 "output_type": "display_data"
115 }
116 ],
117 "source": [
118 "fc = collections.Counter(sca)\n",
119 "plot_frequency_histogram(fc, sort_key=fc.get)"
120 ]
121 },
122 {
123 "cell_type": "code",
124 "execution_count": 8,
125 "metadata": {},
126 "outputs": [
127 {
128 "data": {
129 "text/plain": [
130 "'dynamite'"
131 ]
132 },
133 "execution_count": 8,
134 "metadata": {},
135 "output_type": "execute_result"
136 }
137 ],
138 "source": [
139 "key_a, score = vigenere_frequency_break(sca)\n",
140 "key_a"
141 ]
142 },
143 {
144 "cell_type": "code",
145 "execution_count": 9,
146 "metadata": {},
147 "outputs": [
148 {
149 "data": {
150 "text/plain": [
151 "'theengineersharrybroughtwithhimwantedtotakesawsandtorchestothetablesafebutneitherofuswerekeenicouldseethatitwouldbeworthalottocollectorsandprobablybelongedinamuseumharrysaidheagreedbutihadafeelinghewanteditinstalledbackinheadquarterstherewassomethingproprietorialinthewayhelookedatitittookthetwoofuseighteenhourstocrackitbutwithasquadronofspecialforcesfromherefordguardingustherewasnoriskthatmyattackerswouldreturniaskedharryhowhegotauthorisationtobringtheminafterallwestillwerentsurethattherewereanynationalsecurityimplicationsbuthejustlaughedandpointedoutthatwewerecurrentlysomewhereundernewscotlandyardandithadnttakenmuchtoconvincethehomesecretarythatweneededapropersecurityteamtoguardtheshadowarchiveanotherteamofanalystshadcampedononesideofthecommandcentreandwerecombingthroughblackscodebookandthefewpapersthatourantagonistshadleftbehindfromtimetotimeoneofthetrooperswouldarrivewithanotherboxofdocumentsretrievedfromasideroomoramoredistantpartofthearchivelabyrinthmostcouldbedecryptedquicklyusingthestandardtoolsblackhadusedarangeofciphersincludingsubstitutionandtranspositionandalmostnoneofitwasharderthanavigenereitwasslowsincethepapershadtobehandledwithcareandthescannertheyhadbroughtwiththemwasntreallydesignedforthissortofworkacoupleoftimestheycalledharryovertoshowhimsomethingandhereaditnoddedthencamebacktoworkonthesafeasweworkedwetalkedaboutwhatihadlearnedconcerningtheshadowarchiveandblackssecretworkforthegovernmentharrydidntgivemuchawaybutihadafeelinghehadheardsomeofthisstorybeforeandafterthesecondtimehewascalledawayheseemedparticularlythoughtfuliaskedhimwhatwasbotheringhimbuthewasenjoyingbeingcryptichesaidsomethingaboutthebalkanproblemthenmutteredsteveturnerspoemhistorylessonhistoryrepeatsitselfhastonoonelistensicouldntgetanythingmoreoutofhimexceptsomecommentaboutworkingbackwardsthroughthepaperssoiturnedmyattentionbacktothesafethemechanismwasbiggerandheavierthanwewereusedtoandhadbeenexquisitelymadethecomponentsslidalmostnoiselesslyoveroneanotherandtheloudclicksihadheardwhenmovingthetableseemedtobetheresultofabrokencogtoothcatchingonthebarrelsthatmadeitevenhardertoopenwhatlittlenoisethelocktumblersmadewasheavilydisguisedandwehadtoresorttodrillingasmallaccessholetopassanendoscopeintotheinterioritwasaswellwehadtakentheeffortsittinginthemiddleofapileofnotebooksandpaperswasasmallpileofwhatlookedlikeexplosivesthesafewasriggedtoblowifwemadeamistake'"
152 ]
153 },
154 "execution_count": 9,
155 "metadata": {},
156 "output_type": "execute_result"
157 }
158 ],
159 "source": [
160 "vigenere_decipher(sca, key_a)"
161 ]
162 },
163 {
164 "cell_type": "code",
165 "execution_count": 10,
166 "metadata": {},
167 "outputs": [
168 {
169 "name": "stdout",
170 "output_type": "stream",
171 "text": [
172 "the engineers harry brought with him wanted to take saws and torches to the table safe but neither\n",
173 "of us were keen i could see that it would be worth alot to collectors and probably belonged in a\n",
174 "museum harry said he agreed but i had a feeling he wanted it installed back in headquarters there\n",
175 "was something proprietor i al in the way he looked at it it took the two of us eighteen hours to\n",
176 "crack it but with a squadron of special forces from hereford guarding us there was no risk that my\n",
177 "attackers would return i asked harry how he got authorisation to bring them in after all we still\n",
178 "werent sure that there were any national security implications but he just laughed and pointed out\n",
179 "that we were currently somewhere under new scotland yard and it hadnt taken much to convince the\n",
180 "home secretary that we needed a proper security team to guard the shadow archive another team of\n",
181 "analysts had camped on one side of the command centre and were combing through blacks codebook and\n",
182 "the few papers that our antagonists had left behind from time to time one of the troopers would\n",
183 "arrive with another box of documents retrieved from aside room or a more distant part of the archive\n",
184 "labyrinth most could be decrypted quickly using the standard tools black had used a range of ciphers\n",
185 "including substitution and transposition and almost none of it was harder than avi genere it was\n",
186 "slow since the papers had to be handled with care and the scanner they had brought with them wasnt\n",
187 "really designed for this sort of work a couple of times they called harry over to show him something\n",
188 "and he read it nodded then came back to work on the safe as we worked we talked about what i had\n",
189 "learned concerning the shadow archive and blacks secret work for the government harry didnt give\n",
190 "much away but i had a feeling he had heard some of this story before and after the second time he\n",
191 "was called away he seemed particularly thoughtful i asked him what was bothering him but he was\n",
192 "enjoying being cryptic he said something about the balkan problem then muttered steve turners poem\n",
193 "history lesson history repeats itself has to no one listens i couldnt get anything more out of him\n",
194 "except some comment about working backwards through the papers so i turned my attention back to the\n",
195 "safe the mechanism was bigger and heavier than we were used to and had been exquisitely made the\n",
196 "components slid almost noiselessly over one another and the loud clicks i had heard when moving the\n",
197 "table seemed to be the result of a broken cog tooth catching on the barrels that made it even harder\n",
198 "to open what little noise the lock tumblers made was heavily disguised and we had to resort to\n",
199 "drilling a small access hole to pass an endoscope into the interior it was as well we had taken the\n",
200 "effort sitting in the middle of a pile of notebooks and papers was a small pile of what looked like\n",
201 "explosives the safe was rigged to blow if we made a mistake\n"
202 ]
203 }
204 ],
205 "source": [
206 "print(prettify(vigenere_decipher(sca, key_a)))"
207 ]
208 },
209 {
210 "cell_type": "code",
211 "execution_count": 11,
212 "metadata": {},
213 "outputs": [
214 {
215 "data": {
216 "text/plain": [
217 "2913"
218 ]
219 },
220 "execution_count": 11,
221 "metadata": {},
222 "output_type": "execute_result"
223 }
224 ],
225 "source": [
226 "open('9a.plaintext', 'w').write(prettify(vigenere_decipher(sca, key_a)))"
227 ]
228 },
229 {
230 "cell_type": "markdown",
231 "metadata": {},
232 "source": [
233 "# Part B"
234 ]
235 },
236 {
237 "cell_type": "code",
238 "execution_count": 14,
239 "metadata": {},
240 "outputs": [
241 {
242 "data": {
243 "text/plain": [
244 "'turkey'"
245 ]
246 },
247 "execution_count": 14,
248 "metadata": {},
249 "output_type": "execute_result"
250 }
251 ],
252 "source": [
253 "key_b, score = vigenere_frequency_break(scb)\n",
254 "key_b"
255 ]
256 },
257 {
258 "cell_type": "code",
259 "execution_count": 16,
260 "metadata": {},
261 "outputs": [
262 {
263 "data": {
264 "text/plain": [
265 "'erutuferuceseromdnawenagnitaercfoepohehtnimehthtiwylesolckrowoteunitnoclliwinoigertahtnidlohtoofasueviglliwainsobnikrowtentnegaruoosfiyltnenamrepnoitseuqnaklabehtelttesotspahrepdnasniaglairotirretnaissurenimrednuotepohewetatsnamottoehtotkcabainodecamgnidartybemitemasehttasetuoredartnaenarretidemehtfolortnocruognitnemecsurpycniagyamewtahtsruomurdraehevahidnaseitinutroppognidartnihcireblliwynamregniraazabcitamolpidehtfoerehpsomtadeiznerfehtesruocfosrewoprojamehtnomehthguorhtraeboterusserpgnirbnacewtahtepohehtniytilibatsniderutrunevahsnaklabehtssorcastnegaymseitrapdnasetatsrellamsehtnosisucofymtubweivaevaherusmailliwsrewoprojamehtfollassergnocehtfonrecnocelpicnirpehtsideveihcaebthgimnoitacifinusihtrehtehwdnawohfonoitseuqehtelbitapmocnieraesehtdnasweivlavirynamyreveraerehttahtrehtartubaediraelcakcalyehttahttonsitignidaelsimsitahtspahrepnoitacifinuehtdaeldluohsytraphcihwfoaediraelcondnanworiehtfosnoitibmalacitilopdnalarutlucdeiravhtiwdedividniamersevlesmehtsetatsnaklabehtnoitulosnaklabakeesohwesohtdnaaissurnihtiweildluohsrewopstifosucolehttahteveilebohwesohtneewtebtilpsylpeedsitubdeifinuneebevahylatidnaynamregtahtyawemasehtnisetatscivalsehtetinuotsmiatnemevomcivalsnapehtsisircehtfotuoyawaeesyameweveilebinehtnilrebtaadnegaehtlortnocnacewfitcilfnocrehtrufmorfesoloteromgnihtemosevahyehttahtdesilaerevahlladnatnenitnocehtssorcatpureyamrawtahttaerhtgnirehtagehteesotnugebevahsrewoprojamehtfollasevilnitsocrewolhcumatadeeccusthgimycamolpidfosdohtemwenruotahtsiepohymtsoctaergtanetfodnathguoberagninniwhtrowseirotcivllatahtweivymsititubthguorbnaemylerustsumitahtdiasdnaemdetcerrocehriafyalpotsihtdiasinehwnoituloserarofepohotnigebnacinilrebnissergnocgnidnepmiehttathguobebthgimecaeptahttcepsorpehthtiwyllanifwonnihtiwseimenegnithgifygreneelbaredisnocdednepxeevahidnallahetihwniralupopyllasrevinuneebtonevahsdohtemymsselehtenonmelborphsikrutehtotnoitulosnworuotneserpotsrewopgnidaelrehtohtiwetaitogenotnigebotelbaerewewdnocesdnaseimeneruofosecivrescitamolpidehtnihtiwkrowtentnegaruopolevedotelbaerewewtsrifsgnihtowtdeveihcayaledsihttahtflesymtrofmocidnaelbativeniehtyaledotdekrowseillaehtgnomanoisufnocgniwosfoygetartsruonoituloslanoitanretninaotelbanemaebotgniraeppaelihwnoitisophsitirbehtgnisilanigramylevitceffednaairtsuadnaynamregniseillarehhtiwnoitcaroftroppusgnidliubyltcefrepdnahrehdeyalpsahaissurderaefewtahtllaneebsahrawhsikrutossureht'"
266 ]
267 },
268 "execution_count": 16,
269 "metadata": {},
270 "output_type": "execute_result"
271 }
272 ],
273 "source": [
274 "pb1 = vigenere_decipher(scb, key_b)\n",
275 "pb1"
276 ]
277 },
278 {
279 "cell_type": "code",
280 "execution_count": 27,
281 "metadata": {},
282 "outputs": [
283 {
284 "name": "stdout",
285 "output_type": "stream",
286 "text": [
287 "the russo turkish war has been all that we feared russia has played her hand perfectly building\n",
288 "support for action with her allies in germany and austria and effectively marginalising the british\n",
289 "position while appearing to be amenable to an international solution our strategy of sowing\n",
290 "confusion among the allies worked to delay the inevitable and i comfort myself that this delay\n",
291 "achieved two things first we were able to develop our agent network within the diplomatic services\n",
292 "of our enemies and second we were able to begin to negotiate with other leading powers to present\n",
293 "our own solution to the turkish problem nonetheless my methods have not been universally popular in\n",
294 "whitehall and i have expended considerable energy fighting enemies within now finally with the\n",
295 "prospect that peace might be bought at the impending congress in berlin i can begin to hope for a\n",
296 "resolution when i said this to playfair he corrected me and said that i must surely mean brought but\n",
297 "it is my view that all victories worth winning are bought and often at great cost my hope is that\n",
298 "our new methods of diplomacy might succeed at a much lower cost in lives all of the major powers\n",
299 "have begun to see the gathering threat that war may erupt across the continent and all have realised\n",
300 "that they have something more to lose from further conflict if we can control the agenda at berlin\n",
301 "then i believe we may see away out of the crisis the pan slavic movement aims to unite the slavic\n",
302 "states in the same way that germany and italy have been unified but is deeply split between those\n",
303 "who believe that the locus of its power should lie within russia and those who seek a balkan\n",
304 "solution the balkan states themselves remain divided with varied cultural and political ambitions of\n",
305 "their own and no clear idea of which party should lead the unification perhaps that is misleading it\n",
306 "is not that they lack a clear idea but rather that there are very many rival views and these are\n",
307 "incompatible the question of how and whether this unification might be achieved is the principle\n",
308 "concern of the congress all of the major powers william sure have a view but my focus is on the\n",
309 "smaller states and parties my agents across the balkans have nurtured instability in the hope that\n",
310 "we can bring pressure to bear through the mon the major powers of course the frenzied atmosphere of\n",
311 "the diplomatic bazaar in germany will be rich in trading opportunities and i have heard rumours that\n",
312 "we may gain cyprus cementing our control of the mediterranean trade routes at the same time by\n",
313 "trading macedonia back to the ottoman state we hope to undermine russian territorial gains and\n",
314 "perhaps to settle the balkan question permanently if so our agent network in bosnia will give usa\n",
315 "foothold in that region i will continue to work closely with them in the hope of creating a new and\n",
316 "more secure future\n"
317 ]
318 }
319 ],
320 "source": [
321 "rpb1 = cat(reversed(pb1))\n",
322 "print(prettify(rpb1))"
323 ]
324 },
325 {
326 "cell_type": "code",
327 "execution_count": 28,
328 "metadata": {},
329 "outputs": [
330 {
331 "data": {
332 "text/plain": [
333 "2856"
334 ]
335 },
336 "execution_count": 28,
337 "metadata": {},
338 "output_type": "execute_result"
339 }
340 ],
341 "source": [
342 "open('9b.plaintext', 'w').write(prettify(rpb1))"
343 ]
344 },
345 {
346 "cell_type": "code",
347 "execution_count": null,
348 "metadata": {},
349 "outputs": [],
350 "source": []
351 }
352 ],
353 "metadata": {
354 "kernelspec": {
355 "display_name": "Python 3",
356 "language": "python",
357 "name": "python3"
358 },
359 "language_info": {
360 "codemirror_mode": {
361 "name": "ipython",
362 "version": 3
363 },
364 "file_extension": ".py",
365 "mimetype": "text/x-python",
366 "name": "python",
367 "nbconvert_exporter": "python",
368 "pygments_lexer": "ipython3",
369 "version": "3.6.7"
370 }
371 },
372 "nbformat": 4,
373 "nbformat_minor": 2
374 }