Quiz Entry - updated: 2026.07.14
How does the affine cipher work, and what is its key space?
The affine cipher encrypts as $y = (a \cdot x + b) \mod 26$ with key $k = (a, b)$, and its key space is only 312 keys (or 275 excluding trivial cases).
Encryption: $y = e_k(x) = (a \cdot x + b) \mod 26$ Decryption: $x = d_k(y) = a^{-1} \cdot (y - b) \mod 26$
Constraints:
- $a$ and 26 must be coprime: $\gcd(a, 26) = 1$
- Since $26 = 2 \times 13$, the valid values for $a$ are: $\{1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25\}$ → 12 values
- $b$ can be any value from 0 to 25 → 26 values
- Total key space: $12 \times 26 = 312$ keys
- Excluding the trivial cases — no scaling ($a = 1$) and no shift ($b = 0$) — leaves $11$ values for $a$ and $25$ for $b$: $11 \times 25 =$ 275 non-trivial keys
Example: With $k = (5, 20)$:
- ATTACK → $0,19,19,0,2,10$
- Encrypt: $(5 \cdot x + 20) \mod 26$ → $20,11,11,20,4,18$ → ULLUES
Tip: With only 312 keys, a brute-force attack is trivially easy — a computer checks all keys in microseconds. This cipher is only useful for teaching concepts, never for real security.
Go deeper:
Affine cipher (Wikipedia) — the formula, the coprime constraint, and the 312-key space.