On this page
The affine cipher is a monoalphabetic substitution cipher: number the letters A = 0 to Z = 25, then send each letter x to E(x) = (ax + b) mod 26 (Wikipedia: Affine cipher). The Caesar cipher is the special case a = 1. Decryption runs the formula backwards, D(y) = a⁻¹(y − b) mod 26, and almost everything interesting about the cipher lives in that a⁻¹.
Why must a be coprime to 26?
A modular inverse of a modulo m exists if and only if gcd(a, m) = 1, and when it exists it is unique (Wikipedia: Modular multiplicative inverse). Without an inverse there is no decryption formula.
You can see the failure directly. Take a = 2 and b = 3. A (0) goes to 3, which is D, and N (13) goes to 29 mod 26 = 3, also D. Two plaintext letters share one ciphertext letter, so whoever receives a D cannot tell which was sent. In general, if a shares a factor d with 26, the letters x and x + 26/d always collide. The diagram above shows it: with a = 2, every odd-numbered letter is hit twice and the even-numbered ones never appear.
Since 26 = 2 × 13, a must be odd and must not be 13. That leaves 12 values: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25 (Wikipedia: Affine cipher). With 26 choices of b, there are 12 × 26 = 312 possible keys, and 26 of them (a = 1) are plain Caesar shifts, according to the same entry.
How do you find a⁻¹ with the extended Euclidean algorithm?
The extended Euclidean algorithm finds integers s and t with as + mt = gcd(a, m); when a and m are coprime, s is the inverse of a modulo m (Wikipedia: Extended Euclidean algorithm). If the ordinary division steps feel rusty, our walkthrough of Euclid's gcd algorithm, step by step covers them first.
Take a = 7. Divide forwards until the remainder is 1:
26 = 3 × 7 + 5
7 = 1 × 5 + 2
5 = 2 × 2 + 1
Then substitute backwards, writing 1 in terms of 26 and 7:
1 = 5 − 2 × 2
1 = 5 − 2 × (7 − 5) = 3 × 5 − 2 × 7
1 = 3 × (26 − 3 × 7) − 2 × 7 = 3 × 26 − 11 × 7
So 7 × (−11) ≡ 1 (mod 26), and −11 + 26 = 15. Check: 7 × 15 = 105 = 4 × 26 + 1. The inverse of 7 modulo 26 is 15.
How do you encrypt and decrypt with an affine cipher?
Encrypt MATHS with the key (a, b) = (7, 3):
M = 12: 87 mod 26 = 9, J
A = 0: 3, D
T = 19: 136 mod 26 = 6, G
H = 7: 52 mod 26 = 0, A
S = 18: 129 mod 26 = 25, Z
MATHS becomes JDGAZ. Decrypt with D(y) = 15(y − 3) mod 26:
J = 9: 90 mod 26 = 12, M
D = 3: 0, A
G = 6: 45 mod 26 = 19, T
A = 0: −45 + 52 = 7, H
Z = 25: 330 mod 26 = 18, S
When a product is negative, add multiples of 26 until it lands between 0 and 25.
How do you break an affine cipher from two known letters?
The affine cipher's main weakness is that if an attacker learns the plaintext behind two ciphertext letters, the key follows from a pair of simultaneous equations (Wikipedia: Affine cipher).
Suppose you intercept JDGAZ and guess that its second and third letters stand for A and T:
A → D gives a × 0 + b ≡ 3, so b = 3
T → G gives 19a + b ≡ 6, so 19a ≡ 3 (mod 26)
Now you need the inverse of 19. The extended Euclidean algorithm gives 11, and 19 × 11 = 209 = 8 × 26 + 1 confirms it. So a ≡ 11 × 3 = 33 ≡ 7, and the key (7, 3) is recovered.
Choose the pair with care. Had you used M → J and A → D instead, you would get 12a ≡ 6 (mod 26). Because 12 is not coprime to 26 it has no inverse, and this congruence has two solutions, a = 7 and a = 20 (check: 12 × 20 = 240 = 9 × 26 + 6). The coprime rule throws out 20, since it is even. The same Wikipedia entry notes that knowing a must be coprime to m lets an attacker discard false keys quickly.
The same inverse step is used to derive RSA key pairs (Wikipedia: Extended Euclidean algorithm); RSA also relies on modular exponentiation by square and multiply. For a cipher with perfect secrecy, see one-time pad perfect secrecy.
What should you practise next?
Find the inverses of 3, 5 and 9 modulo 26 and check each by multiplying (answers: 9, 21, 3).
Encrypt your name with the key (5, 8), then decrypt it with 5⁻¹ = 21.
Explain why a = 13 sends every letter to just two ciphertext letters.
Swap ciphertexts with a friend, reveal two letters each, and race to the key.
If a back-substitution will not come out, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer. If gcd or congruences themselves need a second look, the Fermi Concept Library explains concepts across mathematics and computing.



