Cabinet Noir>Learn / Affine cipher

Not sure which cipher? Identify it
A substitution driven by the line y = a·x + b: multiply the letter, then shift, all mod 26.
Typesubstitution cipher
Breaking iteasy to break by hand
EraAntiquity — today
NameA mathematical generalisation of the Caesar shift, named after the affine function ax + b.
Modesencrypt · decrypt · cryptoanalysis

Learn

Affine cipher

How the Affine cipher works

The idea in one sentence

The Affine cipher is a Substitution driven by a straight line: every letter is first stretched by a multiplier and then shifted, all inside Modular arithmetic — the arithmetic of wrapping around at 26.

The recipe

  1. Choose the key: a multiplier a and a shift b.

  2. The multiplier must be coprime with the alphabet length: for the 26-letter alphabet that leaves twelve values (1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25). Anything sharing a factor with 26 would squash several letters onto one, and the tool refuses to build that alphabet.

  3. Number the letters from 0 (A = 0). Encrypt letter number x into letter number (a·x + b) mod 26.

  4. Read the new letter off the alphabet.

A worked example

Step 1 of 13
Plaintext letter
a·x + b (mod 26)
Result
S
9
J

With multiplier 3 and shift 7, S (letter 18) lands on (3·18 + 7) mod 26 = 9 — J. E (letter 4) lands on 19 — T. The middle column is the honest heart of the cipher: a multiplication and an addition, wrapping at 26.

The formula

encrypt: y = (a·x + b) mod 26
decrypt: x = a⁻¹·(y − b) mod 26

Decryption needs the inverse of the multiplier — the number that multiplied by a gives 1 mod 26. For a = 3 the inverse is 9, because 3·9 = 27 ≡ 1 (mod 26). Only multipliers coprime with 26 have an inverse — which is exactly why the key space is 12 × 26 = 312 keys.

Why two numbers are better than one

A pure shift (Caesar, a = 1) slides the alphabet but preserves differences between letters: the pattern A-B-C is visible under any shift. The multiplier reshapes the spacing — B is not "one step after A" any more. Every gap changes with the multiplication, which is why the same frequency analysis that fells Caesar needs more work here.

Try it in the tool

Step-by-step guides

Encrypt your first message

  1. Type your message into the message input.

  2. Set the multiplier in the multiplier field — try 3, a classic choice that is coprime with 26.

  3. Set the shift in the shift field — the Caesar-style add-on, any value from 0 to 25.

  4. Read the result in the output field. The same plaintext letter always maps to the same ciphertext letter, but the mapping is no longer a simple slide.

Decrypt a message you received

  1. Paste the cryptogram into the message input and press the swap button the swap button.

  2. Enter the same multiplier the multiplier field and shift the shift field the sender used. The tool computes the inverse multiplication for you — no modular arithmetic on your side.

  3. Read the original message in the output field.

Break a message without the key

The Affine cipher is still a single fixed Substitution, so it falls to the classic attack — and, better, to a shortcut:

  1. If you know two plaintext–ciphertext letter pairs, you can solve the straight line directly: subtract the two equations to cancel b, then divide — which means multiplying by the modular inverse — to get a.

  2. With a known, recover b from either pair.

  3. Enter both numbers in the multiplier field and the shift field, switch to decrypt mode, and read the message.

  4. No known letters? Fall back to Frequency analysis: the commonest ciphertext letter should be E (or T, or A), and each guess of a pair of candidates pins down a candidate key.

Breaking the Affine cipher

Affine is a monoalphabetic Substitution with a mathematical backbone — and the backbone is exactly what gives it away. Two known letters collapse the whole key. This page shows both routes.

Try it in the tool

The flaw: a straight line leaves a trace

Two parameters, one linear rule: y = (a·x + b) mod 26. Every letter pair of the key lies on the same line. Two points determine a line — so two known letter pairs determine the key.

Attack 1 — two known letters solve it

  1. Guess or obtain two plaintext–ciphertext pairs, say x₁ → y₁ and x₂ → y₂.

  2. Subtract: y₁ − y₂ ≡ a·(x₁ − x₂) (mod 26). Divide by x₁ − x₂ — in Modular arithmetic, dividing means multiplying by the modular inverse — to recover a.

  3. Substitute back to recover b.

  4. Enter the key and decrypt. Crib words like a known opening give you the pairs for free.

Attack 2 — frequency analysis, as always

Without known letters, the cipher is still a fixed Substitution: the commonest ciphertext letter corresponds to E, the second to T or A, and each guessed correspondence is a hypothesis about (a, b) that you can test on the next few letters. There is no period to find, no structure beyond the mapping itself — Frequency analysis works exactly as it did against Caesar, only the candidate alphabets are less orderly.

Why the coprimality rule matters

The multiplier must be coprime with 26 — otherwise two plaintext letters collide onto one ciphertext letter and the message cannot be uniquely decrypted. That requirement halves the key space to 312 combos, and it is the first place a beginner meets the idea that carries modern cryptography: invertibility inside modular arithmetic. The same arithmetic, taken vastly more seriously, secures today's public-key exchanges.

From Caesar to Affine to RSA

Caesar's shift is the Affine cipher with a = 1. Affine adds one multiplication, and with it the concept of a modular inverse. Keep raising the stakes — exponentiation instead of multiplication — and you arrive at RSA's public-key mathematics. The toy cipher and the bank-grade system share one grammar.

Glossary

Plaintext — The original readable message before encryption.
Ciphertext — The scrambled message produced by encryption.
Key — The secret that controls how the text is scrambled.
Alphabet — The set of letters the cipher works with.
Substitution — Replacing each letter with another one.
Modular arithmetic — Counting that wraps around at a fixed size: after the maximum you start again at zero, like hours on a clock.
Frequency analysis — Studying how often letters appear; comparing the counts with the language reveals the plaintext.
Brute force — Trying every possible key until the message makes sense; possible only when the key space is small.