Affine Cipher
A cipher done with arithmetic: multiply each letter's number, add a shift, wrap at 26.
How it works
Number the letters from A = 0 to Z = 25. The affine cipher encrypts a letter x with the formula E(x) = (a × x + b) mod 26. "Mod 26" means divide by 26 and keep the remainder, which brings the result back into the alphabet.
The key is the pair of numbers a and b. The shift b can be anything from 0 to 25. The multiplier a is restricted: it must share no common factor with 26. That leaves twelve choices: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. Pick an even number or 13 and two different letters would encrypt to the same one, so the message could not be decoded.
Twelve multipliers and 26 shifts give 312 possible keys. With a = 1 the cipher is a plain Caesar shift. With a = 25 and b = 25 it is Atbash.
To decrypt, use D(y) = a⁻¹ × (y − b) mod 26, where a⁻¹ is the number that gives 1 when multiplied by a, mod 26. For a = 5 it is 21, because 5 × 21 = 105, and 105 leaves 1 when divided by 26.
Worked example
With a = 5 and b = 8, the letter F is 5. Then 5 × 5 + 8 = 33, and 33 mod 26 is 7, which is H.
Allowed values of a and their inverses
| a | Inverse (used to decrypt) |
|---|---|
| 1 | 1 |
| 3 | 9 |
| 5 | 21 |
| 7 | 15 |
| 9 | 3 |
| 11 | 19 |
| 15 | 7 |
| 17 | 23 |
| 19 | 11 |
| 21 | 5 |
| 23 | 17 |
| 25 | 25 |
Common questions
Why can a not be an even number?
26 is even, so an even multiplier folds the alphabet onto itself and pairs of letters collide. The same happens with 13.
How would someone break it?
By trying all 312 keys, or by guessing two letters, such as the two most common, and solving for a and b.