Added stream cipher
authorNeil Smith <neil.git@njae.me.uk>
Wed, 29 Jul 2015 10:08:29 +0000 (11:08 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Wed, 29 Jul 2015 10:08:29 +0000 (11:08 +0100)
SIGNED.md
stream-cipher.ipynb [new file with mode: 0644]

index 7b813931910e00bb7eba27131a7ee53c80928c71..fc950b99985ac52f902a657cfeff3f19d3e62606 100644 (file)
--- a/SIGNED.md
+++ b/SIGNED.md
@@ -3,19 +3,19 @@
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v2
 
-iQIcBAABAgAGBQJVtlMvAAoJEJPB2e07Pgbqi4QQAKLPu4LQCCX4SCSBMRqbmeUt
-zEn5Epz0RUjSBvfcwo44tbpNt/zHQKG3nff7xTQ9IgL2FX218fuGOkWALPgy94UU
-kYNdx00ZoaD/q7LLJyvXIaT+NVHIVqNvkD1Knjigoa0s7LgvmsfLfKJ+1ncU1ici
-OLSMBWPYZ9wIc3QSho57WMIr1j2Y6PPwgh8Cy+roB976kzph2Jzy/uF+ih0+S5fe
-9uy1ZHP7RM5VrRt9hxCCj8SeUVfC+J+UvTDYT2Bx8VVgJjLhpo1clXWtCO6Yn0OK
-62ZGtmu0lf4Bp2QYRzFc42kbuIVhxM00ZgP2MluQq31yuLJ2gE/tFYd3uib2m56E
-mONhXvdS5CjllHAf5VnjpbTBj3goTQ9VG4liA+AkzJCjx1RKkpnKb9IR50HURpMA
-hkmD8Nr1zmhzRo0OMFX95AotPRnhl3vPvLm8KYLWJflXFE+3ohefmEor/m06JYsd
-KagzwLUCsmz3E3OvrRSSR+O8PYbeLENEnf8uHFSWua5z3eUh9FQRCC8bYKhOfGb3
-PFuNkoNdSEwES2PTdcHQPtbqD408XfUoarIUfNr8dsjmDzAqNa1xxblcI7c/o+PH
-A1aln1HIbyPh3VqRX20XC/5BOsDYG5cL9Nk/ELrnhwV4KmFxlV89qP5M5o3G6RTj
-hFZVracXKEeZqwFEE3ks
-=94iF
+iQIcBAABAgAGBQJVuKYhAAoJEJPB2e07Pgbqj/0P/0XBNZk/zEPZUJrD9EqppzQU
+1m50EprHjaHFXBGGRMrExvpDgKljIR0KpId1wvmdkxmkQFASrr02+bs+YaEUp/Uv
+7+5GmJm6mHQbNzxQevOJUB4YqEF7rBjZAlSEZsSITNa/GRux1C9jEM71IId6ZEYv
+WIXU1lXgyue3tVk3jhv0lebzbyiea6wkFDdbMheQjDaY32g8Iyexo4qR4FWfUjmt
+nVaENg30ZOhvpY5CTGBLfAW35uUZIko9j87AFpQo2YyBBnH391rThJ3e3AL3hFtX
+fyVXFzbJDteYYAEVWET/p3vy+z4wKitJ1v8/o9kHYH4rPRehLGmNlKDFyeSGWI87
+R+m5ZXpZzXATz0Ydso9c1RCp1TgbB7byPyYDvyc92ND7jpdWJ/fH/ATTQggsRxx1
+88TMEMO77nXND4uQZcIRfdIQShUFtJy3F7PGmJ3SjtXHfvLAAqIFP3rwDeegRiiE
+T3yWzQEdM3JpU929xzCInPkJf3hQHUcuJdT1HcZBfySDpxAfVnLr9KYqxj/dpgGT
+YV+w8/D93hhnmArtgGSTSSsjTzJasQRo7/amEz/XiNn3408xJsGBghDXU80oyPfF
+lAbN05d/coqSfam5DEzK/LVvG5n3ghSSAFQKnDqgf2qtE82lC9WHzn1YrzunDcZt
+JQPSnePXSltPU//VuH8A
+=tV49
 -----END PGP SIGNATURE-----
 
 ```
@@ -31,6 +31,7 @@ size   exec  file                         contents
              ./                                                                                           
 384            .gitignore                 a93de2ae5c2a47a38599751d1f914566569dfa09dd1778e207117db6c71421dd
 28564          nim-menace-learning.ipynb  e13cd187b5785516ade802d71674ff682da7315da1a5ef82d2fe66d3ff8675bf
+9766           stream-cipher.ipynb        f02569456c91a825ea445c878b78341e83939444931affb15cded70c5c61d218
 ```
 
 #### Ignore
diff --git a/stream-cipher.ipynb b/stream-cipher.ipynb
new file mode 100644 (file)
index 0000000..213eb83
--- /dev/null
@@ -0,0 +1,491 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "import string"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 40,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def sanitised(text):\n",
+    "    return ''.join(letter.lower() for letter in text if letter in string.ascii_letters)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 41,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'thequickbrownfoxjumpedoverthelzydoga'"
+      ]
+     },
+     "execution_count": 41,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "sanitised('The quick brown fox! jumped OVER the l\\a!z%y* dog....a')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [],
+   "source": [
+    "def encipher_letter(previous, current):\n",
+    "    new = (ord(previous) + ord(current) - 2 * ord('a')) % 26\n",
+    "    return chr(new + ord('a'))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 15,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def decipher_letter(previous, current):\n",
+    "    new = (ord(current) - ord(previous) - 2 * ord('a')) % 26\n",
+    "    return chr(new + ord('a'))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'a'"
+      ]
+     },
+     "execution_count": 4,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "encipher_letter('a', 'a')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'abcdefghijklmnopqrstuvwxyz'"
+      ]
+     },
+     "execution_count": 6,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "''.join(encipher_letter('a', l) for l in string.ascii_lowercase)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'bcdefghijklmnopqrstuvwxyza'"
+      ]
+     },
+     "execution_count": 7,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "''.join(encipher_letter('b', l) for l in string.ascii_lowercase)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 17,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'abcdefghijklmnopqrstuvwxyz'"
+      ]
+     },
+     "execution_count": 17,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "''.join(decipher_letter('a', l) for l in string.ascii_lowercase)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 18,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'b'"
+      ]
+     },
+     "execution_count": 18,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "decipher_letter('a', 'b')"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "e = (p + c) % 26\n",
+    "c = e - p"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 20,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'abcdefghijklmnopqrstuvwxyzbcdefghijklmnopqrstuvwxyzacdefghijklmnopqrstuvwxyzabdefghijklmnopqrstuvwxyzabcefghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdeghijklmnopqrstuvwxyzabcdefhijklmnopqrstuvwxyzabcdefgijklmnopqrstuvwxyzabcdefghjklmnopqrstuvwxyzabcdefghiklmnopqrstuvwxyzabcdefghijlmnopqrstuvwxyzabcdefghijkmnopqrstuvwxyzabcdefghijklnopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmnpqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoprstuvwxyzabcdefghijklmnopqstuvwxyzabcdefghijklmnopqrtuvwxyzabcdefghijklmnopqrsuvwxyzabcdefghijklmnopqrstvwxyzabcdefghijklmnopqrstuwxyzabcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvwyzabcdefghijklmnopqrstuvwxzabcdefghijklmnopqrstuvwxy'"
+      ]
+     },
+     "execution_count": 20,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "''.join(encipher_letter(k, l) for l in string.ascii_lowercase for k in string.ascii_lowercase)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 21,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[('a', 'a', 'a'),\n",
+       " ('b', 'a', 'b'),\n",
+       " ('c', 'a', 'c'),\n",
+       " ('d', 'a', 'd'),\n",
+       " ('e', 'a', 'e'),\n",
+       " ('f', 'a', 'f'),\n",
+       " ('g', 'a', 'g'),\n",
+       " ('h', 'a', 'h'),\n",
+       " ('i', 'a', 'i'),\n",
+       " ('j', 'a', 'j')]"
+      ]
+     },
+     "execution_count": 21,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "encrypted = [(k, l, encipher_letter(k, l)) for l in string.ascii_lowercase for k in string.ascii_lowercase]\n",
+    "encrypted[:10]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 22,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "True"
+      ]
+     },
+     "execution_count": 22,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "all(l == decipher_letter(k, e) for k, l, e in encrypted)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 55,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def encipher_message(plaintext, key='a'):\n",
+    "    ciphertext = ''\n",
+    "    previous = key\n",
+    "    for letter in plaintext:\n",
+    "        cipherletter = encipher_letter(previous, letter)\n",
+    "        ciphertext += cipherletter\n",
+    "        previous = cipherletter\n",
+    "        # previous = letter\n",
+    "    return ciphertext"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 56,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'hlwhv'"
+      ]
+     },
+     "execution_count": 56,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "encipher_message('hello')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 57,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'xbmxl'"
+      ]
+     },
+     "execution_count": 57,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "encipher_message('hello', 'q')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 54,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def decipher_message(ciphertext, key='a'):\n",
+    "    plaintext = ''\n",
+    "    previous = key\n",
+    "    for letter in ciphertext:\n",
+    "        plainletter = decipher_letter(previous, letter)\n",
+    "        plaintext += plainletter\n",
+    "        previous = letter\n",
+    "        # previous = plainletter\n",
+    "    return plaintext"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 58,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'hello'"
+      ]
+     },
+     "execution_count": 58,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "decipher_message('hlwhv')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 59,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'xello'"
+      ]
+     },
+     "execution_count": 59,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "decipher_message('xbmxl')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 60,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'hello'"
+      ]
+     },
+     "execution_count": 60,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "decipher_message('xbmxl', 'q')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 61,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'hlwhvjxlznbpdrfthvjxlznbpd'"
+      ]
+     },
+     "execution_count": 61,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "encipher_message('helloooooooooooooooooooooo')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 62,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'helloooooooooooooooooooooo'"
+      ]
+     },
+     "execution_count": 62,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "decipher_message('hlwhvjxlznbpdrfthvjxlznbpd')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "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.4.2"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}