Done challenge 4
[cipher-challenge.git] / 2020 / 2020-a-challenge4.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 18,
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.support.text_prettify import *"
14 ]
15 },
16 {
17 "cell_type": "code",
18 "execution_count": 2,
19 "metadata": {
20 "Collapsed": "false"
21 },
22 "outputs": [],
23 "source": [
24 "challenge_number = 4\n",
25 "plaintext_a_filename = f'plaintext.{challenge_number}a.txt'\n",
26 "plaintext_b_filename = f'plaintext.{challenge_number}b.txt'\n",
27 "ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'\n",
28 "ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'"
29 ]
30 },
31 {
32 "cell_type": "code",
33 "execution_count": 3,
34 "metadata": {
35 "Collapsed": "false"
36 },
37 "outputs": [],
38 "source": [
39 "ca = open(ciphertext_a_filename).read()\n",
40 "sca = sanitise(ca)\n",
41 "cb = open(ciphertext_b_filename).read()\n",
42 "scb = sanitise(cb)"
43 ]
44 },
45 {
46 "cell_type": "code",
47 "execution_count": 4,
48 "metadata": {
49 "Collapsed": "false"
50 },
51 "outputs": [
52 {
53 "name": "stdout",
54 "output_type": "stream",
55 "text": [
56 "akkad KeywordWrapAlphabet.from_largest \n",
57 "\n",
58 "i have drawn a blank in my enqciries aboct uaistor st edmcnd and even harry is getting nowhere with his uontauts in the mod. we both agree that the silenue is very worrying. this is ulearly an area of interest as it presents a urcuial front in any potential invasion so we wocld have expeuted some interest in ocr intelligenue, or at least some pcsh bauk warning cs to keep away from basiu defenue installations like listening posts. the faut that no-one wants to talk aboct the fauilities in the area scggests that it is all very hcsh hcsh. something important is happening there and we probably want to find oct what.\n",
59 "\n",
60 "ocr agents in london kept watuh for the tocring party and reuorded them as arriving at liverpool street. the grocp uonsisted of twenty three uyulists who split cp and stayed with members of loual suoct grocps while their leader was hosted by a member of the loual ortsgrcppe. we reueived reports from k's network that the grocp has been invited to stay with the spalding rotary ulcb in linuoln and we need yoc to investigate any reports yoc uan find aboct that visit. i will uontince to press for information aboct strategiu developments in norwiuh and norfolk.\n",
61 "\n",
62 "the akela initiative has been given approval and i will be setting that cp this week. the intention is to enroll leaders and members of loual suoct grocps, train them in uocnter-intelligenue and cse them to host the spyulist tocring grocps in the hope of getting inside their operation. sinue fctcre uommcniuations may inulcde sensitive operational details of this initiative i intend to inurease seucrity by moving to a blouked keyword uipher.\n",
63 "\n",
64 "the attauhed doucment was interuepted by ocr london agents when the party posted it on arrival at liverpool street. as cscal it was addressed to tirpitzcfer. primary analysis scggests that it does not cse a scbstitction uipher.\n",
65 "\n",
66 "more to follow.\n",
67 "\n",
68 "pearl.\n",
69 "\n"
70 ]
71 }
72 ],
73 "source": [
74 "(word_a, wrap_a), score_a = keyword_break_mp(sca, fitness=Ptrigrams)\n",
75 "print(word_a, wrap_a, '\\n')\n",
76 "pa = keyword_decipher(ca, word_a, wrap_a)\n",
77 "print(pa)"
78 ]
79 },
80 {
81 "cell_type": "code",
82 "execution_count": 5,
83 "metadata": {
84 "Collapsed": "false"
85 },
86 "outputs": [
87 {
88 "name": "stdout",
89 "output_type": "stream",
90 "text": [
91 "akelmnopqrstuvwxyzbcdfghij \n",
92 "\n",
93 "i have drawn a blank in my enquiries about caistor st edmund and even harry is getting nowhere with his contacts in the mod. we both agree that the silence is very worrying. this is clearly an area of interest as it presents a crucial front in any potential invasion so we would have expected some interest in our intelligence, or at least some push back warning us to keep away from basic defence installations like listening posts. the fact that no-one wants to talk about the facilities in the area suggests that it is all very hush hush. something important is happening there and we probably want to find out what.\n",
94 "\n",
95 "our agents in london kept watch for the touring party and recorded them as arriving at liverpool street. the group consisted of twenty three cyclists who split up and stayed with members of local scout groups while their leader was hosted by a member of the local ortsgruppe. we received reports from k's network that the group has been invited to stay with the spalding rotary club in lincoln and we need you to investigate any reports you can find about that visit. i will continue to press for information about strategic developments in norwich and norfolk.\n",
96 "\n",
97 "the akela initiative has been given approval and i will be setting that up this week. the intention is to enroll leaders and members of local scout groups, train them in counter-intelligence and use them to host the spyclist touring groups in the hope of getting inside their operation. since future communications may include sensitive operational details of this initiative i intend to increase security by moving to a blocked keyword cipher.\n",
98 "\n",
99 "the attached document was intercepted by our london agents when the party posted it on arrival at liverpool street. as usual it was addressed to tirpitzufer. primary analysis suggests that it does not use a substitution cipher.\n",
100 "\n",
101 "more to follow.\n",
102 "\n",
103 "pearl.\n",
104 "\n"
105 ]
106 }
107 ],
108 "source": [
109 "word_a, score_a = simulated_annealing_break(sca, fitness=Ptrigrams,\n",
110 " plain_alphabet=string.ascii_lowercase, cipher_alphabet=keyword_cipher_alphabet_of('akkad', wrap_alphabet=KeywordWrapAlphabet.from_largest))\n",
111 "print(word_a, '\\n')\n",
112 "pa = keyword_decipher(ca, word_a)\n",
113 "print(pa)"
114 ]
115 },
116 {
117 "cell_type": "code",
118 "execution_count": 6,
119 "metadata": {
120 "Collapsed": "false"
121 },
122 "outputs": [
123 {
124 "name": "stdout",
125 "output_type": "stream",
126 "text": [
127 "i have drawn a blank in my enquiries about caistor st edmund and even harry is getting nowhere with his contacts in the mod. we both agree that the silence is very worrying. this is clearly an area of interest as it presents a crucial front in any potential invasion so we would have expected some interest in our intelligence, or at least some push back warning us to keep away from basic defence installations like listening posts. the fact that no-one wants to talk about the facilities in the area suggests that it is all very hush hush. something important is happening there and we probably want to find out what.\n",
128 "\n",
129 "our agents in london kept watch for the touring party and recorded them as arriving at liverpool street. the group consisted of twenty three cyclists who split up and stayed with members of local scout groups while their leader was hosted by a member of the local ortsgruppe. we received reports from k's network that the group has been invited to stay with the spalding rotary club in lincoln and we need you to investigate any reports you can find about that visit. i will continue to press for information about strategic developments in norwich and norfolk.\n",
130 "\n",
131 "the akela initiative has been given approval and i will be setting that up this week. the intention is to enroll leaders and members of local scout groups, train them in counter-intelligence and use them to host the spyclist touring groups in the hope of getting inside their operation. since future communications may include sensitive operational details of this initiative i intend to increase security by moving to a blocked keyword cipher.\n",
132 "\n",
133 "the attached document was intercepted by our london agents when the party posted it on arrival at liverpool street. as usual it was addressed to tirpitzufer. primary analysis suggests that it does not use a substitution cipher.\n",
134 "\n",
135 "more to follow.\n",
136 "\n",
137 "pearl.\n",
138 "\n"
139 ]
140 }
141 ],
142 "source": [
143 "pa = keyword_decipher(ca, 'akela', wrap_alphabet=KeywordWrapAlphabet.from_largest)\n",
144 "print(pa)"
145 ]
146 },
147 {
148 "cell_type": "code",
149 "execution_count": 7,
150 "metadata": {
151 "Collapsed": "false"
152 },
153 "outputs": [
154 {
155 "data": {
156 "text/plain": [
157 "1883"
158 ]
159 },
160 "execution_count": 7,
161 "metadata": {},
162 "output_type": "execute_result"
163 }
164 ],
165 "source": [
166 "open(plaintext_a_filename, 'w').write(pa)"
167 ]
168 },
169 {
170 "cell_type": "code",
171 "execution_count": 11,
172 "metadata": {
173 "Collapsed": "false"
174 },
175 "outputs": [],
176 "source": [
177 "clue_dict = ['alerting', 'altering', 'integral', 'relating', 'triangle', 'angriest', 'gantries', 'granites', 'ingrates', 'rangiest', 'tangiers', \n",
178 " 'dniester', 'inserted', 'nerdiest', 'resident', 'trendies', 'respects', 'scepters', 'sceptres', 'specters', 'spectres', 'restrain', \n",
179 " 'retrains', 'strainer', 'terrains', 'trainers']\n",
180 "clue_trans = [transpositions_of(w) for w in clue_dict]"
181 ]
182 },
183 {
184 "cell_type": "code",
185 "execution_count": 13,
186 "metadata": {
187 "Collapsed": "false"
188 },
189 "outputs": [
190 {
191 "name": "stdout",
192 "output_type": "stream",
193 "text": [
194 "(1, 2, 3, 5, 0, 4) True False \n",
195 "\n",
196 "REERNLNHNRTAANNBSCROIAUSHOALISICTETSIEAARASTSOSLNOIOOWESEIOGMHISTXDIEGEAGATTADRVSICAYSITREARESOFNCFFWPNSPPTNTAELHYBNRRAMNSMSXOENGPNINFAFOGDLOMENOORHNTNERMTATFTWATFAESOGRYNCSHSENTEDAOTRKIBPPARHCERRTTHNNTGIREITMVHEDCONTCTUWPDOIROENHEGIKIATNCWLNODRSSNTEOGAFREBTAEHOLWHOSSITPTRNCUNLECLOUTDSTHNTSLLHTRTGNDCRERSESALLBEOCOAUEOEOUAEHIDVADSAHETISIVTGLNNESTTPCIWUFRAETIRHIIWFLEREYTEEOKHYRFOTIRERNERTSFIILFCINYTSALGAHHSDIVSLLSUTWSTELNPOOSISOSHAYNEGNLOGTORDTHTRRORRSDISUEOEIALSTKGASPUNBNLUMYIIBSIELHEEAIGIRISSCRCVNATGTYIOIAIUTTEANNNURSCMMHEEAPEOIRIRTESOODTHILWCESTTYSRALHUROOSAAGRRIOEIAHHFSOYTRMATSEAUSNKVVSWTLSNTDONOENFTYMRHTCNETHLDEWWHSPHNSTALNIIEEETTAFTASSFHASRAEOSEPUEOXAOOHHTTTNIEENYAIOYATVIEIISEOLIYGEIEIROTPUTASARUETHONOIPNIHUUTOEHIUNTUWISTNRYNSREAOUNCTEMFEIFGEISNRHOCYORTRLANATASOYHIFTHHHAGMNTNMWOICTPIRAEEAIARNNLDUNYFEHRYURTOONDIONTAOOIEUWLJEOWODMHITGGRHKOPENNIEAYMMANETIRPDRBYACCRPUTOHRSRSIMOBSUMEOWPHTATORENNBHHNTAIHNALIDCOOLWOPUFODIFAEOIFELITHNBEEAEOSTNIDIERILBDOAQTEVGEUEWLUEIDSRBEDEDAETHVBOTISHTTNTMEWOSTFEEDCODTDUMEATLISLOETVOLSHIJGETTASAHEIEBIHFSERHNHREROOUDDTTTTYGNATOHAGWIVISGTACENOIRUHRNAUDAEUTPNHYPETSRCY \n",
197 "DNIMOYNARCIENTATITSUELDTDIDBOPBPSHRAREYLTVROYERSNAGEUTTBFRHTONUIEEFYHRUXGEOEEDHCTETPCTIUTUVAMLRNLTTERRRTONOOHEDUOCSRFAAOTINWENEETSNUISIEASSEURHNOUTRRSRTSTHEEERELSESYTEETIUTIOIAIARMHISEMBIECISSSREMIEOAOLNOIDPLESCYUNATMROETIOIMNUTANSHHHOHTKOUENONOIAUTHRHSNRTATEEPPCFRMRSAGASOSEESE\n"
198 ]
199 }
200 ],
201 "source": [
202 "(trans_b, fill_b, empty_b), score_b = column_transposition_break(scb, translist=clue_trans, fitness=Ptrigrams)\n",
203 "print(trans_b, fill_b, empty_b, '\\n')\n",
204 "pb = column_transposition_decipher(cb, trans_b, \n",
205 " fillcolumnwise=fill_b,\n",
206 " emptycolumnwise=empty_b)\n",
207 "print(pb)"
208 ]
209 },
210 {
211 "cell_type": "code",
212 "execution_count": 22,
213 "metadata": {
214 "Collapsed": "false"
215 },
216 "outputs": [
217 {
218 "name": "stdout",
219 "output_type": "stream",
220 "text": [
221 "(1, 2, 4, 0, 3) False False \n",
222 "\n"
223 ]
224 }
225 ],
226 "source": [
227 "(trans_b, fill_b, empty_b), score_b = column_transposition_break(scb, fitness=Ptrigrams)\n",
228 "print(trans_b, fill_b, empty_b, '\\n')"
229 ]
230 },
231 {
232 "cell_type": "code",
233 "execution_count": 19,
234 "metadata": {
235 "Collapsed": "false"
236 },
237 "outputs": [
238 {
239 "name": "stdout",
240 "output_type": "stream",
241 "text": [
242 "dear uncle wilhelm our journey to london was short and charming the trains rattling along the\n",
243 "british tracks through open fields and countryside their roads maybe almost impassable in places but\n",
244 "these iron tracks provide a very usable way to travel across country at speed our hosts in london\n",
245 "have organised for us to stay with members of the british scouting movement though i will spend the\n",
246 "next few days with the orts gruppe exchanging news about the fatherland and their activities herein\n",
247 "the capital city it was difficult to tear ourselves away from the pleasures of norfolk and the city\n",
248 "offers far fewer opportunities for open exploration on the other hand the bustle of the city as in\n",
249 "berlin offers a certain anonymity and it is in some ways relaxing to leave behind the gossip and\n",
250 "inquisitiveness of village life as you suggested we will use our time herein london to support our\n",
251 "brothers and sisters and to reassure them that they have not been forgotten i will pass on the gifts\n",
252 "that you entrusted to me together with your instructions for their use as we did in caistor st\n",
253 "edmund the radio you sent me is remarkable it is able to pickup signals across the spectrum even\n",
254 "from berlin itself though i am enjoying listening to british radio especially the home service they\n",
255 "are building a chain of transmitters across the country which provide information throughout the day\n",
256 "and night something that i think you might want to take into account when planning our own radio\n",
257 "services i am assuming that there are no changes planned for our itinerary but if there are then\n",
258 "perhaps you could forward them to our hosts in spalding with a copy sent post restante to cse yours\n",
259 "sincerely\n"
260 ]
261 }
262 ],
263 "source": [
264 "pb = prettify(column_transposition_decipher(scb, trans_b, \n",
265 " fillcolumnwise=fill_b,\n",
266 " emptycolumnwise=empty_b))\n",
267 "print(pb)"
268 ]
269 },
270 {
271 "cell_type": "code",
272 "execution_count": 20,
273 "metadata": {
274 "Collapsed": "false"
275 },
276 "outputs": [
277 {
278 "data": {
279 "text/plain": [
280 "['fable',\n",
281 " 'gable',\n",
282 " 'nacre',\n",
283 " 'nahum',\n",
284 " 'oakum',\n",
285 " 'odium',\n",
286 " 'padre',\n",
287 " 'pinto',\n",
288 " 'salvo',\n",
289 " 'santo',\n",
290 " 'scour',\n",
291 " 'tabus',\n",
292 " 'tagus',\n",
293 " 'talus',\n",
294 " 'thous',\n",
295 " 'timur',\n",
296 " 'torus',\n",
297 " 'torys',\n",
298 " 'whizs',\n",
299 " 'gabble',\n",
300 " 'kabuki',\n",
301 " 'lactic',\n",
302 " 'ladoga',\n",
303 " 'leftie',\n",
304 " 'madrid',\n",
305 " 'megohm',\n",
306 " 'oberon',\n",
307 " 'raisin',\n",
308 " 'refuel',\n",
309 " 'refuge',\n",
310 " 'salvos',\n",
311 " 'santos',\n",
312 " 'scours',\n",
313 " 'tabbys',\n",
314 " 'tabula',\n",
315 " 'taffys',\n",
316 " 'taguss',\n",
317 " 'taiwan',\n",
318 " 'tallys',\n",
319 " 'tammys',\n",
320 " 'tanyas',\n",
321 " 'telexs',\n",
322 " 'tenure',\n",
323 " 'terrys',\n",
324 " 'tethys',\n",
325 " 'thrush',\n",
326 " 'thrust',\n",
327 " 'timmys',\n",
328 " 'toruss',\n",
329 " 'velezs',\n",
330 " 'vilyui',\n",
331 " 'weepys',\n",
332 " 'whizzs',\n",
333 " 'willys',\n",
334 " 'worrys',\n",
335 " 'lactate',\n",
336 " 'macrame',\n",
337 " 'palazzo',\n",
338 " 'raisins',\n",
339 " 'raritan',\n",
340 " 'refresh',\n",
341 " 'refugee',\n",
342 " 'sabbath',\n",
343 " 'salazar',\n",
344 " 'sinuous',\n",
345 " 'tactual',\n",
346 " 'tarawas',\n",
347 " 'telexes',\n",
348 " 'tethyss',\n",
349 " 'thrushs',\n",
350 " 'thrusts',\n",
351 " 'maharaja',\n",
352 " 'nakayama',\n",
353 " 'ramayana',\n",
354 " 'sabbaths',\n",
355 " 'salazars',\n",
356 " 'semester',\n",
357 " 'sensuous',\n",
358 " 'teletype',\n",
359 " 'tortuous',\n",
360 " 'maharajah',\n",
361 " 'refresher',\n",
362 " 'refreshes',\n",
363 " 'semesters',\n",
364 " 'refreshers',\n",
365 " 'sensuousness',\n",
366 " 'sensuousnesss']"
367 ]
368 },
369 "execution_count": 20,
370 "metadata": {},
371 "output_type": "execute_result"
372 }
373 ],
374 "source": [
375 "transpositions[trans_b]"
376 ]
377 },
378 {
379 "cell_type": "code",
380 "execution_count": 21,
381 "metadata": {
382 "Collapsed": "false"
383 },
384 "outputs": [
385 {
386 "data": {
387 "text/plain": [
388 "1685"
389 ]
390 },
391 "execution_count": 21,
392 "metadata": {},
393 "output_type": "execute_result"
394 }
395 ],
396 "source": [
397 "open(plaintext_b_filename, 'w').write(pb)"
398 ]
399 },
400 {
401 "cell_type": "code",
402 "execution_count": null,
403 "metadata": {
404 "Collapsed": "false"
405 },
406 "outputs": [],
407 "source": []
408 }
409 ],
410 "metadata": {
411 "jupytext": {
412 "formats": "ipynb,md"
413 },
414 "kernelspec": {
415 "display_name": "Python 3",
416 "language": "python",
417 "name": "python3"
418 },
419 "language_info": {
420 "codemirror_mode": {
421 "name": "ipython",
422 "version": 3
423 },
424 "file_extension": ".py",
425 "mimetype": "text/x-python",
426 "name": "python",
427 "nbconvert_exporter": "python",
428 "pygments_lexer": "ipython3",
429 "version": "3.7.4"
430 }
431 },
432 "nbformat": 4,
433 "nbformat_minor": 4
434 }