Number TheoryCryptography

Shift and Affine Ciphers

Encrypt letters with x ↦ ax + b (mod 26) and decrypt with x ↦ a⁻¹(x − b); a must be coprime to 26.

Not started Jump to practice

Secret codes are the oldest application of modular arithmetic. Number the letters $A = 0, B = 1, \dots, Z = 25$ and calculate mod 26. Encrypting is multiplying and adding; decrypting is undoing that, which needs a modular inverse.

Shift cipher (Caesar's): $E(x) = x + b \bmod 26$, undone by $D(y) = y - b$. Affine cipher: $$E(x) = ax + b \bmod 26, \qquad D(y) = a^{-1}(y - b) \bmod 26.$$ The key is the pair $(a, b)$, and $a$ must satisfy $\gcd(a, 26) = 1$.

Why it works

To undo $y \equiv ax + b$, subtract $b$ and then "divide by $a$", which mod 26 means multiplying by $a^{-1}$: $a^{-1}(y - b) \equiv a^{-1} a x = x$. The inverse exists exactly when $\gcd(a, 26) = 1$, that is, $a$ is odd and not 13. Those 12 values of $a$ and 26 values of $b$ give $12 \cdot 26 = 312$ keys. If $\gcd(a, 26) > 1$, two letters are sent to the same place, and the message can no longer be read back.
The 26 letters on a clock, with the map $x \mapsto 5x + 8$. Every letter gets its own cipher letter, listed below. With $a = 13$ or $a = 2$, letters collide.

Worked example: Encrypting

Encrypt MATH with $E(x) = 5x + 8 \bmod 26$.
  1. M, A, T, H are $12, 0, 19, 7$.
  2. $5 \cdot 12 + 8 = 68 \equiv 16$, $\ 5 \cdot 0 + 8 = 8$, $\ 5 \cdot 19 + 8 = 103 \equiv 25$, $\ 5 \cdot 7 + 8 = 43 \equiv 17$.
  3. $16, 8, 25, 17$ are Q, I, Z, R.

Answer: QIZR.

Worked example: Decrypting

Decrypt QIZR, encrypted with $E(x) = 5x + 8$.
  1. $5^{-1} \equiv 21 \pmod{26}$, since $5 \cdot 21 = 105 = 4 \cdot 26 + 1$. So $D(y) = 21(y - 8)$.
  2. Q $= 16$: $21 \cdot 8 = 168 \equiv 12$ (M). I $= 8$: $21 \cdot 0 = 0$ (A).
  3. Z $= 25$: $21 \cdot 17 = 357 \equiv 19$ (T). R $= 17$: $21 \cdot 9 = 189 \equiv 7$ (H).

Answer: MATH.

Worked example: A bad key

Why is $a = 13$ not allowed?
  1. $13 \cdot 0 = 0$ and $13 \cdot 2 = 26 \equiv 0$, so A and C get the same cipher letter (for any $b$).
  2. In fact $13x \bmod 26$ is only ever $0$ or $13$, so the cipher uses just two letters.

Answer: $\gcd(13, 26) = 13 \ne 1$.

The allowed multipliers and their inverses mod 26.
$a$1357911151719212325
$a^{-1} \bmod 26$1921153197231151725

Common mistake

Decrypting by ordinary division: "$x = (y - 8)/5$." For Q $= 16$, that gives $8/5$, not a letter. Mod 26, dividing by 5 means multiplying by $5^{-1} = 21$. Check any decryption by encrypting it again.

An affine cipher is also weak against a known-plaintext attack: if Eve learns that two plaintext letters $x_1, x_2$ encrypt to $y_1, y_2$, she solves the two congruences $ax_1 + b \equiv y_1$ and $ax_2 + b \equiv y_2$. Subtracting gives $a(x_1 - x_2) \equiv y_1 - y_2 \pmod{26}$, a linear congruence for $a$, and then $b$ follows.

With only 312 keys, an affine cipher falls to a computer instantly, and even by hand to letter-frequency counting (E is the most common letter in English). Modern cryptography keeps the modular arithmetic but uses enormous moduli and keys that never have to be shared, as in RSA.

Key idea

Affine cipher: $E(x) = ax + b \bmod 26$ with $\gcd(a, 26) = 1$; decrypt with $D(y) = a^{-1}(y - b)$, using the inverse of $a$ mod 26.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 2 XP.

Loading practice…

Builds on

Leads to

This is one of the top rungs of Number Theory.

Sources and further reading

  • William Stein, Elementary Number Theory: Primes, Congruences, and Secrets (Springer, 2009; free online edition), chapter 3, Public-Key Cryptography
  • Lehman, Leighton and Meyer, Mathematics for Computer Science (MIT OpenCourseWare 6.042J), chapter on number theory: Turing's code and modular arithmetic
@teddyrookbook GitHub