Advertisement

Affine cipher

Encrypt and decrypt with the affine cipher. Set multipliers a and b; each letter maps through E(x) = (ax + b) mod 26.

Encrypt or decrypt

E(x) = (a*x + b) mod 26. The multiplier a must be coprime to 26.

Valid: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25.
Nothing is uploaded. Everything is computed locally in your browser.
Affine

The result appears below. Case is preserved; other characters pass through.

-
Advertisement

Math behind it

The affine cipher encrypts each letter x (A=0 through Z=25) as E(x) = (a*x + b) mod 26. The multiplier a must be coprime to 26 (one of 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25); otherwise several letters collide and decryption is impossible. Decryption uses the modular inverse of a.

Tiny key space

Only 12 valid values of a times 26 values of b gives 312 possible keys, brute-forced in milliseconds. Frequency analysis also breaks it directly since it is monoalphabetic. The affine cipher is a teaching tool for modular arithmetic, not a security mechanism.

Affine Cipher FAQs

Why is a=13 rejected?
a must be coprime to 26 or the mapping is not reversible: with a=13, both A and N would encrypt to N. Valid a values are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25.
What is the modular inverse?
The number a^-1 with a*a^-1 = 1 mod 26, used to decrypt. For a=5 it is 21, since 5*21 = 105 = 1 mod 26.
How many possible keys?
12 choices for a times 26 for b = 312 keys. A computer tries them all instantly, which is why the cipher is insecure.
Does it handle lowercase?
Yes, case is preserved and non-letters pass through unchanged.
Affine vs Caesar?
Caesar is the special case a=1. The affine cipher generalizes it with the multiplier, but both fall to frequency analysis.
Advertisement