What Is the Affine Cipher?
The affine cipher is a monoalphabetic substitution cipher that maps each letter of the alphabet to another using a linear function over modular arithmetic. Assign the letters numbers A = 0, B = 1, …, Z = 25; then the ciphertext letter for a plaintext letter P is C = (a·P + b) mod 26, where a and b are the two keys. The "mod 26" part means the result wraps around the alphabet, exactly like clock arithmetic. The name comes from the mathematical term affine transformation: a scaling by a followed by a shift by b, applied to the alphabet considered as numbers on a circle.
Seen this way, the Caesar cipher is just the affine cipher with the multiplier fixed at a = 1 — only the shift remains. Allowing a to vary is what stretches the alphabet out of order: with a = 5, plaintext letters that sit side by side (values 0 and 1) become ciphertext letters five apart, which destroys the one-letter-ahead rhythm of a pure Caesar shift. The concept appears throughout classical and medieval cryptography in various guises, and it remains one of the first "real" mathematical ciphers taught in any introduction to the subject, because it exercises the two ideas that show up everywhere in modern crypto: modular arithmetic and one-to-one mappings.
Why the Multiplier Must Be Coprime With 26
Decryption has to reverse the formula, which means dividing by a mod 26 — in practice, multiplying by a's modular inverse: the value a⁻¹ such that (a · a⁻¹) mod 26 = 1. That inverse exists exactly when a and 26 share no common factor (their greatest common divisor is 1). If they do share a factor, the mapping is not one-to-one: several different plaintext letters map to the same ciphertext letter, and no unique decryption is possible. Because 26 = 2 · 13, the valid multipliers are precisely the values that are odd and not divisible by 13 — twelve of the twenty-six possibilities.
| Multiplier a | Modular inverse a⁻¹ |
|---|---|
| 1 | 1 |
| 3 | 9 |
| 5 | 21 |
| 7 | 15 |
| 9 | 3 |
| 11 | 19 |
| 15 | 7 |
| 17 | 23 |
| 19 | 11 |
| 21 | 5 |
| 23 | 17 |
| 25 | 25 |
Notice 25 is its own inverse, as is 1 — and 5 pairs with 21, 7 with 15, and so on. The tool uses exactly this table (computed, not hard-coded) to decrypt, and it refuses to proceed when a is not in the list.
Step-by-Step Example
Encrypt the phrase "affine cipher" with a = 5 and b = 8. Take the first two letters: A has value 0, so C = (5·0 + 8) mod 26 = 8, which is I. F has value 5, so C = (5·5 + 8) mod 26 = 33 mod 26 = 7, which is H. Continue across the phrase:
| Letter | P | 5·P + 8 | Cipher |
|---|---|---|---|
| a | 0 | 8 | i |
| f | 5 | 33 → 7 | h |
| f | 5 | 33 → 7 | h |
| i | 8 | 48 → 22 | w |
| n | 13 | 73 → 21 | v |
| e | 4 | 28 → 2 | c |
The full result is "ihhwvc swfrcp" (the space passes through untouched). To decrypt, multiply by a⁻¹ = 21: for the first ciphertext letter i (8), P = 21 · (8 − 8) mod 26 = 0, the letter a — and continuing recovers "affine cipher". Type either string into the tool to confirm.
Example Reference Table
| Plain Text | a, b | Ciphertext |
|---|---|---|
| affine cipher | 5, 8 | ihhwvc swfrcp |
| HELLO | 7, 3 | AFCCX |
| Hello, World! | 7, 3 | Afccx, Bxscy! |
| ATTACK AT DAWN | 5, 8 | IZZISG IZ XIOV |
| SimplyCalculated | 9, 4 | KyijzmWezwczetof |
Paste any plain-text row with its keys into the tool to confirm the ciphertext, or paste the ciphertext with the same keys to confirm it decodes back.
Security and Cryptanalysis
The affine cipher is a monoalphabetic substitution, so its security ceiling is the same as the Caesar cipher's: letter frequencies survive intact, and a simple frequency analysis maps the ciphertext alphabet back onto English within minutes. Its only advantage over Caesar is the key space: 12 valid multipliers times 26 offsets gives a few hundred distinct ciphers instead of 25 — still trivially small for exhaustive search, and still vulnerable to a single known plaintext–ciphertext pair, which solves the two equations for a and b directly. It occupies an honest place in cryptographic history as one of the earliest algebraic ciphers, and in the modern classroom as the standard first exercise in modular inverses. For any real secrecy, it has been obsolete for centuries.
Troubleshooting & Common Mistakes
An error message appears about the multiplier
The "a" value shares a factor with 26 (or is 0), so no inverse exists and the cipher cannot be decrypted. Enter one of the twelve valid values (1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25). The tool leaves your output untouched until you do, rather than writing garbage over it.
My decryption is gibberish
Both keys must match the ones used to encrypt. Because negative and large keys are wrapped into 0–25 first, "a = 33" and "a = 7" are the same key — but "a = 7" and "a = 17" are not. Re-enter the exact keys.
What happens to numbers, punctuation, and spaces in my text?
They pass through unchanged — only letters A–Z participate in the arithmetic. This is deliberate and matches classical usage; it also means the cipher's letter-frequency fingerprints are perfectly preserved.
How This Cipher Compares to Other Classical Ciphers
The Affine cipher is one of several classical ciphers covered on this site. Its two-key linear function generalizes the single-shift idea behind Caesar — the table below shows how it and every other cipher here actually transform text.
| Cipher | Mechanism | What Makes It Different |
|---|---|---|
| Caesar Cipher | Substitution | Shifts every letter by one fixed number (the key) through the alphabet — the simplest classical substitution cipher. |
| Atbash Cipher | Substitution (fixed) | A special case of substitution with no key at all — it always mirrors the alphabet (A↔Z, B↔Y, ...). |
| Vigenère Cipher | Polyalphabetic substitution | Repeats a keyword to shift each letter by a different amount, defeating simple frequency analysis that breaks Caesar. |
| Beaufort Cipher | Polyalphabetic substitution (reciprocal) | A variant of Vigenère's idea that is self-reciprocal — the same operation both encrypts and decrypts. |
| Playfair Cipher | Digraph substitution | Encrypts letters two at a time using their positions in a 5×5 key square — the first practical digraph cipher (1854). |
| Affine Cipher (this page) | Mathematical substitution | Encrypts using a linear function (ax + b mod 26) with two keys instead of one shift value. |
| ROT47 | Substitution (extended range) | Applies a Caesar-style shift across the full printable ASCII range, not just letters — used to obfuscate text online (e.g. spoilers), not for security. |
| Rail Fence Cipher | Transposition | Rearranges letter order in a zigzag pattern instead of substituting letters — a fundamentally different technique from every cipher above. |
| A1Z26 | Encoding (not a cipher) | Simply maps each letter to its position number (A=1...Z=26) — a puzzle/encoding convention, not intended for real secrecy. |
| NATO Phonetic Alphabet | Encoding (not a cipher) | Replaces each letter with a spoken code word (Alfa, Bravo, Charlie…) for reliable voice transmission on noisy radios — built for clarity, not secrecy. |