Started work on the pocket enigma slides
[cipher-training.git] / slides / pocket-enigma-encipher.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Pocket enigma</title>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
6 <style type="text/css">
7 /* Slideshow styles */
8 body {
9 font-size: 20px;
10 }
11 h1, h2, h3 {
12 font-weight: 400;
13 margin-bottom: 0;
14 }
15 h1 { font-size: 3em; }
16 h2 { font-size: 2em; }
17 h3 { font-size: 1.6em; }
18 a, a > code {
19 text-decoration: none;
20 }
21 code {
22 -moz-border-radius: 5px;
23 -web-border-radius: 5px;
24 background: #e7e8e2;
25 border-radius: 5px;
26 font-size: 16px;
27 }
28 .plaintext {
29 background: #272822;
30 color: #80ff80;
31 text-shadow: 0 0 20px #333;
32 padding: 2px 5px;
33 }
34 .ciphertext {
35 background: #272822;
36 color: #ff6666;
37 text-shadow: 0 0 20px #333;
38 padding: 2px 5px;
39 }
40 .float-right {
41 float: right;
42 }
43 </style>
44 </head>
45 <body>
46 <textarea id="source">
47
48 # Pocket Enigma
49
50 ![centre-aligned Pocket Engima](pocket-enigma-small.jpg)
51
52 Stateful cipher
53
54 ---
55
56 # Pocket Enigma
57
58 Emulates the Enigma machine from WWII
59
60 Mechanical cipher machine
61
62 * Substitution cipher
63 * Substitution changes with every letter
64
65 Ciphering method: advance the wheel, then follow the lines to encipher the letter
66
67 ## Stateful enciphering
68
69 The cipher depends on the position of the wheel
70
71 We need to encapsulate that state
72
73 Objects have state
74
75 ---
76
77 # The PocketEnigma object
78
79 What do we want it to do?
80
81 What data should the object hold?
82
83 ---
84
85 # The PocketEnigma object
86
87 What do we want it to do?
88
89 * Initialise with the appropriate wheel (and possible starting position)
90 * Spin the wheel to a given position
91 * Advance the wheel one position
92 * Look up a letter given the wheel position
93 * Encipher a letter (advance the wheel then look up the letter)
94 * Encipher a message (optionally give the key)
95 * Make aliases for deciphering (same as enciphering)
96
97
98 * Accept user-defined wheels
99 * ...and validate them
100
101 What data should it hold?
102
103 * A description of the wheel being used
104 * The current position of the wheel
105
106 ---
107
108 # Data structures
109
110 What's a convenient representation of the wheel
111
112 1. for the object to use internally
113 2. for a person to use to describe the wheel
114
115 They may not be the same, and we'll have to translate between them
116
117 ---
118
119 # Data structures
120
121 ### Internal use: list of transpositions.
122
123 ```python
124 [2, 3, 0, 1, 22, 8, 15, 12, 5, ...
125 ```
126
127 so position 0 ('a') swaps with position 2 ('c'), position 3 ('d') swaps with position 1 ('b'), and so on.
128
129 * This will be a nightmare to enter correctly
130
131 ### Exernal use: list of pairs
132
133 ```python
134 [('a', 'c'), ('b', 'd'), ...]
135 ```
136
137 Easier to enter
138
139 * Need to validate the human-entered list, to check it's valid
140
141 ---
142
143 # Validating the wheel description
144
145 What tests?
146
147 ---
148
149 # Validating the wheel specification
150
151 What tests?
152
153 * 13 elements...
154 * ...each a pair...
155 * ...and 26 letters mentioned overall
156
157 ---
158
159 # Making the PocketEnigma class
160
161 ```python
162 class PocketEnigma(object):
163 def __init__(self, wheel=1, position='a'):
164 self.wheel1 = [('a', 'z'), ('b', 'e'), ('c', 'x'), ('d', 'k'),
165 ('f', 'h'), ('g', 'j'), ('i', 'm'), ('l', 'r'), ('n', 'o'),
166 ('p', 'v'), ('q', 't'), ('s', 'u'), ('w', 'y')]
167 self.wheel2 = [('a', 'c'), ('b', 'd'), ('e', 'w'), ('f', 'i'),
168 ('g', 'p'), ('h', 'm'), ('j', 'k'), ('l', 'n'), ('o', 'q'),
169 ('r', 'z'), ('s', 'u'), ('t', 'v'), ('x', 'y')]
170 # Rest of initialisation code here
171
172 def make_wheel_map(self, wheel_spec):
173 ...
174 self.wheel_map = ...
175 ...
176
177 def validate_wheel_spec(self, wheel_spec):
178 if len(wheel_spec) != 13:
179 raise ValueError("Wheel specification has {} pairs, requires 13".
180 format(len(wheel_spec)))
181 ...
182 ```
183
184 ---
185
186 # Looking up the enciphered version of a letter
187
188 *Not* advancing the wheel before
189
190 Keep `self.position` to record where the wheel is
191
192 * `__init__` can be passed a letter, but internally it's a number
193
194 But the wheel map only works if the wheel arrow is pointing at 'a'
195
196 Idea:
197
198 1. Rotate the source letter back `position` spaces
199 2. Do the lookup
200 3. Rotate the destination letter forward `position` spaces
201
202 (all mod 26)
203
204
205 </textarea>
206 <script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
207 </script>
208
209 <script type="text/javascript"
210 src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
211
212 <script type="text/javascript">
213 var slideshow = remark.create({ ratio: "16:9" });
214
215 // Setup MathJax
216 MathJax.Hub.Config({
217 tex2jax: {
218 skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
219 }
220 });
221 MathJax.Hub.Queue(function() {
222 $(MathJax.Hub.getAllJax()).map(function(index, elem) {
223 return(elem.SourceElement());
224 }).parent().addClass('has-jax');
225 });
226 MathJax.Hub.Configured();
227 </script>
228 </body>
229 </html>