{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os,sys,inspect\n", "currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))\n", "parentdir = os.path.dirname(currentdir)\n", "sys.path.insert(0,parentdir) \n", "\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "\n", "from cipher.keyword_cipher import *\n", "from cipher.vigenere import *\n", "from support.utilities import *\n", "from support.text_prettify import *\n", "\n", "c6a = sanitise(open('6a.ciphertext').read())\n", "c6b = sanitise(open('6b.ciphertext').read())" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(('hammering', ), -2247.716859509375)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(key_a_word, key_a_wrap), score = keyword_break_mp(c6a)\n", "(key_a_word, key_a_wrap), score" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "freslitiacehartaypoinseailinthareagevtiaeceyiaepptestwboarinartayptwmthhegthctpifktwupenwinartasenhfsipaodartsewiomthhegtpstaayunliktlyfoinfiwtnfthoidigustyousjuniosfiprtsfltskcehinvolvtwmigrabtcosarfrtfkingrtsbefkgsounwenwbenkeffounahareaihellimtenacrtniheiwarthousftcehflohtaoromtbuaarinkingebouaiaiemnoahustiamekthhtnhtaoarinkhrtceharthousftartmthhegthhaillkttpfomingenwiemguthhingareahrtrehgonthoartstmuhabthomtoartshousftdosousinatlligtnfthomtaringihboartsingmtebouaartaontodartmthhegthcrywothousenaegonihakttpaeunainguhebouaousellithiaihnoaliktartaringhctestbtingehktwaobiwdosestaringhctestliktlyaoriwtdsomontenoartsedatsellenyceyidolloctwontodousdstnfrfollteguthaodsitwsifrhasehhtenwceafrtwrtsasyaopifkupartktydsomlofktshttmhaorevtbttnefonifoulwnaflteslyhttcreacehgoingonbuahrtwiwnahttmaobtebltaooptnlofktsenwltdaobviouhlyuphtaedatshrtrewgontiaookelookealofktsiarehedelhtbefkhoiemguthhingartmontycehaektnbuanoaringpsoviwtwintxfrengtfoulwyouwigesounwciaryousfonaefahinartbsiaihrenwsuhhientmbehhithenwhttidartyestgtaaingarthemthosaodfommunifeaionhmeybtyoufoulwcesnartmbyartceywitelfrtmihatnpsojtfacehartfowtnemtdosartnezieaombombtddosairewesuninciarartmwusingartcescrtnctctstasyingaokttpartisrenwhoddartrtevyceatshupplystmtmbtsartbombinginvtmoskareacehuhosseartsousnoswifellithenyceyiaihhaillaophtfstahoartdefaouspsoaegonihaknochebouaiaihhignidifenaihuhptfaartktyaoarihcroltmyhatsylithinartisiwtnaiayidctkntccroartyctstcoskingdosctmigrabtebltaodigustouacreaartyestupaoontlehaaringefaingonerunfriaookelookeahomtodartdstnfrwtewwsophedatsartdsitwsifrhasehhtinfiwtnaenwdounwarteaaefrtwfommunifeaionirevtnarewaimtaofsefkiabuaiarinkiameybtevigtntstegeinbuairevtnarewefrenftaoasybebbegthasifkoniaytagivtiaaoyousblefkfrembtsenwhttcreaartyfenmektodiaellartbtharessy\n" ] } ], "source": [ "print(keyword_decipher(c6a, key_a_word, wrap_alphabet=key_a_wrap))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'icrdvsfshmrghcfhpzysxdrhsvsxfghcrhnrkfshrmrpshrzzfrdflayhcsxhcfhpzflwfggrnfgmfzsiufljzrxlsxhcfhdrxgidszhyehcfdrlsywfggrnfzdfhhpjxvsufvpiysxislfxifgysesnjdfpyjdtjxsydiszcfdivfdumrgsxkyvkflwsnchafmydhcicfiusxncfdariundyjxlrxlarxuriiyjxhghcrhsgrvvswfrxhmcfxsgrslhcfgyjdifmrgivygfhycywfajhhcsxusxnrayjhshsrwxyhgjdfshwrufggfxgfhyhcsxugcfmrghcfgyjdifhcfwfggrnfgghsvvuffziywsxnrxlsrwnjfggsxnhcrhgcfcrgnyxfgyhcfdfwjghafgywfyhcfdgyjdifeydyjdsxhfvvsnfxifgywfhcsxnsgayhcfdsxnwfrayjhhcfhyxfyehcfwfggrnfgmcplyfgyjdrxhrnyxsghuffzhrjxhsxnjgrayjhyjdrvvsfgshsgxyhvsufhcfhcsxngmfrdfafsxnrguflhyasleydrdfhcsxngmfrdfvsufvphycslfedywyxfrxyhcfdrehfdrvvrxpmrpseyvvymflyxfyeyjdedfxiciyvvfrnjfghyedsfldsicghdrggfrxlmrhicflcfdhdphyzsiujzhcfufpedywvyiufdgffwghycrkfaffxriyxsiyjvlxhivfrdvpgffmcrhmrgnysxnyxajhgcflslxhgffwhyafravfhyyzfxvyiufdrxlvfehyaksyjgvpjzgfhrehfdgcfcrlnyxfshyyurvyyurhvyiufdshcrgrervgfariugysrwnjfggsxnhcfwyxfpmrghrufxajhxyhcsxnzdykslflsxfoicrxnfiyjvlpyjlsnrdyjxlmshcpyjdiyxhrihgsxhcfadshsgcrxldjggsrxfwarggsfgrxlgffsehcfprdfnfhhsxnhcfgrwfgydhyeiywwjxsirhsyxgwrpafpyjiyjvlmrdxhcfwaphcfmrplsfrvicfwsghfxzdytfihmrghcfiylfxrwfeydhcfxrqsrhywaywafeeydhscrlrdjxsxmshchcfwljdsxnhcfmrdmcfxmfmfdfhdpsxnhyuffzhcfsdcrxlgyeehcfcfrkpmrhfdgjzzvpdfwfwafdhcfaywasxnsxkfwyduhcrhmrgjgyddrhcfdyjdxydlsirvvsfgrxpmrpshsgghsvvhyzgfidfhgyhcferihyjdzdyhrnyxsghuxymgrayjhshsggsnxsesirxhsgjgzfihhcfufphyhcsgmcyvfwpghfdpvsfgsxhcfsdslfxhshpsemfuxfmmcyhcfpmfdfmydusxneydmfwsnchafravfhyesnjdfyjhmcrhhcfprdfjzhyyxfvrghhcsxnrihsxnyxrcjxicshyyurvyyurhgywfyehcfedfxiclfrlldyzgrehfdhcfedsfldsicghdrggfsxislfxhrxleyjxlhcfrhhricfliywwjxsirhsyxscrkfxhcrlhswfhyidriushajhshcsxushwrpafrksnfxfdfrnrsxajhscrkfxhcrlricrxifhyhdparaarnfghdsiuyxshpfhnskfshhypyjdavriuicrwafdrxlgffmcrhhcfpirxwrufyeshrvvhcfafghcrddp'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c6a" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'CHARLIE IT WAS THE TYPO IN RAT I LINES THAT GAVE IT AWAY IT APPEARED BOTH IN THE TYPED MESSAGES WE PICKED UP AND IN THE TRANSCRIPT OF THE RADIO MESSAGE PRETTY UNLIKELY COINCIDENCE SO I FIGURE YOUR JUNIOR CIPHER CLERK WAS INVOLVED MIGHT BE WORTH CHECKING HER BACKGROUND AND BANK ACCOUNTS THAT IS ALL I MEANT WHEN I SAID THE SOURCE WAS CLOSE TO HOME BUT THINKING ABOUT IT I AM NOT SURE IT MAKES SENSE TO THINK SHE WAS THE SOURCE THE MESSAGES STILL KEEP COMING AND I AM GUESSING THAT SHE HAS GONE SO THERE MUST BE SOME OTHER SOURCE FOR OUR INTELLIGENCE SOMETHING IS BOTHERING ME ABOUT THE TONE OF THE MESSAGES WHY DOES OUR ANTAGONIST KEEP TAUNTING US ABOUT OUR ALLIES IT IS NOT LIKE THE THINGS WE ARE BEING ASKED TO BID FOR ARE THINGS WE ARE LIKELY TO HIDE FROM ONE ANOTHER AFTER ALL ANYWAY I FOLLOWED ONE OF OUR FRENCH COLLEAGUES TO FRIEDRICHSTRASSE AND WATCHED HER TRY TO PICKUP THE KEY FROM LOCKER SEEMS TO HAVE BEEN A CON I COULDNT CLEARLY SEE WHAT WAS GOING ON BUT SHE DIDNT SEEM TO BE ABLE TO OPEN LOCKER AND LEFT OBVIOUSLY UPSET AFTER SHE HAD GONE I TOOK A LOOK AT LOCKER IT HAS A FALSE BACK SO I AM GUESSING THE MONEY WAS TAKEN BUT NOTHING PROVIDED IN EXCHANGE COULD YOU DIG AROUND WITH YOUR CONTACTS IN THE BRITISH AND RUSSIAN EMBASSIES AND SEE IF THEY ARE GETTING THE SAME SORT OF COMMUNICATIONS MAYBE YOU COULD WARN THEM BY THE WAY DIE ALCHEMIST EN PROJECT WAS THE CODENAME FOR THE NAZI ATOM BOMB EFFORT I HAD A RUN IN WITH THEM DURING THE WAR WHEN WE WERE TRYING TO KEEP THEIR HANDS OFF THE HEAVY WATER SUPPLY REMEMBER THE BOMBING IN VE MORK THAT WAS US OR RATHER OUR NORDIC ALLIES ANYWAY IT IS STILL TOP SECRET SO THE FACT OUR PROTAGONIST KNOWS ABOUT IT IS SIGNIFICANT I SUSPECT THE KEY TO THIS WHOLE MYSTERY LIES IN THEIR IDENTITY IF WE KNEW WHO THEY WERE WORKING FOR WE MIGHT BE ABLE TO FIGURE OUT WHAT THEY ARE UP TO ONE LAST THING ACTING ON A HUNCH I TOOK A LOOK AT SOME OF THE FRENCH DEAD DROPS AFTER THE FRIEDRICHSTRASSE INCIDENT AND FOUND THE ATTACHED COMMUNICATION I HAVENT HAD TIME TO CRACK IT BUT I THINK IT MAYBE AVI GENERE AGAIN BUT I HAVENT HAD A CHANCE TO TRY BABBAGE STRICK ON IT YET GIVE IT TO YOUR BLACK CHAMBER AND SEE WHAT THEY CAN MAKE OF IT ALL THE BEST HARRY'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "translations = {'c': 'H', 'r': 'A', 'd': 'R', 'p': 'Y', 'i': 'C', 'd': 'R', 'v': 'L', 's': 'I', 'f': 'E', \n", " 'h': 'T', 'a': 'B', 'g': 'S', 'm': 'W', 'y': 'O', 'z': 'P', 'n': 'G', 'j': 'U', 't': 'J',\n", " 'x': 'N', 'k': 'V', 'l': 'D', 'w': 'M', 'u': 'K', 'e': 'F', 'q': 'Z', 'o': 'X'}\n", "translation_table = ''.maketrans(translations)\n", "plaintext = ' '.join(segment(c6a.translate(translation_table)))\n", "plaintext" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'BHRFESTCUVDWGXYZAIJKLMNOP'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "''.join(translations[l] for l in sorted(translations))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'railfencstuvwxyzdghjkmopq'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inverted_translations = {translations[a]: a for a in translations}\n", "''.join(inverted_translations[l] for l in sorted(inverted_translations))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'railfencstuvwxyzbdghjkmopq'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "keyword_cipher_alphabet_of('railfences', wrap_alphabet=KeywordWrapAlphabet.from_last)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "charlie it was the typo in rat i lines that gave it away it appeared both in the typed messages we picked up and in the transcript of the radio message pretty unlikely coincidence so i figure your junior cipher clerk was involved might be worth checking her background and bank accounts that is all i meant when i said the source was close to home but thinking about it i am not sure it makes sense to think she was the source the messages still keep coming and i am guessing that she has gone so there must be some other source for our intelligence something is bothering me about the tone of the messages why does our antagonist keep taunting us about our allies it is not like the things we are being asked to bid for are things we are likely to hide from one another after all anyway i followed one of our french colleagues to friedrichstrasse and watched her try to pickup the key from locker seems to have been a con i couldnt clearly see what was going on but she didnt seem to be able to open locker and left obviously upset after she had gone i took a look at locker it has a false back so i am guessing the money was taken but nothing provided in exchange could you dig around with your contacts in the british and russian embassies and see if they are getting the same sort of communications maybe you could warn them by the way die alchemist en project was the codename for the nazi atom bomb effort i had a run in with them during the war when we were trying to keep their hands off the heavy water supply remember the bombing in ve mork that was us or rather our nordic allies anyway it is still top secret so the fact our protagonist knows about it is significant i suspect the key to this whole mystery lies in their identity if we knew who they were working for we might be able to figure out what they are up to one last thing acting on a hunch i took a look at some of the french dead drops after the friedrichstrasse incident and found the attached communication i havent had time to crack it but i think it maybe avi genere again but i havent had a chance to try babbage strick on it yet give it to your black chamber and see what they can make of it all the best harry\n" ] } ], "source": [ "print(' '.join(segment(keyword_decipher(c6a, 'railfences', wrap_alphabet=KeywordWrapAlphabet.from_last))))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('kremlin', -908.5396262316657)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "key_b, score = vigenere_frequency_break(c6b)\n", "key_b, score" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the americans have robbed you rather than trying to outbid you they got to the lockers first and arranged to steal your money and the valuable intelligence i provided for you they can not be trusted none of your allies can be trusted they believe that they can cheat you but they do not understand that you can only cheat in a game and this is not a game if you try to playa game of chess like your allies we will find ourselves in a stalemate you have been warned so let us start again i can let you have the address of another safehouse at a small discount on our original price and i will include the identity of a british double agent working in your embassy shall we say four hundred thousand francs to be paid directly to an account of my choosing if you want to know more about the treachery of your so called friends then let us meet in the park by the british embassy on friday at eleven\n" ] } ], "source": [ "print(' '.join(segment(sanitise(vigenere_decipher(sanitise(c6b), key_b)))))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('railfencstuvwxyzbdghjkmopq', -5965.858211099189)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulated_annealing_break(c6a, fitness=Ptrigrams)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'charlieitwasthetypoinratilinesthatgaveitawayitappearedbothinthetypedmessageswepickedupandinthetranscriptoftheradiomessageprettyunlikelycoincidencesoifigureyourjuniorcipherclerkwasinvolvedmightbeworthcheckingherbackgroundandbankaccountsthatisallimeantwhenisaidthesourcewasclosetohomebutthinkingaboutitiamnotsureitmakessensetothinkshewasthesourcethemessagesstillkeepcomingandiamguessingthatshehasgonesotheremustbesomeothersourceforourintelligencesomethingisbotheringmeaboutthetoneofthemessageswhydoesourantagonistkeeptauntingusaboutouralliesitisnotlikethethingswearebeingaskedtobidforarethingswearelikelytohidefromoneanotherafterallanywayifollowedoneofourfrenchcolleaguestofriedrichstrasseandwatchedhertrytopickupthekeyfromlockerseemstohavebeenaconicouldntclearlyseewhatwasgoingonbutshedidntseemtobeabletoopenlockerandleftobviouslyupsetaftershehadgoneitookalookatlockerithasafalsebacksoiamguessingthemoneywastakenbutnothingprovidedinexchangecouldyoudigaroundwithyourcontactsinthebritishandrussianembassiesandseeiftheyaregettingthesamesortofcommunicationsmaybeyoucouldwarnthembythewaydiealchemistenprojectwasthecodenameforthenaziatombombeffortihadaruninwiththemduringthewarwhenweweretryingtokeeptheirhandsofftheheavywatersupplyrememberthebombinginvemorkthatwasusorratherournordicalliesanywayitisstilltopsecretsothefactourprotagonistknowsaboutitissignificantisuspectthekeytothiswholemysteryliesintheiridentityifweknewwhotheywereworkingforwemightbeabletofigureoutwhattheyareuptoonelastthingactingonahunchitookalookatsomeofthefrenchdeaddropsafterthefriedrichstrasseincidentandfoundtheattachedcommunicationihaventhadtimetocrackitbutithinkitmaybeavigenereagainbutihaventhadachancetotrybabbagestrickonityetgiveittoyourblackchamberandseewhattheycanmakeofitallthebestharry'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "keyword_decipher(c6a, 'railfencstuvwxyzbdghjkmopq')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 1 }