Updated notebooks for new library organisation
[cipher-tools.git] / 2016 / 2016-challenge8b.ipynb
index fc0d8511b3530f96585e71060b18b16ccae07448..5a4982e880990824c665e51f0a030835994b8f3a 100644 (file)
@@ -2,7 +2,7 @@
  "cells": [
   {
    "cell_type": "code",
-   "execution_count": 1,
+   "execution_count": 26,
    "metadata": {},
    "outputs": [],
    "source": [
     "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 support.utilities import *\n",
+    "from support.text_prettify import *\n",
+    "from support.language_models import *\n",
+    "from logger import *\n",
     "\n",
     "c8a = open('8a.ciphertext').read()\n",
     "c8b = open('8b.ciphertext').read()\n",
@@ -27,7 +32,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 2,
+   "execution_count": 27,
    "metadata": {},
    "outputs": [
     {
        "3226615"
       ]
      },
-     "execution_count": 2,
+     "execution_count": 27,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
-    "wap = open('../war-and-peace.txt').read()\n",
+    "wap = open('../support/war-and-peace.txt').read()\n",
     "len(wap)"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 3,
+   "execution_count": 28,
    "metadata": {},
    "outputs": [
     {
@@ -57,7 +62,7 @@
        "['10', '00', '00', '01', '00', '111', '000', '100', '010', '101']"
       ]
      },
-     "execution_count": 3,
+     "execution_count": 28,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -69,7 +74,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
+   "execution_count": 29,
    "metadata": {},
    "outputs": [
     {
@@ -87,7 +92,7 @@
        " ['0010', '0010', '0100', '0100', '0000']]"
       ]
      },
-     "execution_count": 4,
+     "execution_count": 29,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": 30,
    "metadata": {},
    "outputs": [
     {
        " ['00000', '00110', '11000', '00000']]"
       ]
      },
-     "execution_count": 5,
+     "execution_count": 30,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
+   "execution_count": 31,
    "metadata": {},
    "outputs": [
     {
        "(25, 0)"
       ]
      },
-     "execution_count": 6,
+     "execution_count": 31,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
+   "execution_count": 32,
    "metadata": {},
    "outputs": [
     {
        "'bi vjr dom omdabhx qzbp qzmh b gdv ym amda dxdbh ebffma qtbnm hjt yv qzm kjpq abxbqdf prosmbffdhnm p'"
       ]
      },
-     "execution_count": 7,
+     "execution_count": 32,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 8,
+   "execution_count": 33,
    "metadata": {},
    "outputs": [
     {
        "5325"
       ]
      },
-     "execution_count": 8,
+     "execution_count": 33,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 9,
+   "execution_count": 34,
    "metadata": {},
    "outputs": [
     {
        "5325"
       ]
      },
-     "execution_count": 9,
+     "execution_count": 34,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 10,
+   "execution_count": 35,
    "metadata": {},
    "outputs": [
     {
        "5325"
       ]
      },
-     "execution_count": 10,
+     "execution_count": 35,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 11,
+   "execution_count": 36,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "(-32687.168128732188, -20426.14140719279, -30376.82045021536)"
+       "(-32687.168128732188, -20426.14140719279, -30689.376594117315)"
       ]
      },
-     "execution_count": 11,
+     "execution_count": 36,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 12,
+   "execution_count": 37,
    "metadata": {},
    "outputs": [
     {
        "40669.896112351824"
       ]
      },
-     "execution_count": 12,
+     "execution_count": 37,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 13,
+   "execution_count": 38,
    "metadata": {},
    "outputs": [
     {
        "3476.059496782207"
       ]
      },
-     "execution_count": 13,
+     "execution_count": 38,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 14,
+   "execution_count": 39,
    "metadata": {},
    "outputs": [
     {
        "2391.2432630309268"
       ]
      },
-     "execution_count": 14,
+     "execution_count": 39,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 15,
+   "execution_count": 40,
    "metadata": {},
    "outputs": [
     {
        "2.788846178301471e+22"
       ]
      },
-     "execution_count": 15,
+     "execution_count": 40,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 16,
+   "execution_count": 41,
    "metadata": {},
    "outputs": [
     {
        "0.951229424500714"
       ]
      },
-     "execution_count": 16,
+     "execution_count": 41,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 17,
+   "execution_count": 42,
    "metadata": {},
    "outputs": [
     {
        "1738.0297483911036"
       ]
      },
-     "execution_count": 17,
+     "execution_count": 42,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 18,
+   "execution_count": 43,
    "metadata": {},
    "outputs": [
     {
        "'etoainhsrdlumwycfgpbvkxjqz'"
       ]
      },
-     "execution_count": 18,
+     "execution_count": 43,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 19,
+   "execution_count": 44,
    "metadata": {},
    "outputs": [
     {
        "'mqdbjhopzafrntgxvkiyseuclw'"
       ]
      },
-     "execution_count": 19,
+     "execution_count": 44,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 20,
+   "execution_count": 45,
    "metadata": {
     "scrolled": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 21,
+   "execution_count": 46,
    "metadata": {},
    "outputs": [
     {
        "-21381.986189863124"
       ]
      },
-     "execution_count": 21,
+     "execution_count": 46,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 22,
+   "execution_count": 47,
    "metadata": {},
    "outputs": [
     {
        "'dajoklynpigqemhstuvwxfzcbr'"
       ]
      },
-     "execution_count": 22,
+     "execution_count": 47,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 23,
+   "execution_count": 48,
    "metadata": {},
    "outputs": [],
    "source": [
   },
   {
    "cell_type": "code",
-   "execution_count": 30,
+   "execution_count": 49,
    "metadata": {
     "scrolled": true
    },
        "('mqjdbhzpoafrgtvnixkyseuclw', -17922.293053151883)"
       ]
      },
-     "execution_count": 30,
+     "execution_count": 49,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 31,
+   "execution_count": 50,
    "metadata": {
     "scrolled": true
    },
        "'if you are reading this then i may be dead again killed twice now by the post digital surveillance syndicate if i am still alive then you may be my only hope if they have killed me then this letter will have been distributed in unencrypted form to all the social media sites and all the news agencies in the world their name will be known to everyone and the story of what they tried to do will be headline news if not then my hope is that you will keep this quiet for now for your sake as well as for mine my name is jamelia dalmar and i am partly responsible for this mess this letter is my attempt to explain my role in what happened in the hope that nothing like it will ever happen again the syndicate was a government initiative to prepare for a world in which encryption will be so secure that no one can crack it that world is almost here and the quantum technology revolution is likely to give birth to it soon if nothing changes privacy will be absolute and so will secrecy how you feel about that depends a lot on who you are and who you think has most to gain i was neutral but when my friend martin suggested that we could adapt some of the technology we developed for the ligo project i was excited at the possibilities our plan was to build behavioural template filters to track group activities on the web originally we hoped to model the spread of ideas martin was interested in the marketing angle and saw it as a way of selling products by understanding how memes propagate i was more interested in understanding how political ideas spread through a population it was a million miles from our real areas of expertise and i should have realised the dangers much earlier the war games department at dynamix saw the potential for this tool to be used as a twenty first century version of sigint allowing intelligence agencies and governments to track small group activities by matching online behaviour to known templates small groups including terrorist cells the pds syndicate had agents at just about every arms manufacturer in the west including dynamix when they became aware of our work they arranged for dynamix to recruit us on their intern scheme it was an amazing time we had every resource we could want and at first the freedom to do what we wanted gently they brought us round to working on small group dynamic tracking and following some of the recent tragedies i worked out for myself the potential of our software to detect and prevent terrorism but we needed data we called it the temp bank a database of behavioural templates which we would use to match against live online traffic to highlight areas of interest no one had ever worked on this sort of pattern based tracking before so we had no data on which to base the templates modelling communication patterns between members of a terrorist group without the templates it was impossible to carry out real time monitoring as the data rate was far too high for live analysis i raised my concerns with the head of research at dynamix and that was when i was introduced to someone from the syndicate they promised us data based on mathematical models developed by the department of defence and the project continued it was more successful than we had dared to hope a stream of high quality data filled the temp bank and the algorithm began to spot real world examples that closely matched the templates of course a lot of that was just chatter there is a world of difference between discussing and carrying out an attack and no security service can investigate every deluded fantasy online in order to find the one real plot dynamix were excited by what we had achieved but as the results rolled in they demanded more accuracy and for that we needed much better data the syndicate were happy to oblige and that is when i realised the how far they were prepared to go i tried to convince myself that the syndicate had access to a large database of existing communications that they were using to extract behavioural templates but the more i thought about it the more unlikely it seemed our software was seeing real world attack patterns that were suspiciously similar almost choreographed and i began to wonder if the pds syndicate were not just tracking terrorist activity but were encouraging it among groups that they had penetrated in order to build an encyclopaedia of behavioural templates that could be used to spot future attacks before they happened the syndicate had an achilles heel it was not possible to coordinate their research programme without leaving their own digital footprint martin and i refocused our efforts on tracking them down and found all the evidence we needed but they were the experts not us and we must have triggered an alarm somewhere they gave us one choice my death was now inevitable the only question was whether or not it would be faked once i was officially dead i would have no protection but we were valuable to them and that was the key to our survival i never knew the name of the girl they used to fake my suicide but they did assure me that she had died of natural causes i hope that much was true the syndicate were the real experts in espionage but martin and i had the edge in coding they watched us carefully but we managed to plant a virus in the database engine and it will have been triggered long before you read this it is designed to infect every copy of our algorithm and to encrypt crucial elements of the code rendering it unusable we learned a lot from studying stuxnet and the temp bank will suffer the same fate as the iranian centrifuges now the virus is released we convinced the syndicate to fund a hacker collective in norway and to allow martin to work there undercover as far as they were concerned he will be harvesting baseline data for noise filtering in social media traffic but he will be distributing this file and the virus to as many servers as he can without alerting them martin is good at this he has already hacked a number of back up company and app servers and used them as a distribution mechanism we have also been leaving a trail for the intelligence services we have no reason to believe the governments are connected with this and every reason to believe they may be our best hope of stopping them are we safe probably not or this insurance policy would have remained just that we might be on the run though unable to trigger the dead man switch that was keeping these files safe we do not plan on giving up and we urge you not to either'"
       ]
      },
