🔐 Affine Cipher – Classical Encryption with Modular Mathematics
The Affine cipher is a classical monoalphabetic substitution cipher that encrypts each letter in a message by applying a mathematical formula drawn from modular arithmetic. Unlike the simple Caesar cipher, which only shifts letters, the Affine cipher also scales them — producing a more complex substitution that is nonetheless fully reversible with the right keys.
The Encryption Formula
Each letter is first converted to its zero-indexed numeric position in the alphabet (A=0, B=1, …, Z=25). The encryption formula is then applied:
where:
x = numeric value of the plaintext letter (0–25)
a = multiplicative key (must be coprime with 26)
b = additive shift key (0–25)
For example, with a=7 and b=3, the letter H (x=7) encrypts as: (7×7 + 3) mod 26 = 52 mod 26 = 0 → A.
Decryption and the Modular Inverse
Decryption reverses the transformation using the formula:
where a⁻¹ is the modular multiplicative inverse of
a modulo 26 — the unique value such that (a · a⁻¹) mod 26 = 1.The tool automatically computes the modular inverse for you. For example, 7⁻¹ mod 26 = 15 because (7 × 15) mod 26 = 105 mod 26 = 1.
Valid Values for Key a
The multiplicative key a must satisfy gcd(a, 26) = 1 — it must be coprime with 26. This is required for the modular inverse to exist, which is necessary for decryption. There are exactly 12 valid values:
| Key a | a⁻¹ mod 26 | Key a | a⁻¹ mod 26 |
|---|---|---|---|
| 1 | 1 | 15 | 7 |
| 3 | 9 | 17 | 23 |
| 5 | 21 | 19 | 11 |
| 7 | 15 | 21 | 5 |
| 9 | 3 | 23 | 17 |
| 11 | 19 | 25 | 25 |
Key Space and Security
The Affine cipher has 312 distinct key pairs (12 valid a values × 26 b values). Key pair (a=1, b=0) is the identity transformation (no encryption), and (a=1, b=n) degenerates to a Caesar cipher with shift n. The cipher is not secure by modern standards — it can be broken in milliseconds by brute force or letter frequency analysis. Use it for learning, puzzles, and educational demonstrations only.
Worked Example
Encrypting "HELLO" with a=7, b=3:
H (x=7): (7×7 + 3) mod 26 = 52 mod 26 = 0 → A E (x=4): (7×4 + 3) mod 26 = 31 mod 26 = 5 → F L (x=11): (7×11 + 3) mod 26 = 80 mod 26 = 2 → C L (x=11): (7×11 + 3) mod 26 = 80 mod 26 = 2 → C O (x=14): (7×14 + 3) mod 26 = 101 mod 26 = 23 → X Result: HELLO → AFCCX
How This Tool Works
Enter your text, choose a valid key a (use the quick-select buttons for convenience), and set key b (0–25). Switch between Encrypt and Decrypt modes using the dropdown. The tool validates your key a in real time — a green indicator confirms it is coprime with 26. Enable Cipher Alphabet Table to see the full A–Z substitution mapping, or turn on Step-by-Step Breakdown to trace the arithmetic for each character. Non-alphabetic characters (spaces, digits, punctuation) are passed through unchanged.
Educational Applications
The Affine cipher is widely used in cryptography courses to introduce modular arithmetic, modular inverses, and the concept of a key space. Its structure makes it ideal for demonstrating why certain mathematical properties (like coprimality) are essential for reversible encryption, laying the groundwork for understanding modern ciphers such as RSA.