Encrypt with a = 5, b = 8
AFFINE CIPHER
IHHWVC SWFRCP
A (0) → 5·0 + 8 = 8 = I; F (5) → 5·5 + 8 = 33 mod 26 = 7 = H.
Encrypt or decrypt with the affine cipher, E(x) = (ax + b) mod 26. Choose the keys a and b, or let the solver try all 312 keys and rank them by how English the result looks.
Most likely keys
AFFINE CIPHER
IHHWVC SWFRCP
A (0) → 5·0 + 8 = 8 = I; F (5) → 5·5 + 8 = 33 mod 26 = 7 = H.
IHHWVC SWFRCP
AFFINE CIPHER
Decryption multiplies by 21, the inverse of 5 modulo 26: 5 × 21 = 105 = 4 × 26 + 1.
attack at dawn
dwwdfn dw gdzq
With a = 1, the affine cipher is just a shift by b, the Caesar cipher.
The affine cipher turns each letter into a number, multiplies it by a key a, adds a key b, and takes the result modulo 26. It is a monoalphabetic substitution cipher: each letter always maps to the same ciphertext letter, but the mapping is scrambled by multiplication instead of a simple shift. The Caesar cipher (a = 1) and the Atbash cipher (a = 25, b = 25) are both special cases.
With A = 0, B = 1 … Z = 25:
encrypt: E(x) = (a·x + b) mod 26decrypt: D(y) = a⁻¹ · (y − b) mod 26a⁻¹ is the modular inverse of a: the number that gives 1 when multiplied by a modulo 26. It only exists when a and 26 share no common factor, which is why a cannot be even or 13.
| a | 1 | 3 | 5 | 7 | 9 | 11 | 15 | 17 | 19 | 21 | 23 | 25 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| a⁻¹ | 1 | 9 | 21 | 15 | 3 | 19 | 7 | 23 | 11 | 5 | 17 | 25 |
With a = 2, for example, both A (0) and N (13) would encrypt to the same letter, so the message could not be decrypted.
There are 12 valid values of a and 26 values of b, so 12 × 26 = 312 keys. One of them (a = 1, b = 0) leaves the text unchanged. That is small enough to try every key, which is exactly what the solver on this page does: it decrypts the message with all 312 keys and ranks the results with a chi-squared test against normal English letter frequencies.
With a known plaintext, it is even easier: two known letter pairs give two equations, which can be solved for a and b directly.
The affine cipher offers no real security. With only 312 keys and the same letter frequencies as the plaintext, it falls to brute force or frequency analysis in seconds.
const inverse = (a) => [...Array(26).keys()].find((x) => (a * x) % 26 === 1); function affine(text, a, b, decrypt = false) { const ai = inverse(a); return text.replace(/[a-z]/gi, (c) => { const base = c <= 'Z' ? 65 : 97; const x = c.charCodeAt(0) - base; const y = decrypt ? (ai * (x - b + 26)) % 26 : (a * x + b) % 26; return String.fromCharCode(base + y); });} affine('AFFINE CIPHER', 5, 8); // "IHHWVC SWFRCP"affine('IHHWVC SWFRCP', 5, 8, true); // "AFFINE CIPHER"Why can't I choose a = 2, 4, 13 or 26?
*a* must have no common factor with 26, otherwise two letters encrypt to the same letter and decryption is impossible. Only 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25 work.
Decryption gives nonsense with the right a and b
Check the letter numbering. This tool uses A = 0. Some textbooks use A = 1, which gives different results with the same keys.
The solver's top result is wrong
Short messages do not have reliable letter frequencies. Look further down the list of 10 candidates for the readable one.
Choose Decrypt and paste the message. The tool tries all 312 keys and lists the 10 most English-looking results. Click one to apply its a and b.
Encryption is E(x) = (ax + b) mod 26, and decryption is D(y) = a⁻¹(y − b) mod 26, where a⁻¹ is the modular inverse of a.
Yes. With a = 1 the affine cipher is a shift by b, which is the Caesar cipher. Atbash is also affine, with a = 25 and b = 25.
312: 12 valid values of a times 26 values of b.
No. Everything runs in your browser.
Free, private developer tools that run in your browser. No signup, nothing uploaded, no waiting.
Tip: press Ctrl D to bookmark dev247.