SimplyCalculated.org

Affine Cipher Encoder & Decoder

One small equation — C = (a·P + b) mod 26 — turns each letter of your message into another by multiplying and shifting. It is the Caesar cipher's algebraic big sibling, with two numeric keys instead of one. Type on either side to encrypt or decrypt instantly. Everything runs locally in your browser.

100% private: everything is processed in your browser's memory — nothing you enter is uploaded to a server, logged, or stored.

Affine Cipher

The cipher is C = (a·P + b) mod 26. The multiplier a must not share a factor with 26 (valid: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25) — otherwise the cipher cannot be undone. Both keys wrap into 0–25 automatically, and non-letters pass through unchanged.

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.

Frequently Asked Questions

Which values of "a" are valid, and why only those?
Valid multipliers are exactly the values coprime with 26: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. Decryption divides by a, which in modular arithmetic means multiplying by a's modular inverse — and an inverse exists only when a and 26 share no common factor. If you use an invalid value, two different plaintext letters map to the same ciphertext letter, so the cipher cannot be undone. The tool refuses to compute with an invalid "a" and shows an inline message instead.
Why does an invalid "a" even matter — can't you just encrypt anyway?
You can perform the arithmetic, but the result is not decryptable: because 26 is even and an invalid "a" shares a factor with it, the mapping collapses. For example a = 2 maps both A and N to the same letter, so any ciphertext letter has two possible plaintexts and no unique answer exists. The tool blocks this rather than silently producing a message nobody can read back.
How many different affine ciphers are there?
Exactly 12 valid multipliers times 26 offsets, minus a few duplicates that produce identical mappings (a = 1 and a = 25 with the right offset can coincide). In practice there are about 300 distinct encryption rules — a small number that makes exhaustive search trivial for a computer, which is one reason the cipher is only a historical curiosity today.
How is the affine cipher related to the Caesar cipher?
The affine cipher generalizes Caesar's cipher. A Caesar cipher is C = (P + b) mod 26 — the special case of the affine formula with a = 1. Setting a to any other valid value adds the extra scrambling step of stretching the alphabet, so that even plaintext letters one apart may end up far apart in the ciphertext. The "b" term is the same shifting concept either way.
What does a negative "a" or "b" mean?
Keys are wrapped into 0–25 before any arithmetic, so a negative value is just a different key. For example a = −21 behaves exactly like a = 5, because −21 and 5 differ by a multiple of 26. Validity is judged on the wrapped value, so a = −21 is fine while a = −2 (which wraps to 24, sharing a factor with 26) is rejected.
Is the affine cipher used anywhere real today?
Not for secrecy — its tiny key space and preserved letter frequencies make it trivially breakable. It survives as a staple of cryptography education, puzzle hunts, and escape rooms, and as a building block in discussions of modular arithmetic and one-to-one mappings. Understanding it is a genuinely useful stepping stone toward grasping how modern ciphers handle keys and inverses.

Formula last verified August 22, 2026 against our published methodology .