Updated notebooks for new library organisation
[cipher-tools.git] / 2015 / 2015-challenge7.ipynb
index 4fb1a18071eab3fbcfd472ce76f5f09ac550c291..dcfa20581c0e1c175d7284969cf2f800919684a2 100644 (file)
@@ -2,10 +2,8 @@
  "cells": [
   {
    "cell_type": "code",
-   "execution_count": 2,
-   "metadata": {
-    "collapsed": false
-   },
+   "execution_count": 12,
+   "metadata": {},
    "outputs": [],
    "source": [
     "import os,sys,inspect\n",
     "import matplotlib.pyplot as plt\n",
     "%matplotlib inline\n",
     "\n",
-    "from cipherbreak import *\n",
+    "from cipher.column_transposition import *\n",
+    "from cipher.keyword_cipher import *\n",
+    "from cipher.amsco import *\n",
+    "from support.utilities import *\n",
+    "from support.text_prettify import *\n",
+    "from support.language_models import *\n",
     "\n",
     "c7a = sanitise(open('7a.ciphertext').read())\n",
     "c7b = sanitise(open('7b.ciphertext').read())"
   },
   {
    "cell_type": "code",
-   "execution_count": 8,
-   "metadata": {
-    "collapsed": false
-   },
+   "execution_count": 2,
+   "metadata": {},
    "outputs": [
     {
      "data": {
@@ -35,7 +36,7 @@
        "(('annmarie', <KeywordWrapAlphabet.from_largest: 3>), -1865.8708508162845)"
       ]
      },
-     "execution_count": 8,
+     "execution_count": 2,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 9,
-   "metadata": {
-    "collapsed": false
-   },
+   "execution_count": 3,
+   "metadata": {},
    "outputs": [
     {
      "name": "stdout",
@@ -66,9 +65,8 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 10,
+   "execution_count": 4,
    "metadata": {
-    "collapsed": false,
     "scrolled": true
    },
    "outputs": [
        " 'Z': 'n'}"
       ]
      },
-     "execution_count": 10,
+     "execution_count": 4,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 3,
-   "metadata": {
-    "collapsed": false
-   },
+   "execution_count": 5,
+   "metadata": {},
    "outputs": [
     {
      "data": {
        "'charlie i think i know what is going on but i need to check a few things before i report we may have an opportunity here i checked the cipher clerks background and it turns out she is white russian her family left moscow in but she has relatives in the gulag at perm she clearly has no love for the soviet government so i am still not sure who she was working for but i think this is key intelligence in the meantime i have been watching the brits they seem to have been in contact with our friends in the reichs doktor and they in turn have been watching the french it seems like we are all working against one another which i really didnt expect and given what we read in the french document last week i dont think that is a coincidence my own guess is that the russians know what is going on and that our best hope of uncovering it is to break into their hq and try to find something there unfortunately according to my source yuri they have taken to using a new cipher solitaire for archive storage of top secret files so even if we manage to steal the relevant file it will take alot of computing to break the cipher i attach a brief message from yuri encrypted using an amsco cipher keyword length is six in which he describes the cipher it is very clever simple to implement but a devil to crack and my one hope is that we can also find the cipher key while in the hq or atleast part of it i plan to enter in two weeks on december sixteenth the russians are hosting a large propaganda event as part of the international ebau ausstellung with leading politburo members in attendance most of the security team will be occupied with that and hq security will be relatively light with luck i will get in and out with the files we need that night and then we can get to the bottom of the whole reichs doktor stratagem all the best harry'"
       ]
      },
-     "execution_count": 3,
+     "execution_count": 5,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 12,
-   "metadata": {
-    "collapsed": false
-   },
+   "execution_count": 6,
+   "metadata": {},
    "outputs": [
     {
      "data": {
        "'oapqbrsteuvdwfxyzcghijklmn'"
       ]
      },
-     "execution_count": 12,
+     "execution_count": 6,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 13,
-   "metadata": {
-    "collapsed": false
-   },
+   "execution_count": 7,
+   "metadata": {},
    "outputs": [
     {
      "data": {
        "'BERLINSTUVWXYZACDFGHJKMOPQ'"
       ]
      },
-     "execution_count": 13,
+     "execution_count": 7,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
-   "metadata": {
-    "collapsed": false
-   },
+   "execution_count": 8,
+   "metadata": {},
    "outputs": [
     {
      "data": {
        "'berlinstuvwxyzacdfghjkmopq'"
       ]
      },
-     "execution_count": 4,
+     "execution_count": 8,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
-   "metadata": {
-    "collapsed": false
-   },
+   "execution_count": 17,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "('berlinstudwxyzacvfghjkmopq', -5028.125895049484)"
+      ]
+     },
+     "execution_count": 17,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "k7a, fit7a = simulated_annealing_break(c7a, fitness=Ptrigrams)\n",
+    "k7a, fit7a"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 21,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "charlie i think i know what is going on but i need to check a few things before i report we may have\n",
+      "an opportunity here i checked the cipher clerks background and it turns out she is white russian her\n",
+      "family left moscow in but she has relatives in the gulag at perm she clearly has no love for the\n",
+      "soviet government so i am still not sure who she was working for but i think this is key\n",
+      "intelligence in the meantime i have been watching the brits they seem to have been in contact with\n",
+      "our friends in the reichs doktor and they in turn have been watching the french it seems like we are\n",
+      "all working against one another which i really didnt expect and given what we read in the french\n",
+      "document last week i dont think that is a coincidence my own guess is that the russians know what is\n",
+      "going on and that our best hope of uncovering it is to break into their hj and try to find something\n",
+      "there unfortunately according to my source yuri they have taken to using a new cipher solitaire for\n",
+      "archive storage of top secret files so even if we manage to steal the relevant file it will take\n",
+      "alot of computing to break the cipher i attach a brief message from yuri encrypted using an amsco\n",
+      "cipher keyword length is six in which he describes the cipher it is very clever simple to implement\n",
+      "but a devil to crack and my one hope is that we can also find the cipher key while in the hj or\n",
+      "atleast part of it i plan to enter in two weeks on december sixteenth the russians are hosting a\n",
+      "large propaganda event as part of the international ebau ausstellung with leading politburo members\n",
+      "in attendance most of the security team will be occupied with that and hj security will be\n",
+      "relatively light with luck i will get in and out with the files we need that night and then we can\n",
+      "get to the bottom of the whole reichs doktor stratagem all the best harry\n"
+     ]
+    }
+   ],
+   "source": [
+    "print(lcat(tpack(segment(keyword_decipher(c7a, k7a)))))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {},
    "outputs": [
     {
      "name": "stdout",
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
-   "metadata": {
-    "collapsed": false
-   },
+   "execution_count": 10,
+   "metadata": {},
    "outputs": [
     {
      "data": {
        " (5, 4, 3, 2, 1, 0): (5, 4, 3, 2, 1, 0)}"
       ]
      },
-     "execution_count": 6,
+     "execution_count": 10,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
-   "metadata": {
-    "collapsed": false
-   },
+   "execution_count": 13,
+   "metadata": {},
    "outputs": [
     {
      "data": {
        " -1437.9908206760847)"
       ]
      },
-     "execution_count": 7,
+     "execution_count": 13,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 8,
-   "metadata": {
-    "collapsed": false
-   },
+   "execution_count": 14,
+   "metadata": {},
    "outputs": [
     {
      "data": {
        "  AmscoSlice(index=407, start=611, end=612)]]"
       ]
      },
-     "execution_count": 8,
+     "execution_count": 14,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 9,
+   "execution_count": 15,
    "metadata": {
-    "collapsed": false,
     "scrolled": true
    },
    "outputs": [
        " 'statisticians']"
       ]
      },
-     "execution_count": 9,
+     "execution_count": 15,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 10,
-   "metadata": {
-    "collapsed": false
-   },
+   "execution_count": 16,
+   "metadata": {},
    "outputs": [
     {
      "name": "stdout",
   {
    "cell_type": "code",
    "execution_count": null,
-   "metadata": {
-    "collapsed": true
-   },
+   "metadata": {},
    "outputs": [],
    "source": []
   }
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.4.3"
+   "version": "3.6.3"
   }
  },
  "nbformat": 4,
- "nbformat_minor": 0
+ "nbformat_minor": 1
 }