-     "execution_count": 31,
+     "execution_count": 50,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 26,
+   "execution_count": 51,
    "metadata": {
     "scrolled": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 27,
+   "execution_count": 52,
    "metadata": {},
    "outputs": [
     {
        " 'z': 'w'}"
       ]
      },
-     "execution_count": 27,
+     "execution_count": 52,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 28,
+   "execution_count": 53,
    "metadata": {
     "scrolled": true
    },
        "'if you are reading this then i may be dead again killed twice now by the post digital surveillance syndicate if i am still alive then you may be my only hope if they have killed me then this letter will have been distributed in unencrypted form to all the social media sites and all the news agencies in the world their name will be known to everyone and the story of what they tried to do will be headline news if not then my hope is that you will keep this quiet for now for your sake as well as for mine my name is jamelia dalmar and i am partly responsible for this mess this letter is my attempt to explain my role in what happened in the hope that nothing like it will ever happen again the syndicate was a government initiative to prepare for a world in which encryption will be so secure that no one can crack it that world is almost here and the quantum technology revolution is likely to give birth to it soon if nothing changes privacy will be absolute and so will secrecy how you feel about that depends a lot on who you are and who you think has most to gain i was neutral but when my friend martin suggested that we could adapt some of the technology we developed for the ligo project i was excited at the possibilities our plan was to build behavioural template filters to track group activities on the web originally we hoped to model the spread of ideas martin was interested in the marketing angle and saw it as a way of selling products by understanding how memes propagate i was more interested in understanding how political ideas spread through a population it was a million miles from our real areas of expertise and i should have realised the dangers much earlier the war games department at dynamix saw the potential for this tool to be used as a twenty first century version of sigint allowing intelligence agencies and governments to track small group activities by matching online behaviour to known templates small groups including terrorist cells the pds syndicate had agents at just about every arms manufacturer in the west including dynamix when they became aware of our work they arranged for dynamix to recruit us on their intern scheme it was an amazing time we had every resource we could want and at first the freedom to do what we wanted gently they brought us round to working on small group dynamic tracking and following some of the recent tragedies i worked out for myself the potential of our software to detect and prevent terrorism but we needed data we called it the temp bank a database of behavioural templates which we would use to match against live online traffic to highlight areas of interest no one had ever worked on this sort of pattern based tracking before so we had no data on which to base the templates modelling communication patterns between members of a terrorist group without the templates it was impossible to carry out real time monitoring as the data rate was far too high for live analysis i raised my concerns with the head of research at dynamix and that was when i was introduced to someone from the syndicate they promised us data based on mathematical models developed by the department of defence and the project continued it was more successful than we had dared to hope a stream of high quality data filled the temp bank and the algorithm began to spot real world examples that closely matched the templates of course a lot of that was just chatter there is a world of difference between discussing and carrying out an attack and no security service can investigate every deluded fantasy online in order to find the one real plot dynamix were excited by what we had achieved but as the results rolled in they demanded more accuracy and for that we needed much better data the syndicate were happy to oblige and that is when i realised the how far they were prepared to go i tried to convince myself that the syndicate had access to a large database of existing communications that they were using to extract behavioural templates but the more i thought about it the more unlikely it seemed our software was seeing real world attack patterns that were suspiciously similar almost choreographed and i began to wonder if the pds syndicate were not just tracking terrorist activity but were encouraging it among groups that they had penetrated in order to build an encyclopaedia of behavioural templates that could be used to spot future attacks before they happened the syndicate had an achilles heel it was not possible to coordinate their research programme without leaving their own digital footprint martin and i refocused our efforts on tracking them down and found all the evidence we needed but they were the experts not us and we must have triggered an alarm somewhere they gave us one choice my death was now inevitable the only question was whether or not it would be faked once i was officially dead i would have no protection but we were valuable to them and that was the key to our survival i never knew the name of the girl they used to fake my suicide but they did assure me that she had died of natural causes i hope that much was true the syndicate were the real experts in espionage but martin and i had the edge in coding they watched us carefully but we managed to plant a virus in the database engine and it will have been triggered long before you read this it is designed to infect every copy of our algorithm and to encrypt crucial elements of the code rendering it unusable we learned a lot from studying stuxnet and the temp bank will suffer the same fate as the iranian centrifuges now the virus is released we convinced the syndicate to fund a hacker collective in norway and to allow martin to work there undercover as far as they were concerned he will be harvesting baseline data for noise filtering in social media traffic but he will be distributing this file and the virus to as many servers as he can without alerting them martin is good at this he has already hacked a number of back up company and app servers and used them as a distribution mechanism we have also been leaving a trail for the intelligence services we have no reason to believe the governments are connected with this and every reason to believe they may be our best hope of stopping them are we safe probably not or this insurance policy would have remained just that we might be on the run though unable to trigger the dead man switch that was keeping these files safe we do not plan on giving up and we urge you not to either'"
       ]
      },
