c8bb5a481608166fdd8b28869cd3227f2142720f
[cipher-tools.git] / 2016-challenge4.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {
7 "collapsed": true
8 },
9 "outputs": [],
10 "source": [
11 "import os,sys,inspect\n",
12 "currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))\n",
13 "parentdir = os.path.dirname(currentdir)\n",
14 "sys.path.insert(0,parentdir) \n",
15 "\n",
16 "import matplotlib.pyplot as plt\n",
17 "%matplotlib inline\n",
18 "\n",
19 "from cipherbreak import *\n",
20 "\n",
21 "c4a = open('4a.ciphertext').read()\n",
22 "c4b = open('4b.ciphertext').read()"
23 ]
24 },
25 {
26 "cell_type": "code",
27 "execution_count": 2,
28 "metadata": {
29 "collapsed": false
30 },
31 "outputs": [
32 {
33 "data": {
34 "text/plain": [
35 "'MWJJT WDEVM WJBXF XWCDG VBGKF JLGLM FHEKK TDEXV WLFAN LXLMX DZXMW PFAFR NDLGN DJWPF BQMTL MFTCX RMLAF XDLFJ FKLFE XDYWC FBXWW DECWJ LXDWK KMFLG BENKX DLMFM XEEFD CFKKW RFXKF DLBWK LQFFZ YWCFB XWMWE AFFDQ GJZXD RGDKX RDWBJ FVGRD XLXGD WLETD WCXSW LOXJK LXQWK HNUUB FEATL MXKWK XVGNB EDLKF FMGQX LJFBW LFELG QFWHG DXKFE RJWPX LTQWP FKANL LMFDX LGGZW BGGZW LMFJA GGZKM FBOWD EXLAF RWDLG CWZFK FDKFX LQWKO NBBGO AGGZK GDRFD FJWBJ FBWLX PXLTA BWVZM GBFKW DEDFN LJGDK LWJKY WCFBX WQWKD GLQGJ ZXDRG DRJWP XLTQW PFKAN LGDRJ WPXLW LXGDW BQWPF KXVWD KFFMG QLMFO XFBEW RFDLK QGNBE MWPFR GLVGD ONKFE XDLMF NKLMF TKGCF LXCFK NKFAG LMDWC FKOGJ LMFKW CFLMX DRANL LMFTJ FWBBT WJFEX OOFJF DLRJW PXLTQ WPFKW JFWKF XKCGB GRXVW BHMFD GCFDG DWKTG NWBJF WETOX RNJFE GNLRJ WPXLW LXGDW BQWPF KWJFF DLXJF BTEXO OFJFD LFXDK LFXDH JFEXV LFELM FCWMN DEJFE TFWJK WRGWD ELMFT QFJFE FLFVL FEATL MFBXR GLFWC BWKLT FWJLM FKXRD WBKWJ FRXPF DGOOA TLMXD RKBXZ FAXDW JTABW VZMGB FKTKL FCKWD EWJFX DVJFE XABTO WXDLL MFTFW KXBTR FLKQW CHFEA TBGVW BDGXK FXLQW KWDWC WUXDR OFWLE FLFVL XDRLM FCWDE YWCFB XWQWK HWJLG OLMWL LGWEE LGGNJ VGDON KXGDK MFQWK XDLMF LFWCJ FKHGD KXABF OGJEF KXRDX DRLMF KFWJV MWBRG JXLMC KWDEL MFDGX KFOXB LFJKN KFELG KHGLL MFKXR DWBGO WRWBW VLXVF PFDLW RWXDK LLMFD GXKFG OKFXK CGBGR XVWBW VLXPX LTKGK MFEXE ZDGQK GCFLM XDRWA GNLRJ WPXLT QWPFK LGGXK LXBBE GDLZD GQFSW VLBTM GQETD WCXSQ FJFNK XDRMF JFSHF JLXKF GJQMW LLMFH EKKTD EXVWL FWJFG JEGXO TGNVW DRFLW DTXDL FBGDL MWLXL CXRML MFBHR NXEFC TFDIN XJXFK LMFDG XKFKX WCMFW JXDRK NRRFK LLMWL HEKWJ FKFJX GNKBG LKGOV WKMBG LKGOX DOBNF DVFKG XKNRR FKLQF XDVJF WKFLM FKFVN JXLTO GJONL NJFCF KKWRF KQMFD TGNJF HBTLJ TNKXD RWEGN ABFFD VJTHL XGDJF PFJKX DRLMX DRKAF OGJFW HHBTX DRWZF TQGJE VXHMF JLGLM FCFKK WRFXQ XBBAF GNLGO VGDLW VLOGJ LMFJF KLGOL MFQFF ZXWCL WZXDR WVJWK MVGNJ KFXDD FNLJG DKLWJ ETDWC XVKXD LMFMG HFLMW LXVWD OXLXD AFLLF JQXLM LMFBG VWBKX MWPFW LFKLW LETDW CXSGD LMNJK EWTOG JWDXD LFJDK MXHXD LMFXJ HJGRJ WCCXD RKFVL XGDXO XVWDR FLWVV FKKLG LMFXJ DFLQG JZLMF DCWTA FQFVW DKLWJ LLGND JWPFB LMXKX DLMFC FWDLX CFXOG NDELM FWLLW VMFEC FCGKB XHOGB EFENH NDEFJ GDFBF RGOYW CFBXW KEFKZ WLOXJ KLXLM GNRML XLQWK YNKLW OXSOG JWQGA ABTEF KZANL XLLNJ DFEGN LLGAF WOWDL WKLXV HBWVF LGMXE FWKFV JFLXL JFCXD EFECF GOLMF HNJBG XDFEB FLLFJ QMXVM RXPFD LMFZF TQGJE XKHJG AWABT DGVGX DVXEF DVFXL BGGZK BXZFY WCFBX WWDEC WJLXD KMWJF EWBGP FGOGB EEFLF VLXPF KLGJX FKLJX DXLT\\n'"
36 ]
37 },
38 "execution_count": 2,
39 "metadata": {},
40 "output_type": "execute_result"
41 }
42 ],
43 "source": [
44 "c4a"
45 ]
46 },
47 {
48 "cell_type": "code",
49 "execution_count": 3,
50 "metadata": {
51 "collapsed": false
52 },
53 "outputs": [
54 {
55 "data": {
56 "text/plain": [
57 "(('waveform', <KeywordWrapAlphabet.from_largest: 3>), -2536.7576625007914)"
58 ]
59 },
60 "execution_count": 3,
61 "metadata": {},
62 "output_type": "execute_result"
63 }
64 ],
65 "source": [
66 "key_a, score = keyword_break_mp(c4a)\n",
67 "key_a, score"
68 ]
69 },
70 {
71 "cell_type": "code",
72 "execution_count": 4,
73 "metadata": {
74 "collapsed": false
75 },
76 "outputs": [
77 {
78 "name": "stdout",
79 "output_type": "stream",
80 "text": [
81 "harry and charlie i am no closer to the pds syndicate but i think i have begun to unravel why they might be interested in jamelia and martin as she told us in the hidden message i sent last week jamelia had been working on signal recognition at dynamix at first i was puzzled by this as i couldnt see how it related to weapon is ed gravity waves but then i took a look at her bookshelf and it began to make sense it was full of books on general relativity blackholes and neutron stars jamelia was not working on gravity waves but on gravitational waves i can see how the field agents would have got confused in the us they sometimes use both names for the same thing but they really are different gravity waves area seismological phenomenon as you already figured out gravitational waves are entirely different einstein predicted them a hundred years ago and they were detected by the ligo team last year the signals are given off by things like binary blackhole systems and are incredibly faint they easily get swamped by local noise it was an amazing feat detecting them and jamelia was part of that to add to our confusion she was in the team responsible for designing the search algorithms and the noise filters used to spot the signal of a galactic event against the noise of seismological activity so she did know something about gravity waves too i still dont know exactly how dynamix were using her expertise or what the pds syndicate are or do if you can get any intel on that it might help guide my enquiries the noise siam hearing suggest that pds are serious lots of cash lots of influence so i suggest we increase the security for future messages when you reply try using a double encryption reversing things before applying a keyword cipher to the message i will be out of contact for the rest of the week i am taking a crash course in neutron star dynamics in the hope that i can fit in better with the locals i have a test at dynamix on thursday for an internship in their programming section if i can get access to their network then maybe we can start to unravel this in the meantime i found the attached memo slip folded up under one leg of jamelia s desk at first i thought it was just a fix for a wobbly desk but it turned out to be a fantastic place to hide a secret it reminded me of the purloined letter which given the keyword is probably no coincidence it looks like jamelia and martin shared a love of old detective stories trinity\n"
82 ]
83 }
84 ],
85 "source": [
86 "print(wcat(segment(sanitise(keyword_decipher(c4a, key_a[0], key_a[1])))))"
87 ]
88 },
89 {
90 "cell_type": "code",
91 "execution_count": 2,
92 "metadata": {
93 "collapsed": false
94 },
95 "outputs": [
96 {
97 "data": {
98 "text/plain": [
99 "'tetheabmpnpskirytetfalulnhtdiiewnkhaeavrcleahecanoiufblgndiatssyeahmttlucodutacapyllrneevtemsooehftacattkehstksieytraohvtteshgierhtntiegilleaenctrethittghinamedgaflirotfaoictnofberiteislotoatitehwkinerncaeylalhtpelhegeansecicdloubawesmwdpeiafthllaseessrtofrouireltilanggtiorhsamhtreobeylalstrmabhtuteahntthwisaaetwrooegdiratghthtetstedpaatdorspvdeidiamsazlginyatdeiadlenkadmemtsiuaechstrieotesehtaeptsneritndokhwnooehwtylucodevhagosotmihucnmrfoanotiatuboplpeoetearhtseyognrieyrvetgnhiplpeoenodolieinnreevtguhohehtwnteisonoutaidphnhtmaeitmachpalyscsitithatlucoddaletmoosenithghtwisihucmidmeamitepitacslltilceovolosmoubgytretheraiseubalzppzalgnyiwwthaeralenuoedterheanytdaaxminacdinletbiheevocuwlkmayitvaohesithoroppttiunytemeiiwngtmohsefeonricomtleadlihetsetafrnonoillwityleloroumeetlarkoloiofngrdrwateeosioynguiaagnnosomitarn'"
100 ]
101 },
102 "execution_count": 2,
103 "metadata": {},
104 "output_type": "execute_result"
105 }
106 ],
107 "source": [
108 "c4br = cat(reversed(sanitise(c4b)))\n",
109 "c4br"
110 ]
111 },
112 {
113 "cell_type": "code",
114 "execution_count": 3,
115 "metadata": {
116 "collapsed": false
117 },
118 "outputs": [],
119 "source": [
120 "transpositions[transpositions_of('edgarallenpoe')] += ['edgarallenpoe']"
121 ]
122 },
123 {
124 "cell_type": "code",
125 "execution_count": 4,
126 "metadata": {
127 "collapsed": false
128 },
129 "outputs": [
130 {
131 "data": {
132 "text/plain": [
133 "(((3, 2, 0, 1, 4), False, False), -1959.882702003483)"
134 ]
135 },
136 "execution_count": 4,
137 "metadata": {},
138 "output_type": "execute_result"
139 }
140 ],
141 "source": [
142 "column_transposition_break_mp(c4br)"
143 ]
144 },
145 {
146 "cell_type": "code",
147 "execution_count": 6,
148 "metadata": {
149 "collapsed": false
150 },
151 "outputs": [
152 {
153 "data": {
154 "text/plain": [
155 "'the temp bank is pretty full and i think we have a real chance of building a system that could actually prevent some of the attacks the key is to harvest the right intelligence at the right time and flag it for action before it is too late i think we can really help the agencies could be swamped with false alerts so our filtering algorithm has to be really smart but then that is what we are good at right the test data pds provided is amazingly detailed and makes it much easier to see the patterns i dont know how they could have got so much information about people are they storing everything people do online in ever thought when i set out on a phd in mathematical physics that it could lead to something with such immediate impact i still love cosmology but there is a real buzz applying what we learned out here at dynamix and icant believe how lucky i am to have this opportunity meeting with someone from citadelle this afternoon i will tell you more later looking forward to seeing you again soon martin'"
156 ]
157 },
158 "execution_count": 6,
159 "metadata": {},
160 "output_type": "execute_result"
161 }
162 ],
163 "source": [
164 "wcat(segment(column_transposition_decipher(c4br, (3, 2, 0, 1, 4), False, False)))"
165 ]
166 },
167 {
168 "cell_type": "code",
169 "execution_count": 7,
170 "metadata": {
171 "collapsed": false
172 },
173 "outputs": [
174 {
175 "data": {
176 "text/plain": [
177 "['debar',\n",
178 " 'decaf',\n",
179 " 'decal',\n",
180 " 'decay',\n",
181 " 'ficas',\n",
182 " 'fleas',\n",
183 " 'freds',\n",
184 " 'gleam',\n",
185 " 'glean',\n",
186 " 'gofer',\n",
187 " 'great',\n",
188 " 'ikeas',\n",
189 " 'incas',\n",
190 " 'infer',\n",
191 " 'juicy',\n",
192 " 'krebs',\n",
193 " 'lobar',\n",
194 " 'loges',\n",
195 " 'lokis',\n",
196 " 'molar',\n",
197 " 'molds',\n",
198 " 'moldy',\n",
199 " 'moles',\n",
200 " 'nolas',\n",
201 " 'opecs',\n",
202 " 'pricy',\n",
203 " 'pries',\n",
204 " 'prigs',\n",
205 " 'prods',\n",
206 " 'profs',\n",
207 " 'proms',\n",
208 " 'pshaw',\n",
209 " 'punky',\n",
210 " 'rugby',\n",
211 " 'stony',\n",
212 " 'straw',\n",
213 " 'stray',\n",
214 " 'strew',\n",
215 " 'sulky',\n",
216 " 'surly',\n",
217 " 'debase',\n",
218 " 'debate',\n",
219 " 'deccan',\n",
220 " 'deccas',\n",
221 " 'encase',\n",
222 " 'endear',\n",
223 " 'fleecy',\n",
224 " 'freddy',\n",
225 " 'giddap',\n",
226 " 'goodby',\n",
227 " 'grease',\n",
228 " 'grebes',\n",
229 " 'greeds',\n",
230 " 'greedy',\n",
231 " 'guffaw',\n",
232 " 'higher',\n",
233 " 'hoffas',\n",
234 " 'incant',\n",
235 " 'indias',\n",
236 " 'indict',\n",
237 " 'infant',\n",
238 " 'inhere',\n",
239 " 'jogger',\n",
240 " 'kojaks',\n",
241 " 'locals',\n",
242 " 'logger',\n",
243 " 'looker',\n",
244 " 'nolans',\n",
245 " 'nonfat',\n",
246 " 'ormolu',\n",
247 " 'ornery',\n",
248 " 'oshawa',\n",
249 " 'osmium',\n",
250 " 'ossify',\n",
251 " 'prepay',\n",
252 " 'promos',\n",
253 " 'prompt',\n",
254 " 'pronto',\n",
255 " 'proofs',\n",
256 " 'pshaws',\n",
257 " 'pulley',\n",
258 " 'straws',\n",
259 " 'strays',\n",
260 " 'strews',\n",
261 " 'sulkys',\n",
262 " 'supply',\n",
263 " 'surrey',\n",
264 " 'swishy',\n",
265 " 'tunney',\n",
266 " 'tweedy',\n",
267 " 'twiggy',\n",
268 " 'debased',\n",
269 " 'debases',\n",
270 " 'debated',\n",
271 " 'decades',\n",
272 " 'decayed',\n",
273 " 'decease',\n",
274 " 'encases',\n",
275 " 'fleeces',\n",
276 " 'goddard',\n",
277 " 'greases',\n",
278 " 'greater',\n",
279 " 'greeces',\n",
280 " 'indians',\n",
281 " 'inhibit',\n",
282 " 'kookier',\n",
283 " 'locally',\n",
284 " 'mombasa',\n",
285 " 'mommies',\n",
286 " 'mummify',\n",
287 " 'noonday',\n",
288 " 'precept',\n",
289 " 'precess',\n",
290 " 'proctor',\n",
291 " 'pronoun',\n",
292 " 'propers',\n",
293 " 'strauss',\n",
294 " 'supplys',\n",
295 " 'surreys',\n",
296 " 'debarred',\n",
297 " 'deceased',\n",
298 " 'deceases',\n",
299 " 'endeared',\n",
300 " 'freebees',\n",
301 " 'indianas',\n",
302 " 'mombasas',\n",
303 " 'opponent',\n",
304 " 'prepares',\n",
305 " 'decadence',\n",
306 " 'deceaseds',\n",
307 " 'indianans']"
308 ]
309 },
310 "execution_count": 7,
311 "metadata": {},
312 "output_type": "execute_result"
313 }
314 ],
315 "source": [
316 "transpositions[(3, 2, 0, 1, 4)]"
317 ]
318 },
319 {
320 "cell_type": "code",
321 "execution_count": 8,
322 "metadata": {
323 "collapsed": false
324 },
325 "outputs": [
326 {
327 "data": {
328 "text/plain": [
329 "(3, 1, 0, 2, 4)"
330 ]
331 },
332 "execution_count": 8,
333 "metadata": {},
334 "output_type": "execute_result"
335 }
336 ],
337 "source": [
338 "transpositions_of('edgar')"
339 ]
340 },
341 {
342 "cell_type": "code",
343 "execution_count": null,
344 "metadata": {
345 "collapsed": true
346 },
347 "outputs": [],
348 "source": []
349 }
350 ],
351 "metadata": {
352 "kernelspec": {
353 "display_name": "Python 3",
354 "language": "python",
355 "name": "python3"
356 },
357 "language_info": {
358 "codemirror_mode": {
359 "name": "ipython",
360 "version": 3
361 },
362 "file_extension": ".py",
363 "mimetype": "text/x-python",
364 "name": "python",
365 "nbconvert_exporter": "python",
366 "pygments_lexer": "ipython3",
367 "version": "3.5.2"
368 }
369 },
370 "nbformat": 4,
371 "nbformat_minor": 1
372 }