How the affine cipher works
The affine cipher turns each letter into a number (A = 0, B = 1 β¦ Z = 25), multiplies it by a, adds b, and wraps around the alphabet:
E(x) = (aΒ·x + b) mod 26
To decrypt, you undo both steps. Subtract b, then multiply by the inverse of a: the number that multiplied by a gives 1 (mod 26). For a = 5 the inverse is 21, because 5 Γ 21 = 105 = 4 Γ 26 + 1.
D(y) = aβ»ΒΉΒ·(y β b) mod 26
Only 12 values of a have an inverse: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. Any even number, or 13, would send two letters to the same place, so the message could not be read back.
Example
With a = 5 and b = 8, the letter H (7) becomes 5 Γ 7 + 8 = 43, and 43 mod 26 = 17, which is R. So HELLO becomes RCLLA.
Why it is easy to break
Every letter always turns into the same cipher letter, so letter frequency survives. The most common cipher letter is probably E, and once two letters are pinned down the key is fixed. Caesar shifts (a = 1) and the Atbash mirror (a = 25, b = 25) are both special cases of the affine cipher.
Related tools
- Caesar cipher: the a = 1 case
- Atbash cipher: the a = 25, b = 25 case
- Frequency analysis: find the most common letters first
- Keyed substitution: a fully scrambled alphabet