Done challenge 9
[cipher-challenge.git] / 2021 / 2021-challenge4.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.support.text_prettify import *"
16 ]
17 },
18 {
19 "cell_type": "code",
20 "execution_count": 2,
21 "id": "d9dd1b5e",
22 "metadata": {
23 "Collapsed": "false"
24 },
25 "outputs": [],
26 "source": [
27 "challenge_number = 4\n",
28 "plaintext_a_filename = f'plaintext.{challenge_number}a.txt'\n",
29 "plaintext_b_filename = f'plaintext.{challenge_number}b.txt'\n",
30 "ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'\n",
31 "ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'"
32 ]
33 },
34 {
35 "cell_type": "code",
36 "execution_count": 6,
37 "id": "0f1f792a",
38 "metadata": {
39 "Collapsed": "false"
40 },
41 "outputs": [],
42 "source": [
43 "ca = open(ciphertext_a_filename).read()\n",
44 "cb = open(ciphertext_b_filename).read()\n",
45 "\n",
46 "sca = sanitise(ca)\n",
47 "scb = sanitise(cb)"
48 ]
49 },
50 {
51 "cell_type": "code",
52 "execution_count": 7,
53 "id": "c3cccd86-5e40-4040-8f91-9ca53ac6e909",
54 "metadata": {},
55 "outputs": [
56 {
57 "name": "stdout",
58 "output_type": "stream",
59 "text": [
60 "archive KeywordWrapAlphabet.from_last \n",
61 "\n",
62 "harry iamba ckisp entmo stoft helas ttwow eeksi nthev aultt rying tounp ackso meoft hemys terys urrou nding theli ghtho useco nspir acycr ewand ithin kimad esome progr esswh ileiw asawa ythet raine estac kledt helet terfr omabc andth eydid agrea tjobt heyar eshap ingup welli think wewil lbegl adoft heirh elpas thisc asepr ogres sesth elibr arian atthe vault wasve ryhel pfula ndwhe nitol dherw hatiw asloo kingf orshe point edmet oanar chive ofvic toria npape rscon cerni ngsci entis tseng ineer sands ocial refor merst hatha dreco rdede arlyb ackgr oundi nvest igati onsin topeo pleof inter estis aypeo plebu tidid notic ethat there wasah eavyb iasto inves tigat ingwo menba ckthe niwas surpr iseda tfirs tihad assum edtha tthey would haveb eenla rgely ignor edbut isupp oseth esere marka blewo menma dethe irpre sence known andpe rhaps thees tabli shmen tfelt threa tened inmos tcase sther earen ostan dards ecuri tyfil esfor these women butwh ereth erewe rethe ywere atbes tthin there allyi ntere sting mater ialwa sinth ehidd enarc hivew herei found repor tsand paper sabou tlove lacen ighti ngale andab cases tabli shmen tfigu resth emsel vesth eysee mtoha vebee nigno redby these crets ervic eandc learl ynotc onsid ereda secur ityth reatb utour bossf orebe arsdi dcarr youti nvest igati onsin tothe iract iviti esand there arecl earsi gnsth atthe ymayh avewo rkedt ogeth erobv iousl ythef actth atthe ywere makin gsuch animp acton socie tywas enoug htoma kesom epeop lener vousa ftera llitw asati meofg reatc hange andco ncern sabou trevo lutio nwere veryr ealon eofth enote sonfi lesug gests thatt hegro wingc ampai gnfor women ssuff ragea ndsoc ialac tivis mmigh thave beent hesta rtofa ninsu rrect ionan ditna medse veral ofour prota gonis tsbut itdid ntsay howth eymig hthav einte ndedt otrig gerth eupri singw hatwe haves eenso faris thatw hatev erthe light house consp iracy wereu ptoth eywen ttoex traor dinar yleng thsto keepi tsecr etand thefa cttha twear eonly nowun cover ingit sugge ststh atitw asmuc hsubt lerth anapl anned upris ingia mstil lstru gglin gwith theid eatha tpeop lewho werep artof thebi rthof bossc ouldh avebe enact ingse cretl yagai nstus butth atisc ertai nlyim plied bysom eofth efile sweha vecra ckeda ndthe attac heddo cumen thasa ddedt omywo rries itwas writt enint heaft ermat hofth ehind enbur gdisa sterb ysome oneit hinky oukne wwell sheha dafan tasti creco rdwor kingd eepun derco veran drepo rting backt owest ernin telli gence agenc iesbu tthet hingt hatca ughtm yeyew astha there sheus edthe coden ameau gusta idont knowi fyoua lread yspot tedit butth elett ering aroun dthee dgeof theli ghtho usest ampre adsad aaddd aaada aadad andsu bstit uting theaa nddfo rdota nddas hspel lsout herco denam eitho ughta tfirs tthat imigh tbere ading toomu chint oitth elett erinh erfil eseem sneve rtoha vebee nbrok ensom eonew roted ouble encry ptedo nthee nvelo peand igues sthat wasen ought odiss uadet heage ncies froms pendi ngmuc htime onitw ithhi ndsig htiti spret tyobv ioust hatac tuall yitha sjust beene ncryp tedon ceand thene ncode dasmo rseco deusi ngthe samec onven tiona equal sdotd equal sdash oncet hatha dbeen untan gledi found itpre ttyea sytob reaka ndiam veryw orrie dabou twhat ifoun dithi nkyou needt otake alook jodie\n",
63 "\n"
64 ]
65 }
66 ],
67 "source": [
68 "(word_a, wrap_a), score_a = keyword_break(sca)\n",
69 "print(word_a, wrap_a, '\\n')\n",
70 "pa = keyword_decipher(ca, word_a, wrap_a)\n",
71 "print(pa)"
72 ]
73 },
74 {
75 "cell_type": "code",
76 "execution_count": 8,
77 "id": "c7f2e770-8062-4d18-b90a-cb2e410ef88c",
78 "metadata": {},
79 "outputs": [
80 {
81 "name": "stdout",
82 "output_type": "stream",
83 "text": [
84 "harry i am back i spent most of the last two weeks in the vault trying to unpack some of the mystery\n",
85 "surrounding the lighthouse conspiracy crew and i think i made some progress while i was away the\n",
86 "trainees tackled the letter from abc and they did a great job they are shaping up well i think we\n",
87 "will be glad of their help as this case progresses the librarian at the vault was very helpful and\n",
88 "when it old her what i was looking for she pointed me to an archive of victorian papers concerning\n",
89 "scientists engineers and social reformers that had recorded early background investigations into\n",
90 "people of interest i say people but i did notice that there was a heavy bias to investigating women\n",
91 "back then i was surprised at first i had assumed that they would have been largely ignored but i\n",
92 "suppose these remarkable women made their presence known and perhaps the establishment felt\n",
93 "threatened in most cases there are no standard security files for these women but where there were\n",
94 "they were at best thin the really interesting material was in the hidden archive where i found\n",
95 "reports and papers about lovelace nightingale and abc as establishment figures themselves they seem\n",
96 "to have been ignored by the secret service and clearly not considered a security threat but our boss\n",
97 "forebears did carryout investigations into their activities and there are clear signs that they may\n",
98 "have worked together obviously the fact that they were making such an impact on society was enough\n",
99 "to make some people nervous after all it was a time of great change and concerns about revolution\n",
100 "were very real one of the notes on file suggests that the growing campaign for womens suffrage and\n",
101 "social activism might have been the start of an insurrection and it named several of our\n",
102 "protagonists but it didnt say how they might have intended to trigger the uprising what we have seen\n",
103 "so far is that whatever the lighthouse conspiracy were up to they went to extraordinary lengths to\n",
104 "keep it secret and the fact that we are only now uncovering it suggests that it was much subtler\n",
105 "than a planned uprising i am still struggling with the idea that people who were part of the birth\n",
106 "of boss could have been acting secretly against us but that is certainly implied by some of the\n",
107 "files we have cracked and the attached document has added to my worries it was written in the\n",
108 "aftermath of the hindenburg disaster by someone i think you knew well she had a fantastic record\n",
109 "working deep undercover and reporting back to western intelligence agencies but the thing that\n",
110 "caught my eye was that here she used the codename augusta i dont know if you already spotted it but\n",
111 "the lettering around the edge of the lighthouse stamp reads ada add da a ada a a dad and\n",
112 "substituting the a and d for dot and dash spells out her codename i thought at first that i might be\n",
113 "reading too much into it the letter in her file seems never to have been broken someone wrote double\n",
114 "encrypted on the envelope and i guess that was enough to dissuade the agencies from spending much\n",
115 "time on it with hindsight it is pretty obvious that actually it has just been encrypted once and\n",
116 "then encoded as morsecode using the same convention a equals dotd equals dash once that had been\n",
117 "untangled i found it pretty easy to break and i am very worried about what i found i think you need\n",
118 "to take a look jodie\n"
119 ]
120 }
121 ],
122 "source": [
123 "pa = keyword_decipher(sca, word_a, wrap_a)\n",
124 "print(prettify(pa))"
125 ]
126 },
127 {
128 "cell_type": "code",
129 "execution_count": 9,
130 "id": "6cecddd9",
131 "metadata": {
132 "Collapsed": "false"
133 },
134 "outputs": [
135 {
136 "data": {
137 "text/plain": [
138 "3341"
139 ]
140 },
141 "execution_count": 9,
142 "metadata": {},
143 "output_type": "execute_result"
144 }
145 ],
146 "source": [
147 "open(plaintext_a_filename, 'w').write(prettify(pa))"
148 ]
149 },
150 {
151 "cell_type": "code",
152 "execution_count": 11,
153 "id": "77e0eb5c-226d-4197-a687-4a5d725c6af0",
154 "metadata": {},
155 "outputs": [],
156 "source": [
157 "morse_chars = 'a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9'.split()\n",
158 "# morse_chars"
159 ]
160 },
161 {
162 "cell_type": "code",
163 "execution_count": 12,
164 "id": "0c7912fb-f552-4933-9730-3784197fc729",
165 "metadata": {},
166 "outputs": [],
167 "source": [
168 "morse_codes = '.- / -... / -.-. / -.. / . / ..-. / --. / .... / .. / .--- / -.- / .-.. / -- / -. / --- / .--. / --.- / .-. / ... / - / ..- / ...- / .-- / -..- / -.-- / --.. / ----- / .---- / ..--- / ...-- / ....- / ..... / -.... / --... / ---.. / ----.'.split(' / ')"
169 ]
170 },
171 {
172 "cell_type": "code",
173 "execution_count": 14,
174 "id": "ae62967b-0c5d-4e14-adf2-8cad03ed6970",
175 "metadata": {},
176 "outputs": [
177 {
178 "data": {
179 "text/plain": [
180 "['.-', '-...', '-.-.']"
181 ]
182 },
183 "execution_count": 14,
184 "metadata": {},
185 "output_type": "execute_result"
186 }
187 ],
188 "source": [
189 "char_to_morse = {l: c for l, c in zip(morse_chars, morse_codes)}\n",
190 "morse_to_char = {c: l for l, c in zip(morse_chars, morse_codes)}\n",
191 "[char_to_morse[l] for l in 'abc']"
192 ]
193 },
194 {
195 "cell_type": "code",
196 "execution_count": 25,
197 "id": "d5c24bcb-fda8-40f0-89ba-166380046fc9",
198 "metadata": {},
199 "outputs": [
200 {
201 "data": {
202 "text/plain": [
203 "'otjsjseifzotyijsfeajzmoafzkfacjtntjtegsaneyeceggaftwqifzoytygcaweayhczxbweptjqayhotjsjsajjsefetgaifzotyiftgvjsajzkfxtggtzyotwwlekycznefehlqzyezmjsebafjtegjzjseczymwtcjtsaneleeyalwejzhftbmeehtyjewwtieycejzjseoegjefyawwtegntajsetfaieyjsaffqayhagmafagtcayjewwsefexatygtiyzfayjzmzkfjfkebkfbzgeyzyejseweggjseftgvfexatygstiszmzyezflzjsgthegacdktftyijsegecfejjzajzxtcoeabzygeafwqayhoeotwwyeehjzacjmagjtmoeafejzbfeneyjjstgjsewzggzmjsestyheylkfibfegeyjehamalkwzkgzbbzfjkytjqayhtkgehtjjzxavegkfejsajjseawwtegkyhefgjzzhosajjseyartgcteyjtgjgoefeozfvtyizytsanejftehyzjjzbfznthesaffqotjsjzzxkcstymzfxajtzyajjstggjaiegtycetjtgcfkctawjzzkfxtggtzyjsajoexatyjatyjselawaycezmkycefjatyjqtjtgyzjajawwcweafoszxtisjotyjseykcweafafxgfacelkjzkfepbefteycezmjsexzyjsgayhqeafgweahtyikbjzjseifeajoafgkiiegjgjsajjsefaceotwwsanetjgzoyxzxeyjkxayhtjotwwjaveczygthefalweemmzfjlqawwzmkgtmoeszbejzhtfecjtjjzzkfahnayjaieakikgja'"
204 ]
205 },
206 "execution_count": 25,
207 "metadata": {},
208 "output_type": "execute_result"
209 }
210 ],
211 "source": [
212 "morse_table = ''.maketrans('AD', '.-')\n",
213 "cb_dd = cb.translate(morse_table)\n",
214 "cb_words = cb_dd.split(' / ')\n",
215 "cb_letters = cat(cat(morse_to_char[l] for l in w.split()) for w in cb_words)\n",
216 "cb_letters"
217 ]
218 },
219 {
220 "cell_type": "code",
221 "execution_count": 27,
222 "id": "86085f32-1564-4f01-8d58-2786625a743f",
223 "metadata": {},
224 "outputs": [
225 {
226 "name": "stdout",
227 "output_type": "stream",
228 "text": [
229 "alchemist KeywordWrapAlphabet.from_last \n",
230 "\n",
231 "with the growing threat of war our activities have necessarily grown in scale and complexity and\n",
232 "with that there is a growing risk that our mission will be uncovered by one of the parties to the\n",
233 "conflict i have be enable to drip feed intelligence to the western allies via their agent harry and\n",
234 "as far as i can tell he remains ignorant of our true purpose nonetheless the risk remains high of\n",
235 "one or both sides acquiring the secret to atomic weapons early and we will need to act fast if we\n",
236 "are to prevent this the loss of the hindenburg presented a fabulous opportunity and i used it to\n",
237 "make sure that the allies understood what the nazi scientists were working on i have tried not to\n",
238 "provide harry with too much information at this stage since it is crucial to our mission that we\n",
239 "maintain the balance of uncertainty it is not at all clear who might win the nuclear arms race but\n",
240 "our experience of the months and years leading up to the great war suggests that the race will have\n",
241 "its own momentum and it will take considerable effort by all of us if we hope to direct it to our\n",
242 "advantage augusta\n"
243 ]
244 }
245 ],
246 "source": [
247 "(word_b, wrap_b), score_a = keyword_break(cb_letters, fitness=Ptrigrams)\n",
248 "print(word_b, wrap_b, '\\n')\n",
249 "pb = keyword_decipher(cb_letters, word_b, wrap_b)\n",
250 "print(prettify(pb))"
251 ]
252 },
253 {
254 "cell_type": "code",
255 "execution_count": 28,
256 "id": "d12a663c",
257 "metadata": {
258 "Collapsed": "false"
259 },
260 "outputs": [
261 {
262 "data": {
263 "text/plain": [
264 "1097"
265 ]
266 },
267 "execution_count": 28,
268 "metadata": {},
269 "output_type": "execute_result"
270 }
271 ],
272 "source": [
273 "open(plaintext_b_filename, 'w').write(prettify(pb))"
274 ]
275 },
276 {
277 "cell_type": "code",
278 "execution_count": null,
279 "id": "504ec2e2",
280 "metadata": {
281 "Collapsed": "false"
282 },
283 "outputs": [],
284 "source": []
285 }
286 ],
287 "metadata": {
288 "jupytext": {
289 "formats": "ipynb,md"
290 },
291 "kernelspec": {
292 "display_name": "Python 3 (ipykernel)",
293 "language": "python",
294 "name": "python3"
295 },
296 "language_info": {
297 "codemirror_mode": {
298 "name": "ipython",
299 "version": 3
300 },
301 "file_extension": ".py",
302 "mimetype": "text/x-python",
303 "name": "python",
304 "nbconvert_exporter": "python",
305 "pygments_lexer": "ipython3",
306 "version": "3.8.8"
307 }
308 },
309 "nbformat": 4,
310 "nbformat_minor": 5
311 }