{ "cells": [ { "cell_type": "code", "execution_count": 14, "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 support.utilities import *\n", "from support.text_prettify import *\n", "from support.language_models import *\n", "\n", "c4a = open('4a.ciphertext').read()\n", "c4b = open('4b.ciphertext').read()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(('reanimates', ), -911.6411317751041)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(key_a_word, key_a_wrap), score = keyword_break_mp(c4a)\n", "(key_a_word, key_a_wrap), score" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gharlie, the attaghew note fas ledt in one od my weaw wrops last nibht anw appears to ce drom our mysterious sourge. it gontains tfo really sibnidigant pieges od intellibenge. the dirst is that the rieghswoktor mibht not reder to an inwiviwual adter all. it seems to ce the gowename dor the orbanization runninb the ratlines. aggorwinb to my other sourges this is a gollegtion od routes, abents anw sade houses usew to transport nazi sympathisers anw far griminals out od bermany anw on to south ameriga. fe have knofn that sugh an orbanisation exists singe the enw od the far, cut this is the dirst time i have seen it namew. the other piege od indormation is mugh more suctle. i am cebinninb to fonwer id our sourge is gloser to home than fe haw realisew. anw i am not rederrinb to drangois! see id you gan spot the tfo thinbs i notigew. cy the fay, fho transgricew the rawio intergept you sent me last feek? harry\n", "\n" ] } ], "source": [ "print(keyword_decipher(c4a, key_a_word, wrap_alphabet=key_a_wrap))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a': 'c',\n", " 'b': 'o',\n", " 'c': 'p',\n", " 'd': 'q',\n", " 'e': 'b',\n", " 'f': 'r',\n", " 'g': 's',\n", " 'h': 't',\n", " 'i': 'e',\n", " 'j': 'u',\n", " 'k': 'v',\n", " 'l': 'w',\n", " 'm': 'f',\n", " 'n': 'd',\n", " 'o': 'x',\n", " 'p': 'y',\n", " 'q': 'z',\n", " 'r': 'a',\n", " 's': 'h',\n", " 't': 'g',\n", " 'u': 'i',\n", " 'v': 'j',\n", " 'w': 'k',\n", " 'x': 'l',\n", " 'y': 'm',\n", " 'z': 'n'}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trans = ''.maketrans(keyword_cipher_alphabet_of(key_a_word, wrap_alphabet=key_a_wrap), string.ascii_lowercase)\n", "t2 = {chr(c): chr(trans[c]) for c in trans}\n", "t2" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('r', 'a'),\n", " ('a', 'b'),\n", " ('t', 'c'),\n", " ('l', 'd'),\n", " ('i', 'e'),\n", " ('n', 'f'),\n", " ('e', 'g'),\n", " ('s', 'h'),\n", " ('u', 'i'),\n", " ('v', 'j'),\n", " ('w', 'k'),\n", " ('x', 'l'),\n", " ('y', 'm'),\n", " ('z', 'n'),\n", " ('b', 'o'),\n", " ('c', 'p'),\n", " ('d', 'q'),\n", " ('f', 'r'),\n", " ('g', 's'),\n", " ('h', 't'),\n", " ('j', 'u'),\n", " ('k', 'v'),\n", " ('m', 'w'),\n", " ('o', 'x'),\n", " ('p', 'y'),\n", " ('q', 'z')]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t2['t'] = 'c'\n", "t2['a'] = 'b'\n", "t2['e'] = 'g'\n", "\n", "t2['l'] = 'd'\n", "t2['n'] = 'f'\n", "t2['m'] = 'w'\n", "\n", "sorted(((c, t2[c]) for c in t2), key=lambda p: p[1])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "charlie, the attached note was left in one of my dead drops last night and appears to be from our mysterious source. it contains two really significant pieces of intelligence. the first is that the riechsdoktor might not refer to an individual after all. it seems to be the codename for the organization running the ratlines. according to my other sources this is a collection of routes, agents and safe houses used to transport nazi sympathisers and war criminals out of germany and on to south america. we have known that such an organisation exists since the end of the war, but this is the first time i have seen it named. the other piece of information is much more subtle. i am beginning to wonder if our source is closer to home than we had realised. and i am not referring to francois! see if you can spot the two things i noticed. by the way, who transcribed the radio intercept you sent me last week? harry\n", "\n" ] } ], "source": [ "print(keyword_decipher(c4a, 'ratlines', wrap_alphabet=key_a_wrap))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(('francois', ), -1082.7018217012803)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(key_b_word, key_b_wrap), score =keyword_break_mp(c4b)\n", "(key_b_word, key_b_wrap), score" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the french maybe your allies but they are not your friends they plan to infiltrate the rat i lines and to try to turn the high value targets for themselves they have a particular interest in nazi scientists from the die alchemist en project if you want to break the reichs doktor network before they can do so take care not to share any intelligence with them you have been warned i think it is time to begin negotiations i have a number in mind and i think once you know what i am offering you will find it very reasonable as a sign of good faith ioffer you the following information one of the local rat i line coordinators will be leaving the us sector tomorrow night in a black limousine under the backseat of his car you will find hidden a juniors s officer who is trying to escape and in the trunk you will find a number of papers relating to stolen artworks that he hopes to trade to the french for his freedom you might want to consider carefully whether you can trust your friend charlie with this information after all her husband francois is french\n" ] } ], "source": [ "print(' '.join(segment(sanitise(keyword_decipher(c4b, key_b_word, wrap_alphabet=key_b_wrap)))))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'reanimtsuvwxyzbcdfghjklopq'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "keyword_cipher_alphabet_of(key_a_word, wrap_alphabet=key_a_wrap)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('firzuyhgjkmopqatlnesvwxbcd', -2435.9305949573036)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "monoalphabetic_break_hillclimbing(c4a, \n", " plain_alphabet=keyword_cipher_alphabet_of(key_a_word, wrap_alphabet=key_a_wrap),\n", " fitness=Ptrigrams)" ] }, { "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 }