2021 challenge 1
[cipher-challenge.git] / 2020 / 2020-challenge6.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 21,
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.vigenere import *\n",
14 "from szyfrow.support.text_prettify import *\n",
15 "from szyfrow.polybius import *\n",
16 "import collections"
17 ]
18 },
19 {
20 "cell_type": "code",
21 "execution_count": 2,
22 "metadata": {
23 "Collapsed": "false"
24 },
25 "outputs": [],
26 "source": [
27 "challenge_number = 6\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": 8,
37 "metadata": {
38 "Collapsed": "false"
39 },
40 "outputs": [],
41 "source": [
42 "ca = open(ciphertext_a_filename).read()\n",
43 "sca = sanitise(ca)\n",
44 "rsca = cat(reversed(sca))\n",
45 "cb = open(ciphertext_b_filename).read()\n",
46 "scb = sanitise(cb)"
47 ]
48 },
49 {
50 "cell_type": "code",
51 "execution_count": null,
52 "metadata": {
53 "Collapsed": "false"
54 },
55 "outputs": [],
56 "source": [
57 "fc = collections.Counter(sca)\n",
58 "plot_frequency_histogram(fc, sort_key=fc.get)"
59 ]
60 },
61 {
62 "cell_type": "code",
63 "execution_count": 12,
64 "metadata": {
65 "Collapsed": "false"
66 },
67 "outputs": [
68 {
69 "name": "stdout",
70 "output_type": "stream",
71 "text": [
72 "held KeywordWrapAlphabet.from_last \n",
73 "\n",
74 "eutzo propo khzdc gowfs jovhp ahbsa euvwn ctotq lwssk bdtsd ukcvv dnorc shhgm ebpqf unotk enwhc putjs eqjat egceo giutb femtq jcuwt ebuop ategj euuss ebfiz dvhkp ewpio fsoaq urwag pyier hwhpg auwfh btuwn oesvq dussv vatsw ehnqi ikbhv erhfo tiwjw ejwsp etega ezswg ptwsh osxys cgowf ierdn gaery xstvs fezov egvva oongt owbfp etjwn ciots pqgtv vafga eytjg owuia omhki hbops oiiih zdprl erchh mhbtk enhrt jotuv ayosu ofves hsihg ptsvk euuga uwfhb tnsdx ssqhu grapz yfctc zdgii eitjs yjoff sapob nstqg aaemo snghu avdpw ehgar zdasm hbtie rvvai zdvwm kuivv dxsmk hshrt jscof otidp qaqtt jwsfc tysih orgii hhdos sverd otvvr hsfkt ghgap idgrr hhshh cpiih sduia proiz oprop olnkc vvtjs sedag spegt wgago thong iieie qwnaw fhrwk iivva qgccw nezdt gcxol qttjs srmen wsvio wgcpu hteur trqdn qggqz kqbaq ttjsl ebfno fkssj ofuem hkiei gqgtw wtqcs nmkhf tegaa ergeg vvaae mkbhu onguo kbhue gjsrt sskra piseb fyose plhio gssag cfsvk hcver uiovv agspe gthrt hbdpi tjsrh vdgpa hbdpw nagae hakbt jsnwd bhgox srvva vvrhs whsku zaerc puuri ovvak gfhfd tiuts dprtj otgot hqokb ekrag kcvvt jsdtg cxolq ttjss rmenw sviow gcpuh teurw nvvaw ycawr aclei agrau qrkft kenue gvvag spegt hrthb dpisv eowgd csnvh dviih fotid wiiqg cvwau ongii hmwhg aeplh ioaen iwroi ieidc gowfm eiejw nciih rauqr kftke njofn sgvii hqowb ttmtj sfemd iiati ihhpd qlkht cgowf dtgcx sfeik kbhuq rqhsq creua piseb feyan ohteu rhcps susxe bfmsn vkats duyag iotsp qgtqb tjssr menws vurqc puwnv vakgn hwhjp owgiq efuon gpovv thomu hapim holkh tqttj stqcr kbhro rvwau gaaen awlkb hvvao ohewn uitjs pegtd egvka piyvv dvhav egitr qdlkb eqzny sieja nesvo nqiih ggkja ezopu tjswe mlwqk kzyvv aeyan ohteu rwnhh shlie rbhsn yotav cpugq gtjsf hfdti utsgt emvva hosvs ngong hoysk pewvv dviih tunzt ysnvm shioi tdnen ciihi iedau wnvko cgowf sqtth bwkii hwhji dtgcx wncwn ukdns dprsh japot aonhk fqbtj sdago uicaw nvvan osvia nshto myscp iatqa riagh phzlh rowit jsmhh seuae itegh hirgt dprct onvvd vfdui mdoct dcpws vgyae nvoev hwjed vzdui dcgah rtqpr ksgos opiih wraen asrph rgtsv onghg qgrer cqrct sevwo pongt cprcp ucsca uicqb agiik htjwn mwncw tyose batgo tongi ieitj syosd pirer cqrct sevwo ptcpr cputj snejc cotke numsv smysu usbwi drfdt snvzy vvcuw suemh iikbh pswig ooiih poitc phopi ihicb orgqo odcvi ahiih meezl kiejo cpvoo sdprc vgais ruioe bayoa tepno nhrav sevwo phyui aoiie iuuss tsgns evwop egtof kewej auiot oncsg kbfeh whzle hcprc aothp aegcp ugtem ewrvv rhotu iihmi ejafs apsxr srkda picpu wkiiv vcuto towjw lhong orhbo ypukz fkbhq cteha twaue goosv htquc xsgte nvzcp swegn kbhqt dkgdv idays vvaiw ruigh ksvot kenuo rhpak bhfcc nicpb otton ylkbe qzneb fhhsh ltqfr qjcgs dtwnc eggsg hbeho rqcng zopro pongw tneom hlkya qcruf yazcu igtwa prseg ajcnv wncto twnie rootk enepo witjw svsej boneh dwtkh nqien sdtwg vvady nqkwj otkic upuvw tkheh gtewn vvdvk ajovh iouio riihd gkbfk bhqct komez sqbov hutsw jotvv aowsu wncfd tiyos mfsru orhro kbhki eebtf sjwht oerhh pdwnc osvva kgeqj atkcv vtjst qcrkb hrorv wauol newhr tjsme hmwqi eqehh sehtj syysr hzcms ldioc stues qdavv cpuan hakhu rwdov orwnc iieit jsdvi davag wnvsr aspvk cnzhk jawhd azuhk aebdn mshrt jsapq rdfth rthza cgdoo ngtow bfvvd vwtwh auoen osuwe ergcj xawpj srkiu ussvv auomh yadko trgqg tjstt onufo uwtke nuidc soiii hqcrv atosk vdxsu usfie rvvcu jccsn hgaaw pjsre bfmbo ywnci ieicv hiqcl gpats duene pldht toccv tiery orgio agday tjsct dauhd csbwi yqcwk zljov hioav aaytj sfhid kzsie rdeut hantd uwiej avehq vdtgy joseh khrmh iovgd xslve eewsv erves hiuro eqdme bfres votvv atrgv ewhgc pqdus whbah rtqoe vaukq knmci kaaon agday tjwsq baebf yerme uvkie icuuo kbhqb tjsnk vorsw hqdpd owbte qowbt hgtqi ihhpd qlkht qfato tkenf sgqga kiswq ehsfu faegl\n",
75 "\n"
76 ]
77 }
78 ],
79 "source": [
80 "(word_a, wrap_a), score_a = keyword_break_mp(rsca, fitness=Pletters)\n",
81 "print(word_a, wrap_a, '\\n')\n",
82 "pa = keyword_decipher(ca, word_a, wrap_a)\n",
83 "print(pa)"
84 ]
85 },
86 {
87 "cell_type": "code",
88 "execution_count": 17,
89 "metadata": {
90 "Collapsed": "false"
91 },
92 "outputs": [
93 {
94 "name": "stdout",
95 "output_type": "stream",
96 "text": [
97 "rdf \n",
98 "\n",
99 "ourlondonakelagroupshavebeenscoutingforcluesinareaswithalargegermanpopulationusingthecoverofgoodturndaytovisitanumberofhousesandflatsknowntobeoccupiedbyforeignresidentsinmostcasestherewasnothingtoreportwhichisnotarealsurpriseasvksgrouphadalreadyvettedalotofthemandfoundnothingtoreportthebreakthroughcamewhenoneofthelandladiesmentionedthatshewassadtoseeherpreviousresidentleavesosuddenlybutgladthattheyhadbeenabletorecommendsuchanicereplacementfortheflatimighthavemissedtheimportanceofthisbutweheardthesamestoryatthreedifferentaddressesintheeastendoflondonallwiththesamedeparturedateandthatcoincidedwiththeoriginalarrivalofthespyclisttouringgroupfromnorfolkoneofthelandladieshadsomewhatfortuitouslykeptarecordofthecomingsandgoingsofherresidentsandwasabletodescribevisitorstothedepartedtenanttherehadbeenanincreaseinthenumberoverthethreeweeksleadinguptotheirdepartureandthatdatecoincidedwiththearrivalofthespyclisttouringgroupintheukicirculateddescriptionsofthedepartedtenantstoouragentsattheportauthoritiesandtheywereabletoconfirmthatagroupmatchingthedescriptionhadleftthecountrythedayafterthespyclistgrouparrivedatkingscrossouragentsandakelagroupsinessexandkentwereaskedtoreportonthespyclistgroupsintheirneighbourhoodsandbothteamssentmealistofthetouringpartiesreconcilingthemagainstthepartyoftwentythatsetofffromlincolnwehavelostanotherfivealongthewayluckilytheakelagroupinessexhadbeenwatchingforthedeparturefromtheeastendandsoweknowthatthefulltwentysetoffalongthethamesintwogroupsoftenwitheightarrivinginswaleandsevenatcanewdontheacrosticinthelasttelegramweinterceptedspelledoutthemessageattargetrdfandiranthatpastmyairministrycontactswhoatlastagreedtobriefmeontheirconcernsrdfstandsforradiodirectionandfindingiquestionedthisthinkingitwasanerrorandthattheymeantradiodirectionfindingthenavigationsystemweusebutapparentlythisissomethingnewfromtheboffinsonthetizardcommitteetheycallitchainhomeanditreferstoanewaeroplanedetectionsystemthatusesreflectionofradiowavestorangefindaswellasindicatebearingfromairthreatstheyhavebeenexperimentingwiththisforawhileandarenowbuildingoutaseriesofmaststogivefrontlinewarningofairattacksthefirstfewstationsarebeingbuiltinnorfolklincolnandessextoprovidearingofdefencearoundlondonanditlookslikeourspyclistfriendsarehuntingforinformationaboutthistechnologyitisnotcleariftheyknowwhatitisbutitiscertainthatwehavetostopthemfindingoutiamalsonotsurewhatthemissingpartymembersaredoingitcantbejustmorespyingastheircoverwiththetouringpartiesallowedthemasmuchaccessastheywerelikelytogetsosomethingelseisupiamhopingthattheattachedinterceptwillgiveusaclueweanalysedtheencryptedtelegramandfoundthatitusesaclassicadfgvxcipheritusesthesamekeywordforthetranspositionstageofthecipherasihaveusedforthisvigenerecipherandknowingthatitshouldbereasonablystraightforwardtocracktheirmessagebutyouwillhavetocheckthedetailsforyourselfasihavetogoharryhasaskedmetotraveltocaistortosetupacommandpostattherdftowerincaseweneedtoactquicklyifwecancrackthisoneandworkoutwhatisgoingonthenihopewecanmountacountertothespyclistoperationbeforeitsucceedspearl\n"
100 ]
101 }
102 ],
103 "source": [
104 "key_a, score_a = vigenere_frequency_break(sca, fitness=Ptrigrams)\n",
105 "print(key_a, '\\n')\n",
106 "pa = vigenere_decipher(sca, key_a)\n",
107 "print(pa)"
108 ]
109 },
110 {
111 "cell_type": "code",
112 "execution_count": 19,
113 "metadata": {
114 "Collapsed": "false"
115 },
116 "outputs": [
117 {
118 "name": "stdout",
119 "output_type": "stream",
120 "text": [
121 "our london akela groups have been scouting for clues in areas with a large german population using\n",
122 "the cover of good turn day to visit a number of house sandflats known to be occupied by foreign\n",
123 "residents in most cases there was nothing to report which is not a real surprise as v ks group had\n",
124 "already vetted alot of them and found nothing to report the breakthrough came when one of the land\n",
125 "ladies mentioned that she was sad to see her previous resident leave so suddenly but glad that they\n",
126 "had be enable to recommend such a nice replacement for the flat i might have missed the importance\n",
127 "of this but we heard the same story at three different addresses in the east end of london all with\n",
128 "the same departure date and that coincided with the original arrival of the spy c list touring group\n",
129 "from norfolk one of the land ladies had somewhat fortuitously kept a record of the comings and\n",
130 "goings of her residents and was able to describe visitors to the departed tenant there had been an\n",
131 "increase in the number over the three weeks leading up to their departure and that date coincided\n",
132 "with the arrival of the spy c list touring group in the uk i circulated descriptions of the departed\n",
133 "tenants to our agents at the port authorities and they were able to confirm that a group matching\n",
134 "the description had left the country the day after the spy c list group arrived at kings cross our\n",
135 "agents and akela groups in essex and kent were asked to report on the spy c list groups in their\n",
136 "neighbourhoods and both teams sent me a list of the touring parties reconciling them against the\n",
137 "party of twenty that set off from lincoln we have lost another five along the way luckily the akela\n",
138 "group in essex had been watching for the departure from the east end and so we know that the full\n",
139 "twenty set off along the thames in two groups often with eight arriving in swale and seven at ca new\n",
140 "don the acrostic in the last telegram we intercepted spelled out the message at target rdf and iran\n",
141 "that past my air ministry contacts who at last agreed to brief me on their concerns rdf stands for\n",
142 "radio direction and finding i questioned this thinking it was an error and that they meant radio\n",
143 "direction finding the navigation system we use but apparently this is something new from the boffins\n",
144 "on the tizard committee they call it chain home and it refers to a new aeroplane detection system\n",
145 "that uses reflection of radiowaves to range find as well as indicate bearing from air threats they\n",
146 "have been experimenting with this for a while and are now building out a series of masts to give\n",
147 "frontline warning of air attacks the first few stations are being built in norfolk lincoln and essex\n",
148 "to provide a ring of defence around london and it looks like our spy c list friends are hunting for\n",
149 "information about this technology it is not clear if they know what it is but it is certain that we\n",
150 "have to stop them finding out i am also not sure what the missing party members are doing it cant be\n",
151 "just more spying as their cover with the touring parties allowed them as much access as they were\n",
152 "likely to get so something else is up i am hoping that the attached intercept will give usa clue we\n",
153 "analysed the encrypted telegram and found that it uses a classic adfg vx cipher it uses the same\n",
154 "keyword for the transposition stage of the cipher as i have used for this vi genere cipher and\n",
155 "knowing that it should be reasonably straightforward to crack their message but you will have to\n",
156 "check the details for yourself as i have to go harry has asked me to travel to caistor to setup a\n",
157 "command post at the rdf tower in case we need to act quickly if we can crack this one and workout\n",
158 "what is going on then i hope we can mount a counter to the spy c list operation before it succeeds\n",
159 "pearl\n"
160 ]
161 }
162 ],
163 "source": [
164 "pa = prettify(vigenere_decipher(sca, key_a))\n",
165 "print(pa)"
166 ]
167 },
168 {
169 "cell_type": "code",
170 "execution_count": 20,
171 "metadata": {
172 "Collapsed": "false"
173 },
174 "outputs": [
175 {
176 "data": {
177 "text/plain": [
178 "3756"
179 ]
180 },
181 "execution_count": 20,
182 "metadata": {},
183 "output_type": "execute_result"
184 }
185 ],
186 "source": [
187 "open(plaintext_a_filename, 'w').write(pa)"
188 ]
189 },
190 {
191 "cell_type": "code",
192 "execution_count": 38,
193 "metadata": {
194 "Collapsed": "false"
195 },
196 "outputs": [
197 {
198 "data": {
199 "text/plain": [
200 "'addfaagdgvfgddfddfgxdvfddgdffdffggdgdfggfvgxdfgdaaggddaafgdfgxadfvfgdvgfaagfdvfgggdfgddfgfggdvfgagaagfvafvgvdgaaadgfgvagagdfgfggdfaddvgggxfvgvfdaddadfaafgaddvddaafggfdfdfdgfvgxdvgggxfvgvfdadfddvafggggdgdfgffxdvgddvgggffvaffvgvgddffgagfddvgfdgdgfvgfgggfffaafadvfgagggdgdfffafdfdffdgfaaafdfaafgadgfdfddgvgddfdvfgggdgdfdvgdaffvgdgggddfgfgfdvgffdaafgadgfggfvfxfgdfavdfgdggdgdffddfgfgffgfvggdgdvfgagfvfgggdgdfdfaagdggdgdvgfaffvgddfavdfgdaafgaddvggdgdvfgfaggdgdfvaffaavadgaaavdfaaffdvgffxfdaadddfadafaadvggdgdvfgggdgdvgfaafgadggdgdfdgdfaaavdffgfdvafxfvgxdfgdgfffaavafgfvggdadfgfgggdfvfgagdffgfvgvagdgggfvfxgdfvggdfddggggdgdfffafgdfvffggdgdffvfgddfvffdvfgaggfggfvgdffgfggfvfxggdgdffdfvddaafdfvgdgggfaggdgvfxfxdfgfdgaagddfadgxdvggdgffdfvafvgvgdfdaaggdfgfggagdvafggggdgdfdadfaagvggdvafgvfdddfdfvddfaaddfavdvdddfaafgadgxdfdgaaavdfgffxdffgggffgvdddgggdvffdfaaadffdvgddvfgagdvgggfggfvfxdvgggfgfdvfffxfddvdddvggvadvgfaaadffdvgdaadafddfaafgadfvfgdddfgxdfdgaaavdffddfaagdfgdfadggfvgvgfdfdvggfxgdfvfxdfgdfdvadvgggxdvfdfdfxgdfvavdvaddfgvgfaafdfdgxdvggdgaaavdfgdvadgdvagdgfddfavdffdfvafgfdfddgvgddvggvagfggfvfxgfaaafdfggvaffgvgfggdadffvgvgdfxgddvffaagdvaddfvfgdddfgdfgfgfvgxaagfaafdaaddfafvafddfvfgdddffggggdaaggdvfvfgaaggggdgdvgffxfvdvfgggddfvgvfdaddagddvfgagaddvgfaagfggdfgdgfggfvfxgdaafdafdgaagfdadfdffgagdvavdffgggdgdfaaddddfvfffxaafgvadvfgagfadfvadavaggdgdffvgdgggfaggdgvfxfxdfdvfgfadffgggaafgadfvfgdddfgxdfaagddfgddfgvfgdvggdfaddvgxdvfdfdgvgfdfggdgdvgfggfvgfdfddgvgddffvgvgdafgvgggvgddfddfvffffgvfgdvddaaggdvfvfggfgfggfvfxdfgfggaadafddvgfdgdvfgagaagfaaafdffxfdaafgaffvgdfvgvgddxfvgvgdfgdfvadgfvffdfdvgfdfgfgfdffgggdvaafddvfgfvgdaddfgdggfvaaavfvdvadgvfgfxfddfaagfaafggggfgvgdfxgddvgfdfgfgfggfvfxgagvaagdgddffdgfgxdvggdgfvgvgddgfvgfgggxfvgvfdaddadfaddvafafdvddgvfdggggfvgddfgffvfdavdfaafgaddvgxfvgvfdadfxgddfafdfgddvggdvafgxdfddfvgvfdadgffddvfxaagxaavagvfggddfffaagdfadfadgfggfvfxgvgfdffvafggdgdffdfvddaafdafdvgfdgdvfgagdafvaagggfffdvagdgggdadffxfvgfgfdvdafddfdagvggdvafaafgvaggdgdvfgaggxdfgddfggfvaafdaagdffggdgdffdfvddaafdgfggdgaaggffdvagdggggxdffdfdfxgddfavdffgggfvgvgdaddffxaagdgggvgddfaafgadggdgdfddfdfvddfaddfvgvfdadafaafdfddvfgggfvggdgdfgxgdfvfgagdgaafgadgfgfggfvfxdvafvafvgvdgaaavdfaagfgvagagdfgfggdvfvfgaadafvgvggdgfvgxdadfgfggggfvaagdgdaafgagdffvgvgdgggdaaavdffdggdgdffgfxfddfaagfdffddfgggvgffafgfvgxaafgadgxdfgxdvfdfdffaafadfggdgdfgddfgagvdvgddfadaagdgdaafgagdfffdffggggfgfggfvfxgdaafdafgffxaagdggvagxdvfdfdgddfgggvgdfgggfvfdfvfgadfvfgggdgdvgfgxdfdffaaafgadgxdvfdfdggdgdffggggdaaavdffdggfvffdfdfgggxdvggdggvgfaffvgdggdgdfafdvfgaafdgfggaaagdffvaffvgvgdaaadavdffggggvgddfgxdgdffggxdfgxdvfdfdaafdfdgddfaggdfvgvfxdadfaffvgddfgddfgggvgdfgdvfgagdgfvffdfgfggfvfxdfavdffgdvafvafvgvadfvfgfvgggxdvgfdggvgfggfvfxgdfvdddfdfadgxdvggdgfvgvgdfvgddvagdvfgaafdfxfdaafgggdgdffggxdfaagddfaadafddfggfvgddfgggvgdfggxdvggdgaagxdgfvfddfgggddfaagfgvgddffvafadgdaagxdvfgaggffxdgfvggfvaggdaafxdggfaafgadaddfgfddgddvfxggdvfvfggffvafggdgdfgfdfdvfffxfvgdggaafgggfdaafgadffaagdfagfaffvgdvafvgvaafgadvafvgvgdafgddvdffgadgfggfvgfgggvadvagfggfvfxaddfggaadvfdgffvafvafvgvgdgxdvgfdgdfgfgfdgfvgvfdaddadfgfdffgggdvfgggdgdfgvgfgvaafdgxaavagvgfdvfgagvafvgvgdgddfffaagdfaaadafddfddfdfvddfaaddfavdvdddfgfggfvfxfaaagdfd'"
201 ]
202 },
203 "execution_count": 38,
204 "metadata": {},
205 "output_type": "execute_result"
206 }
207 ],
208 "source": [
209 "tscb = column_transposition_decipher(scb, 'rdf', fillcolumnwise=False, emptycolumnwise=True)\n",
210 "tscb"
211 ]
212 },
213 {
214 "cell_type": "code",
215 "execution_count": 41,
216 "metadata": {
217 "Collapsed": "false"
218 },
219 "outputs": [
220 {
221 "data": {
222 "text/plain": [
223 "'biauxqhoiylokiopwkiwryiuawhaqiybrqlvavlqwiuivwlqdavzrxkabvxddivwiblwyrxobgiaqblhaqviikrylwyrxobolcwwkivslulwvrcrxuiqdolvkkrvwvpanlqdwkipciiovaciaqbvihxuilqwkilucruwuivvlvoaqbvwrsqieiuwkioivvqrwklqdrqwkiiauwklvcruieiuaqblwklqnwkizpazkaeiaplvsoahibcalwklqwklvaqbwkikiaeiqozsryiuvpazqrwgivwurqdiqrxdkwrsurwihwwkipcurpwkirqhrplqdvwrupvwrswkiorhaoruwvduxssivkauibylwkpizrxuoawivwdlcwwkigiaxwlcxohorhnbielhiaqbyikaeivsiqwpxhkwlpiabplulqdlwvwrslwvvlpsolhlwzlvabpluagoiaqbrqhiyikaeioiauqibwrxvilwsursiuozlwyloosurelbixvaooylwkaeiuzkldkoieiorcvihxulwzvwrsvaciwzpxvwgirxusulpauzhrqhiuqqryavaoahnrchrqhiqwuawlrqawwklvsrlqwhrxobgulqdblvavwiuvwrsuaockavgiiqdleiqwkiahhrpsaqzlqdnizgzwkiruwvduxssilqniqwaqbrqhiyiauiuixqlwiblylooxviwklvwrvihxuirxucxwxuihrppxqlhawlrqvvwrsivwagolvklqdavacisoaqcrurxumrxuqizkrpilvivviqwlaolqrubiuwraerlbxqsoiavaqwvxusulvivvwrstxauuiovylwkrxukrvwyrxobgiblcclhxowwruivroeiaqblyrxobsuiciulwlcyihrxobvolsayazxquipaunibvwrsxvircwkiorhaoclvklqdgrawvpldkwgisrvvlgoigxwlcaqzwklqdyiuiwraoaupwkiorhaovwkawpldkwyioosuieiqwrxubisauwxuiaqbwkihorhnhrxobcaoolqwrwkiyurqdkaqbvvwrslczrxkaeiavxddivwlrqagrxwkrygivwwrauuaqdirxuwuaeiowkiqsoiavioiwxvnqryaqbyiyloopaniwkiuitxluibauuaqdipiqwvvwrsuaocvsauwzyloouiwxuqwrorqbrqwklvyiinaqbyloowkiqwuaeiowrpiiwylwkxvcruwkiclqaovwadircrxuabeiqwxuiykiqyiylooaoouidurxsgicruiuiwxuqlqdkrpivwrsieiqlczrxbrqrwylvkxvwrsurhiibylwkrxuruldlqaosoaqwkiqyiauiagoiwruiwxuqylwkaykroiwuiavxuircbuaylqdvskrwrduaskvaqbbivhulswlrqvrcwkivilpsruwaqwoaqbpaunvcruzrxaqbzrxuculiqbvwrvwxbzvwrsbiwalovrczrxuylvkivvkrxobgiviqwlqwkixvxaoyazxvlqdzrxuuipaunagoihorhnbielhivwrsnauo'"
224 ]
225 },
226 "execution_count": 41,
227 "metadata": {},
228 "output_type": "execute_result"
229 }
230 ],
231 "source": [
232 "ptscb = polybius_decipher(tscb, 'a', 'adfgvx', 'adfgvx', wrap_alphabet=KeywordWrapAlphabet.from_last)\n",
233 "ptscb"
234 ]
235 },
236 {
237 "cell_type": "code",
238 "execution_count": 42,
239 "metadata": {
240 "Collapsed": "false"
241 },
242 "outputs": [
243 {
244 "name": "stdout",
245 "output_type": "stream",
246 "text": [
247 "aghbicdklmnopqrstuvwxeyfzj \n",
248 "\n",
249 "dearunclewilhelmthetoweratcanewdonisasinterestingasyouhadsuggesteditwouldbeandicanseehowitwouldliftthespiritsofourenglishhostsmakingthemfeelsafeandsecureintheirfortressislandstopneverthelessnothingontheearthisforeverandithinktheymayhaveamisplacedfaithinthisandtheheavenlypowersmaynotbestrongenoughtoprotectthemfromtheoncomingstormstopthelocalortsgruppesharedwithmeyourlatestgiftthebeautifulclockdeviceandwehavespentmuchtimeadmiringitstopitssimplicityisadmirableandoncewehavelearnedtouseitproperlyitwillprovideusallwithaveryhighlevelofsecuritystopsafetymustbeourprimaryconcernnowasalackofconcentrationatthispointcouldbringdisasterstopralfhasbeengiventheaccompanyingkeybytheortsgruppeinkentandoncewearereunitediwillusethistosecureourfuturecommunicationsstopestablishingasafeplanforourjourneyhomeisessentialinordertoavoidunpleasantsurprisesstopquarrelswithourhostwouldbedifficulttoresolveandiwouldpreferitifwecouldslipawayunremarkedstopuseofthelocalfishingboatsmightbepossiblebutifanythingweretoalarmthelocalsthatmightwellpreventourdepartureandtheclockcouldfallintothewronghandsstopifyouhaveasuggestionabouthowbesttoarrangeourtravelthenpleaseletusknowandwewillmaketherequiredarrangementsstopralfspartywillreturntolondonthisweekandwillthentraveltomeetwithusforthefinalstageofouradventurewhenwewillallregroupbeforereturninghomestopevenifyoudonotwishustoproceedwithouroriginalplanthenweareabletoreturnwithawholetreasureofdrawingsphotographsanddescriptionsoftheseimportantlandmarksforyouandyourfriendstostudystopdetailsofyourwishesshouldbesentintheusualwayusingyourremarkableclockdevicestopkarl\n"
250 ]
251 }
252 ],
253 "source": [
254 "word_b, score_b = simulated_annealing_break(ptscb, fitness=Ptrigrams,\n",
255 " plain_alphabet=string.ascii_lowercase, cipher_alphabet=keyword_cipher_alphabet_of('rdf', wrap_alphabet=KeywordWrapAlphabet.from_largest))\n",
256 "print(word_b, '\\n')\n",
257 "pb = keyword_decipher(ptscb, word_b)\n",
258 "print(pb)"
259 ]
260 },
261 {
262 "cell_type": "code",
263 "execution_count": 43,
264 "metadata": {
265 "Collapsed": "false"
266 },
267 "outputs": [
268 {
269 "name": "stdout",
270 "output_type": "stream",
271 "text": [
272 "dear uncle wilhelm the tower at ca new don is as interesting as you had suggested it would be and i\n",
273 "can see how it would lift the spirits of our english hosts making them feel safe and secure in their\n",
274 "fortress islands top nevertheless nothing on the earth is forever and i think they may have a\n",
275 "misplaced faith in this and the heavenly powers may not be strong enough to protect them from the on\n",
276 "coming storms top the local orts gruppe shared with me your latest gift the beautiful clock device\n",
277 "and we have spent much time admiring its top its simplicity is admirable and once we have learned to\n",
278 "use it properly it will provide us all with a very high level of security stop safety must be our\n",
279 "primary concern no was a lack of concentration at this point could bring disasters top ralf has been\n",
280 "given the accompanying key by the orts gruppe in kent and once we are reunited i will use this to\n",
281 "secure our future communications stop establishing a safe plan for our journey home is essential in\n",
282 "order to avoid unpleasant surprises stop quarrels with our host would be difficult to resolve and i\n",
283 "would prefer it if we could slip away unremarked stop use of the local fishing boats might be\n",
284 "possible but if anything were to alarm the locals that might well prevent our departure and the\n",
285 "clock could fall into the wrong hands stop if you have a suggestion about how best to arrange our\n",
286 "travel then please let us know and we will make the required arrangements stop ralf s party will\n",
287 "return to london this week and will then travel to meet with us for the final stage of our adventure\n",
288 "when we will all re group before returning home stop even if you do not wish us to proceed with our\n",
289 "original plan then we are able to return with a whole treasure of drawings photographs and\n",
290 "descriptions of these important landmarks for you and your friends to study stop details of your\n",
291 "wishes should be sent in the usual way using your remarkable clock devices top karl\n"
292 ]
293 }
294 ],
295 "source": [
296 "ppb = prettify(pb)\n",
297 "print(ppb)"
298 ]
299 },
300 {
301 "cell_type": "code",
302 "execution_count": 45,
303 "metadata": {
304 "Collapsed": "false"
305 },
306 "outputs": [
307 {
308 "data": {
309 "text/plain": [
310 "1950"
311 ]
312 },
313 "execution_count": 45,
314 "metadata": {},
315 "output_type": "execute_result"
316 }
317 ],
318 "source": [
319 "open(plaintext_b_filename, 'w').write(ppb)"
320 ]
321 },
322 {
323 "cell_type": "code",
324 "execution_count": null,
325 "metadata": {
326 "Collapsed": "false"
327 },
328 "outputs": [],
329 "source": []
330 }
331 ],
332 "metadata": {
333 "jupytext": {
334 "formats": "ipynb,md"
335 },
336 "kernelspec": {
337 "display_name": "Python 3",
338 "language": "python",
339 "name": "python3"
340 },
341 "language_info": {
342 "codemirror_mode": {
343 "name": "ipython",
344 "version": 3
345 },
346 "file_extension": ".py",
347 "mimetype": "text/x-python",
348 "name": "python",
349 "nbconvert_exporter": "python",
350 "pygments_lexer": "ipython3",
351 "version": "3.7.4"
352 }
353 },
354 "nbformat": 4,
355 "nbformat_minor": 4
356 }