2021 challenge 1
[cipher-challenge.git] / 2020 / 2020-challenge3.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {
7 "Collapsed": "false"
8 },
9 "outputs": [],
10 "source": [
11 "from szyfrow.affine import *\n",
12 "from szyfrow.keyword_cipher import *"
13 ]
14 },
15 {
16 "cell_type": "code",
17 "execution_count": 2,
18 "metadata": {
19 "Collapsed": "false"
20 },
21 "outputs": [],
22 "source": [
23 "challenge_number = 3\n",
24 "plaintext_a_filename = f'plaintext.{challenge_number}a.txt'\n",
25 "plaintext_b_filename = f'plaintext.{challenge_number}b.txt'\n",
26 "ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'\n",
27 "ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'"
28 ]
29 },
30 {
31 "cell_type": "code",
32 "execution_count": 5,
33 "metadata": {
34 "Collapsed": "false"
35 },
36 "outputs": [],
37 "source": [
38 "ca = open(ciphertext_a_filename).read()\n",
39 "sca = sanitise(ca)\n",
40 "cb = open(ciphertext_b_filename).read()\n",
41 "scb = sanitise(cb)"
42 ]
43 },
44 {
45 "cell_type": "code",
46 "execution_count": 8,
47 "metadata": {
48 "Collapsed": "false"
49 },
50 "outputs": [
51 {
52 "name": "stdout",
53 "output_type": "stream",
54 "text": [
55 "vern KeywordWrapAlphabet.from_last \n",
56 "\n",
57 "thanks to everyone for their work on the intercept, it looks really important and i have discussed the contents with harry. our sources in germany have confirmed that the nca magazine carried the instructions issued by ih and nr, and vk wrote to the chief constables of local police forces requesting them to keep an eye out for the touring groups and to report on any matters of interest. special branch are coordinating intelligence activities and forwarded a report from the local constabulary in norfolk detailing activity by one of the touring cycling parties who were seen taking photographs and sketching the landscape at caistor st edmund. the local constable questioned them, but they were not held as there were no grounds for an arrest. they had arrived at harwich and were apparently touring norfolk at the invitation of a local scout group before heading to london.\n",
58 "while there are significant defence installations in that area, caistor st edmund seems an unlikely target for german intelligence so perhaps this is innocent, but the evidence we have so far suggests that that is unlikely. another possibility is that it is a red herring operation disguising the activities of other groups, but just in case i will make enquiries with our sister agencies to see if there is anything we should know about in that area.\n",
59 "i asked our agents in norfolk to report on unusual activity and sent them a list of keywords that should trigger investigation. they forwarded the attached message addressed to tirpitzufer, which was left at the caistor st edmund post office by the local scoutmaster. it was in a bulky packet containing a number of sketches of the area together with an encrypted message which we need you to decipher quickly. analysis of other communication intercepts have suggested that this message may also contain a key to future communications across the spyclist network so you should spend some time thinking about it once you have submitted the decrypt.\n",
60 "while the mission of the spyclists is spelled out in detail in the last decrypt, it is still not clear what the larger aims of the abwehr mission might be, but your research into wilhelm and nikolaus has probably led you to the same conclusion as me: this is a high-level mission and is likely to pose a significant threat to our security. it follows that we should increase our own security to prevent nazi code breakers from reading our messages, so i will lengthen the keyword in my next mission briefing. i will also review our communication protocols for subsequent messages with harry and will update you on the standards to use in mission briefing 4.\n",
61 "the report from norfolk gave me an idea about how we could gather intelligence on the spyclist network. harry and i discussed this with vk since it carries a number of risks, but vk approved the strategy which we have given the codename the akela initiative. more to follow.\n",
62 "pearl\n",
63 "\n"
64 ]
65 }
66 ],
67 "source": [
68 "(word_a, wrap_a), score_a = keyword_break_mp(sca, fitness=Ptrigrams)\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": 9,
77 "metadata": {
78 "Collapsed": "false"
79 },
80 "outputs": [
81 {
82 "data": {
83 "text/plain": [
84 "2918"
85 ]
86 },
87 "execution_count": 9,
88 "metadata": {},
89 "output_type": "execute_result"
90 }
91 ],
92 "source": [
93 "open(plaintext_a_filename, 'w').write(pa)"
94 ]
95 },
96 {
97 "cell_type": "code",
98 "execution_count": 10,
99 "metadata": {
100 "Collapsed": "false"
101 },
102 "outputs": [
103 {
104 "name": "stdout",
105 "output_type": "stream",
106 "text": [
107 "croatias KeywordWrapAlphabet.from_last \n",
108 "\n",
109 "gear unfle wilhelm,\n",
110 "i trust that you ang the camily at tirpitzucer are all well ang that you enjoyeg the sketfhes i sent you last week. i have enfloseg my most refent impressions oc the deauticul, ic somewhat stark langsfape in norcolk. i hope ang delieve that you will cing them oc breat interest. the clat ang open cielgs have a spartan deauty punftuateg dy just a cew langmarks oc gistinftion, dut their isolation makes them stang out even more ang infreases their interest.\n",
111 "our hosts have deen most abreeadle ang even the lofal polife corfe have deen as affommogatinb as you subbesteg they woulg de. the lofal fonstadle seemeg most interesteg in our photobraphif hoddy, dut he appeareg to de fontent with the explanation oc our interest in their deauticul fountrysige ang villabes. he was also very apprefiative oc our roubh sketfhes oc their strikinb langsfape, thoubh he gig seem surpriseg at the numder oc grawinbs we have mage oc the cielgs aroung faistor st egmung, subbestinb that its sibnicifanfe is not wigely known.\n",
112 "the lofal sfoutmaster abreeg to pass this messabe to you ang has also arranbeg cor our corwarg travel. we will heag next to longon to meet with my fousin nikolaus decore preparinb cor the sefong stabe oc our little agventure. ic you have any curther requests cor incormation or aggitional grawinbs in this extraorginary part oc the worlg then please go make sure to let me know decore we gepart.\n",
113 "let me cinish with a little puzzle cor you. fan you cing an eibht letter enblish worg whifh you fan fhanbe dy alterinb the orger oc its letters to progufe cour other giccerent enblish worgs?\n",
114 "yours sinferely,\n",
115 "otto\n",
116 "\n"
117 ]
118 }
119 ],
120 "source": [
121 "(word_b, wrap_b), score_b = keyword_break_mp(scb, fitness=Ptrigrams)\n",
122 "print(word_b, wrap_b, '\\n')\n",
123 "pb = keyword_decipher(cb, word_b, wrap_b)\n",
124 "print(pb)"
125 ]
126 },
127 {
128 "cell_type": "code",
129 "execution_count": 11,
130 "metadata": {
131 "Collapsed": "false"
132 },
133 "outputs": [
134 {
135 "name": "stdout",
136 "output_type": "stream",
137 "text": [
138 "caistoruvwxyzbdefghjklmnpq \n",
139 "\n",
140 "dear uncle wilhelm,\n",
141 "i trust that you and the family at tirpitzufer are all well and that you enjoyed the sketches i sent you last week. i have enclosed my most recent impressions of the beautiful, if somewhat stark landscape in norfolk. i hope and believe that you will find them of great interest. the flat and open fields have a spartan beauty punctuated by just a few landmarks of distinction, but their isolation makes them stand out even more and increases their interest.\n",
142 "our hosts have been most agreeable and even the local police force have been as accommodating as you suggested they would be. the local constable seemed most interested in our photographic hobby, but he appeared to be content with the explanation of our interest in their beautiful countryside and villages. he was also very appreciative of our rough sketches of their striking landscape, though he did seem surprised at the number of drawings we have made of the fields around caistor st edmund, suggesting that its significance is not widely known.\n",
143 "the local scoutmaster agreed to pass this message to you and has also arranged for our forward travel. we will head next to london to meet with my cousin nikolaus before preparing for the second stage of our little adventure. if you have any further requests for information or additional drawings in this extraordinary part of the world then please do make sure to let me know before we depart.\n",
144 "let me finish with a little puzzle for you. can you find an eight letter english word which you can change by altering the order of its letters to produce four other different english words?\n",
145 "yours sincerely,\n",
146 "otto\n",
147 "\n"
148 ]
149 }
150 ],
151 "source": [
152 "word_b, score_b = simulated_annealing_break(scb, fitness=Ptrigrams)\n",
153 "print(word_b, '\\n')\n",
154 "pb = keyword_decipher(cb, word_b)\n",
155 "print(pb)"
156 ]
157 },
158 {
159 "cell_type": "code",
160 "execution_count": 12,
161 "metadata": {
162 "Collapsed": "false"
163 },
164 "outputs": [
165 {
166 "data": {
167 "text/plain": [
168 "'dear uncle wilhelm,\\ni trust that you and the family at tirpitzufer are all well and that you enjoyed the sketches i sent you last week. i have enclosed my most recent impressions of the beautiful, if somewhat stark landscape in norfolk. i hope and believe that you will find them of great interest. the flat and open fields have a spartan beauty punctuated by just a few landmarks of distinction, but their isolation makes them stand out even more and increases their interest.\\nour hosts have been most agreeable and even the local police force have been as accommodating as you suggested they would be. the local constable seemed most interested in our photographic hobby, but he appeared to be content with the explanation of our interest in their beautiful countryside and villages. he was also very appreciative of our rough sketches of their striking landscape, though he did seem surprised at the number of drawings we have made of the fields around caistor st edmund, suggesting that its significance is not widely known.\\nthe local scoutmaster agreed to pass this message to you and has also arranged for our forward travel. we will head next to london to meet with my cousin nikolaus before preparing for the second stage of our little adventure. if you have any further requests for information or additional drawings in this extraordinary part of the world then please do make sure to let me know before we depart.\\nlet me finish with a little puzzle for you. can you find an eight letter english word which you can change by altering the order of its letters to produce four other different english words?\\nyours sincerely,\\notto\\n'"
169 ]
170 },
171 "execution_count": 12,
172 "metadata": {},
173 "output_type": "execute_result"
174 }
175 ],
176 "source": [
177 "keyword_decipher(cb, 'caistor', KeywordWrapAlphabet.from_last)"
178 ]
179 },
180 {
181 "cell_type": "code",
182 "execution_count": 13,
183 "metadata": {
184 "Collapsed": "false"
185 },
186 "outputs": [
187 {
188 "data": {
189 "text/plain": [
190 "1638"
191 ]
192 },
193 "execution_count": 13,
194 "metadata": {},
195 "output_type": "execute_result"
196 }
197 ],
198 "source": [
199 "open(plaintext_b_filename, 'w').write(pb)"
200 ]
201 },
202 {
203 "cell_type": "code",
204 "execution_count": null,
205 "metadata": {
206 "Collapsed": "false"
207 },
208 "outputs": [],
209 "source": []
210 }
211 ],
212 "metadata": {
213 "jupytext": {
214 "formats": "ipynb,md"
215 },
216 "kernelspec": {
217 "display_name": "Python 3",
218 "language": "python",
219 "name": "python3"
220 },
221 "language_info": {
222 "codemirror_mode": {
223 "name": "ipython",
224 "version": 3
225 },
226 "file_extension": ".py",
227 "mimetype": "text/x-python",
228 "name": "python",
229 "nbconvert_exporter": "python",
230 "pygments_lexer": "ipython3",
231 "version": "3.7.4"
232 }
233 },
234 "nbformat": 4,
235 "nbformat_minor": 4
236 }