-     "execution_count": 28,
+     "execution_count": 53,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 29,
+   "execution_count": 54,
    "metadata": {},
    "outputs": [
     {
        "('mqjdbhzpoafrgtvnixkyseuclw', -17922.293053151883)"
       ]
      },
-     "execution_count": 29,
+     "execution_count": 54,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 32,
+   "execution_count": 55,
    "metadata": {
     "scrolled": true
    },
        "('dynamixzbcefghjklopqrstuvw', -17922.293053151883)"
       ]
      },
-     "execution_count": 32,
+     "execution_count": 55,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 34,
+   "execution_count": 56,
    "metadata": {
     "scrolled": true
    },
        "'if you are reading this then i may be dead again killed twice now by the post digital surveillance syndicate if i am still alive then you may be my only hope if they have killed me then this letter will have been distributed in unencrypted form to all the social media sites and all the news agencies in the world their name will be known to everyone and the story of what they tried to do will be headline news if not then my hope is that you will keep this quiet for now for your sake as well as for mine my name is jamelia dalmar and i am partly responsible for this mess this letter is my attempt to explain my role in what happened in the hope that nothing like it will ever happen again the syndicate was a government initiative to prepare for a world in which encryption will be so secure that no one can crack it that world is almost here and the quantum technology revolution is likely to give birth to it soon if nothing changes privacy will be absolute and so will secrecy how you feel about that depends a lot on who you are and who you think has most to gain i was neutral but when my friend martin suggested that we could adapt some of the technology we developed for the ligo project i was excited at the possibilities our plan was to build behavioural template filters to track group activities on the web originally we hoped to model the spread of ideas martin was interested in the marketing angle and saw it as a way of selling products by understanding how memes propagate i was more interested in understanding how political ideas spread through a population it was a million miles from our real areas of expertise and i should have realised the dangers much earlier the war games department at dynamix saw the potential for this tool to be used as a twenty first century version of sigint allowing intelligence agencies and governments to track small group activities by matching online behaviour to known templates small groups including terrorist cells the pds syndicate had agents at just about every arms manufacturer in the west including dynamix when they became aware of our work they arranged for dynamix to recruit us on their intern scheme it was an amazing time we had every resource we could want and at first the freedom to do what we wanted gently they brought us round to working on small group dynamic tracking and following some of the recent tragedies i worked out for myself the potential of our software to detect and prevent terrorism but we needed data we called it the temp bank a database of behavioural templates which we would use to match against live online traffic to highlight areas of interest no one had ever worked on this sort of pattern based tracking before so we had no data on which to base the templates modelling communication patterns between members of a terrorist group without the templates it was impossible to carry out real time monitoring as the data rate was far too high for live analysis i raised my concerns with the head of research at dynamix and that was when i was introduced to someone from the syndicate they promised us data based on mathematical models developed by the department of defence and the project continued it was more successful than we had dared to hope a stream of high quality data filled the temp bank and the algorithm began to spot real world examples that closely matched the templates of course a lot of that was just chatter there is a world of difference between discussing and carrying out an attack and no security service can investigate every deluded fantasy online in order to find the one real plot dynamix were excited by what we had achieved but as the results rolled in they demanded more accuracy and for that we needed much better data the syndicate were happy to oblige and that is when i realised the how far they were prepared to go i tried to convince myself that the syndicate had access to a large database of existing communications that they were using to extract behavioural templates but the more i thought about it the more unlikely it seemed our software was seeing real world attack patterns that were suspiciously similar almost choreographed and i began to wonder if the pds syndicate were not just tracking terrorist activity but were encouraging it among groups that they had penetrated in order to build an encyclopaedia of behavioural templates that could be used to spot future attacks before they happened the syndicate had an achilles heel it was not possible to coordinate their research programme without leaving their own digital footprint martin and i refocused our efforts on tracking them down and found all the evidence we needed but they were the experts not us and we must have triggered an alarm somewhere they gave us one choice my death was now inevitable the only question was whether or not it would be faked once i was officially dead i would have no protection but we were valuable to them and that was the key to our survival i never knew the name of the girl they used to fake my suicide but they did assure me that she had died of natural causes i hope that much was true the syndicate were the real experts in espionage but martin and i had the edge in coding they watched us carefully but we managed to plant a virus in the database engine and it will have been triggered long before you read this it is designed to infect every copy of our algorithm and to encrypt crucial elements of the code rendering it unusable we learned a lot from studying stuxnet and the temp bank will suffer the same fate as the iranian centrifuges now the virus is released we convinced the syndicate to fund a hacker collective in norway and to allow martin to work there undercover as far as they were concerned he will be harvesting baseline data for noise filtering in social media traffic but he will be distributing this file and the virus to as many servers as he can without alerting them martin is good at this he has already hacked a number of back up company and app servers and used them as a distribution mechanism we have also been leaving a trail for the intelligence services we have no reason to believe the governments are connected with this and every reason to believe they may be our best hope of stopping them are we safe probably not or this insurance policy would have remained just that we might be on the run though unable to trigger the dead man switch that was keeping these files safe we do not plan on giving up and we urge you not to either'"
       ]
      },
-     "execution_count": 34,
+     "execution_count": 56,
      "metadata": {},
      "output_type": "execute_result"
     }
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.5.3"
+   "version": "3.6.3"
   }
  },
  "nbformat": 4,