4 <title>Affine 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">
50 a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z
51 --|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|--
52 b | e | h | k | n | q | t | w | z | c | f | i | l | o | r | u | x | a | d | g | j | m | p | s | v | y
54 An extension of Caesar ciphers
56 * Count the gaps in the letters.
59 # How affine ciphers work
61 _ciphertext_letter_ =_plaintext_letter_ × a + b
63 * Convert letters to numbers
64 * Take the total modulus
26
68 * Build the `affine_encipher()` function
72 # Deciphering affine ciphers is harder
74 `$$p = \frac{c - b}{a}$$`
76 But modular division is hard!
81 ## Explanation of extended Euclid's algorithm from [Programming with finite fields](http://jeremykun.com/
2014/
03/
13/programming-with-finite-fields/)
83 **Definition:** An element _d_ is called a greatest common divisor (gcd) of _a, b_ if it divides both _a_ and _b_, and for every other _z_ dividing both _a_ and _b_, _z_ divides _d_.
85 **Theorem:** For any two integers _a, b_ there exist unique integers _x, y_ such that _ax_ + _by_ = gcd(_a, b_).
87 We could beat around the bush and try to prove these things in various ways, but when it comes down to it there’s one algorithm of central importance that both computes the gcd and produces the needed linear combination _x, y_. The algorithm is called the Euclidean algorithm. Here is a simple version that just gives the gcd.
91 if abs(a)
< abs(b):
101 This works by the simple observation that gcd(_a_, _aq_ + _r_) = gcd(_a_, _r_) (this is an easy exercise to prove directly). So the Euclidean algorithm just keeps applying this rule over and over again: take the remainder when dividing the bigger argument by the smaller argument until the remainder becomes zero. Then gcd(_x_,
0) =
0 because everything divides zero.
103 Now the so-called ‘extended’ Euclidean algorithm just keeps track of some additional data as it goes (the partial quotients and remainders). Here’s the algorithm.
106 def extendedEuclideanAlgorithm(a, b):
108 (x,y,d) = extendedEuclideanAlgorithm(b, a)
114 x1, x2, y1, y2 =
0,
1,
1,
0
119 a, b, x2, x1, y2, y1 = b, r, x1, x, y1, y
124 Indeed, the reader who hasn’t seen this stuff before is encouraged to trace out a run for the numbers
4864,
3458. Their gcd is
38 and the two integers are
32 and -
45, respectively.
126 How does this help us compute inverses? Well, if we want to find the inverse of _a_ modulo _p_, we know that their gcd is
1. So compute the _x, y_ such that _ax_ + _py_ =
1, and then reduce both sides mod _p_. You get _ax_ +
0 =
1 _mod p_, which means that _x mod p_ is the inverse of _a_. So once we have the extended Euclidean algorithm our inverse function is trivial to write!
130 x,y,d = extendedEuclideanAlgorithm(self.n, self.p)
131 return IntegerModP(x)
134 And indeed it works as expected:
137 >>> mod23 = IntegersModP(
23)
138 >>> mod23(
7).inverse()
140 >>> mod23(
7).inverse() * mod23(
7)
146 <script src=
"http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type=
"text/javascript">
149 <script type=
"text/javascript"
150 src=
"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
152 <script type=
"text/javascript">
153 var slideshow = remark.create({ ratio:
"16:9" });
158 skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
161 MathJax.Hub.Queue(function() {
162 $(MathJax.Hub.getAllJax()).map(function(index, elem) {
163 return(elem.SourceElement());
164 }).parent().addClass('has-jax');
166 MathJax.Hub.Configured();