Affine Cipher

A mathematical substitution cipher defined by two numbers a and b (mod 26).

Family: Substitution (mathematical) Era: Classical / early modern (education + puzzles) Strength: Weak

History & context

The affine cipher generalizes Caesar by adding a multiplication step. Instead of shifting letters by a fixed amount, it multiplies the letter index by a and then adds b, all modulo 26. This creates a larger keyspace than Caesar, but it remains monoalphabetic substitution, so classical frequency analysis still breaks it quickly.

How Affine Cipher works

Convert letters to numbers A=0..Z=25. Choose parameters a and b. Compute ciphertext index as (a*x + b) mod 26. To decode, compute x = a^{-1} * (y - b) mod 26, where a^{-1} is the modular inverse of a modulo 26.

Core rules

Worked example

Let a=5, b=8. Plaintext: AFFINE Ciphertext: IHHWVC Because: E(A=0) = (5*0+8)=8 → I E(F=5) = (5*5+8)=33 mod 26=7 → H

How to encode / decode

Step-by-step

  1. Choose a and b (a must be coprime with 26).
  2. Map letters A=0..Z=25.
  3. Compute (a*x + b) mod 26 for each letter.
  4. Map result back to letters.
💡 Tip: If decoding fails, the most common cause is choosing an invalid a (not coprime with 26).

How to break a Affine Cipher

Affine is still a monoalphabetic substitution, so it is breakable via frequency analysis. Additionally, the keyspace is small enough to brute force: there are 12 valid a values and 26 b values, so only 312 possible keys.

Practical checklist

What frequency looks like

Affine preserves frequency distribution like Caesar because it is still monoalphabetic substitution. So ciphertext has strong peaks and IoC close to English. The difference is that the substitution is not a simple shift; it’s a permutation defined by the linear function.

Signals to look for:
  • IoC is close to English (~0.066).
  • Single-letter frequencies remain strongly peaked.
  • Brute force reveals readable output very quickly because the keyspace is tiny.

Common mistakes

Variants

Practice

Try brute forcing Affine keys and compare scoring methods (word hits vs bigram scoring).

Try these prompts

FAQ

Because decoding needs a modular inverse of a modulo 26, which exists only if gcd(a,26)=1.
Slightly (bigger keyspace), but still monoalphabetic and easy to break.
312 total (12 valid a values × 26 b values).