Free online Affine cipher tool. Encrypt and decrypt text using E(x) = (ax + b) mod 26 with configurable a and b keys. Supports automatic key validation.
Not sure which cipher you have? Use the Cipher Identifier to auto-detect cipher types from unknown ciphertext using frequency analysis and Index of Coincidence.
The Affine cipher is a monoalphabetic substitution cipher that encrypts each letter using a linear function modulo 26. It generalizes the Caesar cipher (which is the special case where the multiplier equals 1) by adding a multiplicative component, expanding the key space from 25 to 311 distinct keys.
Number the alphabet A=0, B=1, ..., Z=25. The encryption function is:
E(x) = (a · x + b) mod 26
where a and b are the two halves of the key. Decryption is:
D(y) = a⁻¹ · (y − b) mod 26
where a⁻¹ is the modular multiplicative inverse of a modulo 26.
For decryption to work, a must be coprime to 26 — that is, gcd(a, 26) = 1. Since 26 = 2 × 13, any a that is even or a multiple of 13 has no inverse mod 26 and breaks decryption. The 12 valid values of a are:
1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25
The shift b can be any integer from 0 to 25, giving 12 × 26 = 312 key pairs. Subtracting the trivial identity (a=1, b=0) leaves 311 useful keys.
Encrypt "HELLO" with a=5, b=8.
Ciphertext: "RCLLA". To decrypt, compute a⁻¹: 5 · 21 = 105 = 4·26 + 1, so 5⁻¹ ≡ 21 (mod 26). Then D(y) = 21 · (y − 8) mod 26 recovers each plaintext letter.
The Affine cipher does not have a single inventor or famous historical use — it is best understood as the natural algebraic generalization of the Caesar cipher. It became a standard textbook example in 20th-century cryptography courses because it cleanly illustrates modular arithmetic, multiplicative inverses, and why key constraints matter. The requirement that gcd(a, 26) = 1 is the first place most students encounter the concept of a unit in a modular ring.
For classroom and self-study use only. The Affine cipher is excellent for teaching:
It also shows up in CTF challenges, puzzle hunts, and recreational cryptography, often as the second or third layer in a stacked cipher.
The Affine cipher offers essentially no real-world security. Two independent attacks defeat it:
A known-plaintext attack with just two correct letter pairs gives a linear system that pinpoints (a, b) in one step.
If you have ciphertext and suspect Affine but aren't sure, the Cipher Identifier can flag the monoalphabetic fingerprint and rank Affine against Caesar, ROT13, and general substitution.