Done challenge 4
[cipher-challenge.git] / 2021 / 2021-challenge3.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 12,
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 = 3\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": 14,
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 "scb = sanitise(cb)"
47 ]
48 },
49 {
50 "cell_type": "code",
51 "execution_count": 8,
52 "id": "c3cccd86-5e40-4040-8f91-9ca53ac6e909",
53 "metadata": {},
54 "outputs": [
55 {
56 "name": "stdout",
57 "output_type": "stream",
58 "text": [
59 "lighthouse KeywordWrapAlphabet.from_last \n",
60 "\n",
61 "harry, thanks for your email, i forgot you had spent time here in the archaeologists and it was good to be reminded that it can lead somewhere. in this case i think the best we can hope for is that we will have a new bunch of recruits trained and ready to tackle more serious challenges, but i have to admit that i am getting more excited about this case, even if it is just a training exercise.\n",
62 "the attached memo from abc reinforces what we learned from the last one. the lighthouse conspiracy seems to be a group of influential victorians who are aiming to use their newfound technological prowess to profit from war, famine, pestilence, and any other disturbance in the force. i don’t like to jump to conclusions, but i can’t help noticing that the initials coincide with people of interest to boss. i checked faraday’s intelligence file and as you say, it was empty. it is hard to believe that a scientist of his eminence had escaped notice, so it seems more likely that the real file has either been deleted or stored somewhere more secure. perhaps you can make enquiries? b must surely be babbage. all the talk in the attached letter is about a device that sounds like a computer, and that would mean that al is ada lovelace, who died some years before this letter was written. i can’t be sure who w or n are, but my guess is that this n is the same person as fn in the previous letter. as for abc, i have a guess, but it all seems rather fantastical. al and b appear in some of the earliest boss files, and i always assumed they were on the side of the angels. could they have been double agents working for boss inside the conspiracy? given that it is their work that seems to be driving it on, it seems more likely that the reverse is true. but that means boss was riddled with domestic insurgents from the start. i don’t think this has the same urgency as your discovery of soviet agents at the heart of british intelligence, but it is still rather alarming.\n",
63 "as is the increasing sophistication of our protagonists’ communications. they are still only relying on substitution ciphers, but their head of security, w, is clearly smart enough to know that this is too weak for serious use, and i suspect that future letters will be protected by something more professional. i wonder if he will push them to think about using polyalphabetic ciphers, or if he will just introduce simple changes like blocking the ciphertext.\n",
64 "i think it might pay for me to make a trip to london to carry out some enquiries, so i may be off grid for a couple of weeks. let me know if you hear anything useful.\n",
65 "all the best,\n",
66 "jodie.\n",
67 "\n"
68 ]
69 }
70 ],
71 "source": [
72 "(word_a, wrap_a), score_a = keyword_break(ca)\n",
73 "print(word_a, wrap_a, '\\n')\n",
74 "pa = keyword_decipher(ca, word_a, wrap_a)\n",
75 "print(pa)"
76 ]
77 },
78 {
79 "cell_type": "code",
80 "execution_count": 9,
81 "id": "6cecddd9",
82 "metadata": {
83 "Collapsed": "false"
84 },
85 "outputs": [
86 {
87 "data": {
88 "text/plain": [
89 "2618"
90 ]
91 },
92 "execution_count": 9,
93 "metadata": {},
94 "output_type": "execute_result"
95 }
96 ],
97 "source": [
98 "open(plaintext_a_filename, 'w').write(pa)"
99 ]
100 },
101 {
102 "cell_type": "code",
103 "execution_count": 20,
104 "id": "86085f32-1564-4f01-8d58-2786625a743f",
105 "metadata": {},
106 "outputs": [
107 {
108 "name": "stdout",
109 "output_type": "stream",
110 "text": [
111 "baker KeywordWrapAlphabet.from_last \n",
112 "\n",
113 "my dear miss n while the outbreak of the austro prussian war has dismayed many of the chattering\n",
114 "classes i have found utter delight in the fact that we were able to forecast this development the\n",
115 "fact that we were able to do so not days or weeks but months in advance is the proof that we have\n",
116 "been seeking that our methods combined with the power of professor bs new device can deliver the\n",
117 "knowledge we need to shape our destiny the new statistical tools that you have pioneered can i\n",
118 "believe assist us in modelling further outbreaks of violence and unrest if you will allow me to mix\n",
119 "metaphors while we will not always be able to predict the onset of disease we can hope to predict\n",
120 "its course and thereby profit the device at trinity wharf is now working at full capacity and our\n",
121 "colleagues tell me that it can compute anything we ask of it given enough time and space in which to\n",
122 "record the answers of course these practical men did not think to ask how much time any particular\n",
123 "question might take before we can receive its answer but i think this is a calculation that i can\n",
124 "leave to you before she was taken from us al wrote extensive notes on the potential of the device\n",
125 "and i suspect you will find the answers there in in the current context the machine provided with\n",
126 "diplomatic intelligence by our network of agents in europe took five weeks to calculate the\n",
127 "likelihood that bismarck would launch his offensive and to use the weather data gathered by ws\n",
128 "telegraphic network to suggest when the attack would be launched we had little agency in the\n",
129 "conflict itself but we were able to advise our sympathisers in government and thereby to expand our\n",
130 "influence our intelligence analysts are now working to determine the likely progression of the\n",
131 "conflict in the hope of finding new ways to profit from it in mentioning wi realise that i forgot to\n",
132 "pass on his advice concerning security he has convinced me that our current policy of communicating\n",
133 "via substitution ciphers really is not sufficient to conceal our intentions as our network expands\n",
134 "the risk of leaks increase she is working on a new and considerably more fiendish cipher to be used\n",
135 "for our most delicate secrets and i hope to hear more about his progress on that when i meet him\n",
136 "next month maxwell will be delivering the baker ian lecture and he is hoping to attend i did wonder\n",
137 "if m would be amenable to joining our conspiracy but perhaps i should discuss that with the rest of\n",
138 "the curia i look forward to hearing your deliberations on the efficiency of the device and any\n",
139 "thoughts you might have on its development best wishes abc\n"
140 ]
141 }
142 ],
143 "source": [
144 "(word_b, wrap_b), score_a = keyword_break(scb, fitness=Ptrigrams)\n",
145 "print(word_b, wrap_b, '\\n')\n",
146 "pb = keyword_decipher(scb, word_b, wrap_b)\n",
147 "print(prettify(pb))"
148 ]
149 },
150 {
151 "cell_type": "code",
152 "execution_count": 22,
153 "id": "d12a663c",
154 "metadata": {
155 "Collapsed": "false"
156 },
157 "outputs": [
158 {
159 "data": {
160 "text/plain": [
161 "2600"
162 ]
163 },
164 "execution_count": 22,
165 "metadata": {},
166 "output_type": "execute_result"
167 }
168 ],
169 "source": [
170 "open(plaintext_b_filename, 'w').write(prettify(pb))"
171 ]
172 },
173 {
174 "cell_type": "code",
175 "execution_count": null,
176 "id": "504ec2e2",
177 "metadata": {
178 "Collapsed": "false"
179 },
180 "outputs": [],
181 "source": []
182 }
183 ],
184 "metadata": {
185 "jupytext": {
186 "formats": "ipynb,md"
187 },
188 "kernelspec": {
189 "display_name": "Python 3 (ipykernel)",
190 "language": "python",
191 "name": "python3"
192 },
193 "language_info": {
194 "codemirror_mode": {
195 "name": "ipython",
196 "version": 3
197 },
198 "file_extension": ".py",
199 "mimetype": "text/x-python",
200 "name": "python",
201 "nbconvert_exporter": "python",
202 "pygments_lexer": "ipython3",
203 "version": "3.8.8"
204 }
205 },
206 "nbformat": 4,
207 "nbformat_minor": 5
208 }