4 <title>Keyword ciphers
</title>
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8"/>
6 <style type=
"text/css">
15 h1 { font-size:
3em; }
16 h2 { font-size:
2em; }
17 h3 { font-size:
1.6em; }
19 text-decoration: none;
22 -moz-border-radius:
5px;
23 -web-border-radius:
5px;
31 text-shadow:
0 0 20px #
333;
37 text-shadow:
0 0 20px #
333;
46 <textarea id=
"source">
48 # Transposition ciphers
50 attack the fort at dawn
61 # Transposition ciphers
63 Rather than changing symbols (substitution ciphers),
67 Still disguises the message.
69 (Good ciphers do both, and more.)
75 Even older than Caesar cipher.
77 * Wrap a strip round a pole
78 * Write the message along it
80 *
"Unreadable" unless reader has pole of same diameter
82 attack the fort at dawn
93 # Generalising: column transposition ciphers
95 Scytale essentially fills a grid by rows, then reads it by columns
97 * (Deciphering is the reverse)
99 Column transposition ciphers:
102 * Reorder columns based on keyword
103 * Read the grid (perhaps different direction)
105 Scytale is just a special case of column transposition.
109 # Grids and data structures
111 How to represent a grid?
113 What operations do we need to do on it?
117 # Grids and data structures
119 How to represent a grid?
122 * Each row is a string
123 * Rows in order in the list
125 What operations do we need to do on it?
127 * Fill, by rows or columns
128 * Empty, by rows or columns
130 * Calculate the size of the grid
131 * Pad message to fit a rectangle of the required size
137 Know number of columns
139 Number of rows = ceiling(message length / columns)
141 Paddding is (rows * columns) - message length
143 * What to use as default padding?
146 ## Fit 'thequickbrownfox' (
16 letters) into grid of
153 # Fill and empty grid by rows
155 Split message into row-sized chunks
159 Append all the rows together
161 * `
<string
>.join()`
163 Keep thinking about test cases!
167 # Fill and empty grid by columns
169 Idea: fill and empty by rows, with a transposition.
171 `zip(*rows)` and `itertools.zip_longest(*rows)`
177 How to represent a transposition (_permutation_, to mathematicians)?
179 How to create it from a keyword?
183 # Idea of a transposition
185 Says, for each element, where it should go
195 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, ...
197 `enumerate(_iterable_)` yields an iterator that walks over the iterator, including the element indexes.
200 >>> [i for i in enumerate('treason')]
201 [(
0, 't'), (
1, 'r'), (
2, 'e'), (
3, 'a'), (
4, 's'), (
5, 'o'), (
6, 'n')]
202 >>> [i for i in enumerate((
3,
2,
6,
5,
1,
4,
0))]
203 [(
0,
3), (
1,
2), (
2,
6), (
3,
5), (
4,
1), (
5,
4), (
6,
0)]
206 Write the `transpose` and `untranspose` functions.
210 # Transposition from a keyword
212 Deduplicate the keyword
216 Use `
<iterable
>.index()` to find the positions of the letters in the sorted keyword
220 # Transposition ciphers
226 # Masking the fill characters
228 Padding characters can be distinctive.
230 Make a function that generates a random letter, based on the `normalised_english_counts`
232 Use `callable()` to check if the `fillvalue` should be called or just inserted
235 <script src=
"http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type=
"text/javascript">
238 <script type=
"text/javascript"
239 src=
"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
241 <script type=
"text/javascript">
242 var slideshow = remark.create({ ratio:
"16:9" });
247 skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
250 MathJax.Hub.Queue(function() {
251 $(MathJax.Hub.getAllJax()).map(function(index, elem) {
252 return(elem.SourceElement());
253 }).parent().addClass('has-jax');
255 MathJax.Hub.Configured();