The Affine cipher is a monoalphabetic substitution cipher in which each letter is mapped to a number, transformed by a simple linear function, and mapped back to a letter. It combines a multiplicative key and an additive key, making it more flexible than a plain Caesar shift while remaining easy to understand. This tool encrypts and decrypts text instantly in your browser with no upload.
How it works
Convert each letter to a number with A=0 through Z=25. Encryption applies the function E(x) = (a·x + b) mod 26, where a is the multiplicative key and b is the additive key. The result is mapped back to a letter.
Decryption reverses this with D(y) = a⁻¹·(y − b) mod 26, where a⁻¹ is the modular multiplicative inverse of a modulo 26. For the inverse to exist, a must be coprime with 26, so the tool only offers the valid values 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25.
Example
With a = 5 and b = 8, encrypt the letter A (x = 0): (5·0 + 8) mod 26 = 8, which is the letter I. Encrypting the phrase AFFINE CIPHER produces IHHWVC SWFRCP. Decrypting uses a⁻¹ = 21 (since 5·21 = 105 ≡ 1 mod 26) to recover the original text.
Valid key pairs and why they matter
The multiplier a must be coprime with 26 — meaning gcd(a, 26) = 1. Because 26 = 2 × 13, any even number and any multiple of 13 fail this test and produce collisions. The twelve valid values for a are: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25. When a = 1 the cipher reduces to a plain Caesar shift with offset b. The additive key b can be any value from 0 to 25, giving 12 × 26 = 312 possible keys in total.
Key a | Modular inverse a⁻¹ |
|---|---|
| 3 | 9 |
| 5 | 21 |
| 7 | 15 |
| 9 | 3 |
| 11 | 19 |
| 25 | 25 |
Practical notes and common mistakes
Non-alphabetic characters pass through unchanged. Spaces, digits, and punctuation are not encrypted, so sentence structure stays visible — a useful property for exercises but a hint to a cryptanalyst.
Letter case is preserved. The tool works on the underlying letter (A–Z), so an uppercase A and a lowercase a both encrypt to the same ciphertext letter, returned in the original case.
Breaking the Affine cipher. With only 312 key combinations, a brute-force attack is trivial even by hand. On messages longer than a few dozen characters, frequency analysis also works: in English text, the most common ciphertext letter likely maps to E (frequency ~13%), and the second most common to T or A, which narrows the key quickly.
Use the Affine cipher as a learning aid for modular arithmetic and cryptography fundamentals, not as protection for real secrets.