<!DOCTYPE html>
<html>
<head>
- <title>Affine ciphers</title>
+ <title>Breaking affine ciphers</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
/* Slideshow styles */
<!DOCTYPE html>
<html>
<head>
- <title>Breaking keyword ciphers</title>
+ <title>Further work</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
/* Slideshow styles */
<textarea id="source">
# Taking this further
-
-### Countdown
+
+
+<table>
+<tr valign="top">
+<td>
+### Countdown
+
* Conundrum
* Letters
* Picking letters to maximise score
* Numbers
* Read the "Functional Pearl"
-
+</td>
+<td>
### Hangman
* Letter probabilities based on each word occurring once in the dictionary
* Set of candidate words filtered by length, letters guessed
-
+</td>
+</tr>
+<tr>
+<td rowspan=2>
+### Full Enigma and Bombe
+* Steckerboard
+* Three wheels
+ * (and all the turnover logic)
+* Wheel rings
+* Extend the Bombe
+</td>
+<td>
### Text generation
* Read some text, find the n-grams, generate more text from that.
-
+</td>
+<tr>
+<td>
### Spelling correction
* Suggest the most likely correct word, given the probability of these errors.
+</td>
+</tr>
+</table>
</textarea>
<script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
--- /dev/null
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Index of cipher training</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+ <style type="text/css">
+ /* Slideshow styles */
+ body {
+ font-size: 20px;
+ }
+ h1, h2, h3 {
+ font-weight: 400;
+ margin-bottom: 0;
+ }
+ h1 { font-size: 3em; }
+ h2 { font-size: 2em; }
+ h3 { font-size: 1.6em; }
+ a, a > code {
+ text-decoration: none;
+ }
+ code {
+ -moz-border-radius: 5px;
+ -web-border-radius: 5px;
+ background: #e7e8e2;
+ border-radius: 5px;
+ font-size: 16px;
+ }
+ .plaintext {
+ background: #272822;
+ color: #80ff80;
+ text-shadow: 0 0 20px #333;
+ padding: 2px 5px;
+ }
+ .ciphertext {
+ background: #272822;
+ color: #ff6666;
+ text-shadow: 0 0 20px #333;
+ padding: 2px 5px;
+ }
+ .float-right {
+ float: right;
+ }
+ </style>
+ </head>
+ <body>
+ <textarea id="source">
+
+# Cipher programming training
+
+* [Aims](aims.html)
+* Caesar ciphers: [Making](caesar-encipher.html) and [Breaking](caesar-break.html) *(Changing representations, language models, text encodings)*
+* Affine ciphers: [Making](affine-encipher.html) and [Breaking](affine-break.html) *(Time/space trade-offs, off-by-one issues)*
+* [Word segmentation](word-segmentation.html) *(Memoisation and complexity)*
+* Keyword ciphers: [Making](keyword-encipher.html) and [Breaking](keyword-break.html) *(Being Pythonic and parallelism)*
+* Transposition ciphers: [Making](transposition-encipher.html) and [Breaking](transposition-break.html) *(Equivalence classes)*
+* Pocket enigma: [Making](pocket-enigma-encipher.html) and [Breaking](pocket-enigma-break.html) *(Object orientation)*
+* [Alternative plaintext scoring](alternative-plaintext-scoring.html) *(Empirical computing through simulation)*
+* [Further work](further-work.html)
+
+ </textarea>
+ <script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
+ </script>
+
+ <script type="text/javascript"
+ src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
+
+ <script type="text/javascript">
+ var slideshow = remark.create({ ratio: "16:9" });
+
+ // Setup MathJax
+ MathJax.Hub.Config({
+ tex2jax: {
+ skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
+ }
+ });
+ MathJax.Hub.Queue(function() {
+ $(MathJax.Hub.getAllJax()).map(function(index, elem) {
+ return(elem.SourceElement());
+ }).parent().addClass('has-jax');
+ });
+ MathJax.Hub.Configured();
+ </script>
+ </body>
+</html>
--- /dev/null
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Breaking the pocket enigma</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+ <style type="text/css">
+ /* Slideshow styles */
+ body {
+ font-size: 20px;
+ }
+ h1, h2, h3 {
+ font-weight: 400;
+ margin-bottom: 0;
+ }
+ h1 { font-size: 3em; }
+ h2 { font-size: 2em; }
+ h3 { font-size: 1.6em; }
+ a, a > code {
+ text-decoration: none;
+ }
+ code {
+ -moz-border-radius: 5px;
+ -web-border-radius: 5px;
+ background: #e7e8e2;
+ border-radius: 5px;
+ font-size: 16px;
+ }
+ .plaintext {
+ background: #272822;
+ color: #80ff80;
+ text-shadow: 0 0 20px #333;
+ padding: 2px 5px;
+ }
+ .ciphertext {
+ background: #272822;
+ color: #ff6666;
+ text-shadow: 0 0 20px #333;
+ padding: 2px 5px;
+ }
+ .float-right {
+ float: right;
+ }
+ </style>
+ </head>
+ <body>
+ <textarea id="source">
+
+# Breaking the Pocket Enigma
+
+![centre-aligned Pocket Engima](pocket-enigma-small.jpg)
+
+Using cribs
+
+---
+
+# Breaking the Pocket Enigma
+
+A _crib_ is a piece of plaintext we believe to be in the enciphered message.
+
+This is the way Enigma messages were broken in WWII.
+
+There are many keys, and doing the naïve Bayes analysis was too expensive.
+
+The possible keys were filtered by finding the ones that could have produced the crib. These filtered keys could then be checked manually.
+
+At Bletchley Park, the filtering was done by the Bombes.
+
+---
+
+# Breaking by cribs
+
+```
+Given a message, a wheel spec, a crib, and the location of that crib:
+ Go through each key in turn
+ If the plaintext matches the crib:
+ Add it to the list of possible keys
+Return the possible keys
+```
+
+## Example
+```python
+pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'l', 3)
+['a', 'j', 'n']
+```
+
+Using wheel 1, the `j` (`ciphertext[3]`) could become `l` if the wheel starts the message on a, j, or n.
+
+ </textarea>
+ <script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
+ </script>
+
+ <script type="text/javascript"
+ src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
+
+ <script type="text/javascript">
+ var slideshow = remark.create({ ratio: "16:9" });
+
+ // Setup MathJax
+ MathJax.Hub.Config({
+ tex2jax: {
+ skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
+ }
+ });
+ MathJax.Hub.Queue(function() {
+ $(MathJax.Hub.getAllJax()).map(function(index, elem) {
+ return(elem.SourceElement());
+ }).parent().addClass('has-jax');
+ });
+ MathJax.Hub.Configured();
+ </script>
+ </body>
+</html>
--- /dev/null
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Pocket enigma</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+ <style type="text/css">
+ /* Slideshow styles */
+ body {
+ font-size: 20px;
+ }
+ h1, h2, h3 {
+ font-weight: 400;
+ margin-bottom: 0;
+ }
+ h1 { font-size: 3em; }
+ h2 { font-size: 2em; }
+ h3 { font-size: 1.6em; }
+ a, a > code {
+ text-decoration: none;
+ }
+ code {
+ -moz-border-radius: 5px;
+ -web-border-radius: 5px;
+ background: #e7e8e2;
+ border-radius: 5px;
+ font-size: 16px;
+ }
+ .plaintext {
+ background: #272822;
+ color: #80ff80;
+ text-shadow: 0 0 20px #333;
+ padding: 2px 5px;
+ }
+ .ciphertext {
+ background: #272822;
+ color: #ff6666;
+ text-shadow: 0 0 20px #333;
+ padding: 2px 5px;
+ }
+ .float-right {
+ float: right;
+ }
+ </style>
+ </head>
+ <body>
+ <textarea id="source">
+
+# Pocket Enigma
+
+![centre-aligned Pocket Engima](pocket-enigma-small.jpg)
+
+Stateful cipher
+
+---
+
+# Pocket Enigma
+
+Emulates the Enigma machine from WWII
+
+Mechanical cipher machine
+
+* Substitution cipher
+* Substitution changes with every letter
+
+Ciphering method: advance the wheel, then follow the lines to encipher the letter
+
+## Stateful enciphering
+
+The cipher depends on the position of the wheel
+
+We need to encapsulate that state
+
+Objects have state
+
+---
+
+# The PocketEnigma object
+
+What do we want it to do?
+
+What data should the object hold?
+
+---
+
+# The PocketEnigma object
+
+What do we want it to do?
+
+* Initialise with the appropriate wheel (and possible starting position)
+* Spin the wheel to a given position
+* Advance the wheel one position
+* Look up a letter given the wheel position
+* Encipher a letter (advance the wheel then look up the letter)
+* Encipher a message (optionally give the key)
+* Make aliases for deciphering (same as enciphering)
+
+
+* Accept user-defined wheels
+ * ...and validate them
+
+What data should it hold?
+
+* A description of the wheel being used
+* The current position of the wheel
+
+---
+
+# Data structures
+
+What's a convenient representation of the wheel
+
+1. for the object to use internally
+2. for a person to use to describe the wheel
+
+They may not be the same, and we'll have to translate between them
+
+---
+
+# Data structures
+
+### Internal use: list of transpositions.
+
+```python
+[2, 3, 0, 1, 22, 8, 15, 12, 5, ...
+```
+
+so position 0 ('a') swaps with position 2 ('c'), position 3 ('d') swaps with position 1 ('b'), and so on.
+
+* This will be a nightmare to enter correctly
+
+### Exernal use: list of pairs
+
+```python
+[('a', 'c'), ('b', 'd'), ...]
+```
+
+Easier to enter
+
+* Need to validate the human-entered list, to check it's valid
+
+---
+
+# Validating the wheel description
+
+What tests?
+
+---
+
+# Validating the wheel specification
+
+What tests?
+
+* 13 elements...
+* ...each a pair...
+* ...and 26 letters mentioned overall
+
+Raise exceptions if the specification is invalid
+
+---
+
+# Making the PocketEnigma class
+
+```python
+class PocketEnigma(object):
+ def __init__(self, wheel=1, position='a'):
+ self.wheel1 = [('a', 'z'), ('b', 'e'), ('c', 'x'), ('d', 'k'),
+ ('f', 'h'), ('g', 'j'), ('i', 'm'), ('l', 'r'), ('n', 'o'),
+ ('p', 'v'), ('q', 't'), ('s', 'u'), ('w', 'y')]
+ self.wheel2 = [('a', 'c'), ('b', 'd'), ('e', 'w'), ('f', 'i'),
+ ('g', 'p'), ('h', 'm'), ('j', 'k'), ('l', 'n'), ('o', 'q'),
+ ('r', 'z'), ('s', 'u'), ('t', 'v'), ('x', 'y')]
+ # Rest of initialisation code here
+
+ def make_wheel_map(self, wheel_spec):
+ ...
+ self.wheel_map = ...
+ ...
+
+ def validate_wheel_spec(self, wheel_spec):
+ if len(wheel_spec) != 13:
+ raise ValueError("Wheel specification has {} pairs, requires 13".
+ format(len(wheel_spec)))
+ ...
+```
+
+---
+
+# A note on testing
+
+Testing's easier if everything returns a meaningful value
+
+Saves having to look up different values after performing each operation
+
+`__init__` can't return a value (restriction of Python)
+
+```python
+if __name__ == "__main__":
+ import doctest
+ doctest.testmod(extraglobs={'pe': PocketEnigma(1, 'a')})
+```
+
+`pe` is now available in all tests.
+
+---
+
+# Looking up the enciphered version of a letter
+
+*Not* advancing the wheel before
+
+Keep `self.position` to record where the wheel is
+
+* `__init__` can be passed a letter, but internally it's a number
+
+But the wheel map only works if the wheel arrow is pointing at 'a'
+
+Idea:
+
+1. Rotate the source letter back `position` spaces
+2. Do the lookup
+3. Rotate the destination letter forward `position` spaces
+
+(all mod 26)
+
+i.e. source → subtract position → lookup destination → add position
+
+---
+
+# Advance the wheel
+
+Trivial...
+
+# Encipher a letter
+
+Advance the wheel, then look up the letter
+
+---
+
+# Encipher a message
+
+```python
+ciphertext = ''
+for letter in plaintext:
+ ciphertext += encipher_letter(letter)
+return ciphertext
+```
+
+Have to be explicit as the order of the operations is important
+
+* Something like `map` might choose an order different from strict left-to-right
+
+## Test it against the physical object
+
+ </textarea>
+ <script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
+ </script>
+
+ <script type="text/javascript"
+ src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
+
+ <script type="text/javascript">
+ var slideshow = remark.create({ ratio: "16:9" });
+
+ // Setup MathJax
+ MathJax.Hub.Config({
+ tex2jax: {
+ skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
+ }
+ });
+ MathJax.Hub.Queue(function() {
+ $(MathJax.Hub.getAllJax()).map(function(index, elem) {
+ return(elem.SourceElement());
+ }).parent().addClass('has-jax');
+ });
+ MathJax.Hub.Configured();
+ </script>
+ </body>
+</html>
--- /dev/null
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Breaking transposition ciphers</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+ <style type="text/css">
+ /* Slideshow styles */
+ body {
+ font-size: 20px;
+ }
+ h1, h2, h3 {
+ font-weight: 400;
+ margin-bottom: 0;
+ }
+ h1 { font-size: 3em; }
+ h2 { font-size: 2em; }
+ h3 { font-size: 1.6em; }
+ a, a > code {
+ text-decoration: none;
+ }
+ code {
+ -moz-border-radius: 5px;
+ -web-border-radius: 5px;
+ background: #e7e8e2;
+ border-radius: 5px;
+ font-size: 16px;
+ }
+ .plaintext {
+ background: #272822;
+ color: #80ff80;
+ text-shadow: 0 0 20px #333;
+ padding: 2px 5px;
+ }
+ .ciphertext {
+ background: #272822;
+ color: #ff6666;
+ text-shadow: 0 0 20px #333;
+ padding: 2px 5px;
+ }
+ .float-right {
+ float: right;
+ }
+ </style>
+ </head>
+ <body>
+ <textarea id="source">
+
+# Breaking transposition ciphers
+
+ attack the fort at dawn
+
+ a t t a c
+ k t h e f
+ o r t a t
+ d a w n
+
+ akod ttra aean cft
+
+Generally quite familiar...
+
+## Try all the keys, pick the one that looks most like Englilsh
+
+---
+
+# ...Pick one that looks most like English
+
+But the naïve Bayes score will always be the same!
+
+* Same letters, just a different order.
+
+Score by probability of substrings of letters
+
+* Bigrams, trigrams, _n_-grams
+
+---
+
+# Finding _n_-grams
+
+Given `count_2l.txt` and `count_3l.txt`, counts of bigrams and trigrams in English
+
+# Write a function that returns all the _n_-grams for a text, given _n_
+ * Assume the text is already sanitised
+
+# Build `P2l`, `P3l` (after `Pl`), `Pbigrams`, `Ptrigrams` (after `Pletters`)
+
+---
+
+# Breaking scytale
+
+What are the possible keys?
+
+---
+
+# Try all the keys...
+
+*All* the keys?
+
+What's the transposition of 'cat'?
+
+* 'bat'?
+* 'car'?
+* 'wry'?
+* 'babe'?
+* 'powwow'?
+
+---
+
+# Equivalence classes and canonical forms
+
+Lots of words yield the same transposition
+
+* They're all in the same equivalence class
+* Only need to test one from the class
+
+General idea: if there are different ways to represent something, pick one to make comparisons easier
+
+* Canonical form, canonical representation
+
+---
+
+# Finding the transpositions to try
+
+```
+For each word:
+ if it's a new transposition:
+ add it to the list
+```
+
+What data structure to use to store the transpositions?
+
+
+ </textarea>
+ <script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
+ </script>
+
+ <script type="text/javascript"
+ src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
+
+ <script type="text/javascript">
+ var slideshow = remark.create({ ratio: "16:9" });
+
+ // Setup MathJax
+ MathJax.Hub.Config({
+ tex2jax: {
+ skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
+ }
+ });
+ MathJax.Hub.Queue(function() {
+ $(MathJax.Hub.getAllJax()).map(function(index, elem) {
+ return(elem.SourceElement());
+ }).parent().addClass('has-jax');
+ });
+ MathJax.Hub.Configured();
+ </script>
+ </body>
+</html>
+
<!DOCTYPE html>
<html>
<head>
- <title>Keyword ciphers</title>
+ <title>Transposition ciphers</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
/* Slideshow styles */
* Unwind the strip
* "Unreadable" unless reader has pole of same diameter
- attack the fort at dawn
+```
+attack the fort at dawn
- a t t a c
- k t h e f
- o r t a t
- d a w n
+a t t a c
+k t h e f
+o r t a t
+d a w n
- akod ttra aean cft
+akod ttra aean cft
+```
---
* Reorder columns based on keyword
* Read the grid (perhaps different direction)
+(Keyword = secret → cerst)
+```
+attack the fort at dawn
+
+s e c r t c e r s t
+--------- ---------
+a t t a c t t a a c
+k t h e f h t e k f
+o r t a t t r a o t
+d a w n w a n d
+
+ttaac htekf traot wand
+thtw tra aean akod cft
+```
+
Scytale is just a special case of column transposition.
---
# Grids and data structures
-How to represent a grid?
+What operations do we need to do on a grid?
-What operations do we need to do on it?
+How to represent a grid?
---
# Grids and data structures
-How to represent a grid?
-
-* List of strings
-* Each row is a string
-* Rows in order in the list
-
-What operations do we need to do on it?
+What operations do we need to do on a grid?
* Fill, by rows or columns
* Empty, by rows or columns
* Calculate the size of the grid
* Pad message to fit a rectangle of the required size
+How to represent a grid?
+
+* List of strings
+* Each row is a string
+* Rows in order in the list
+
---
# Finding sizes
Know number of columns
-Number of rows = ceiling(message length / columns)
+Number of rows = `\(\left \lceil \frac{\mathrm{message\ length}}{\mathrm{columns}} \right \rceil\)`
-Paddding is (rows * columns) - message length
+Paddding is (rows ⨉ columns) - message length
* What to use as default padding?
* Keyword parameter!
The transposition `(3, 2, 6, 5, 1, 4, 0)` says that what was in position 3 moves to position 0, what was in position 2 moves to position 1, what was in position 6 moves to position 2, ...
-`enumerate(_iterable_)` yields an iterator that walks over the iterator, including the element indexes.
+`enumerate(_iterable_)` yields an iterator that walks over the iterable, including the element indexes.
```python
>>> [i for i in enumerate('treason')]
---
+# Back to the scytale
+
+Key is number of rows.
+
+No transposition of columns.
+
+* What does a null transposition look like?
+
+How to fill and empty?
+
+(Transposing the grid is easier)
+
+---
+
# Masking the fill characters
Padding characters can be distinctive.