Affine Cipher Decoder & Solver (With or Without Key)
Decode with a and b, or try every valid key and compare the English-ranked plaintexts.
By Hommer Zhao · Updated October 30, 2026
How to decode an affine cipher without the key
- Leave the tool on Decode, choose Try all keys and paste your ciphertext.
- Press Decode all 312 keys. The solver decrypts with every valid pair of a and b.
- Read the top five previews. Use Show all 312 to check lower-ranked alternatives.
- Choose Use key & read full text to open the full plaintext in the keyed decoder. Copy or download it, or use Copy plaintext on a candidate directly.
If you already have the key, choose I know a and b and enter both values. Zero is a valid b. Encode uses the same two fields to encrypt plaintext; Swap reverses the operation while keeping the key.
The solver ranks by χ² = Σ (observed − expected)² / expected over the 26 English letters, using the same reference frequencies as the Caesar decoder. Lower is a closer frequency match. This is a ranking score, not a probability or a statistical claim that the text is English. See the NIST definition of chi-squared.
Very short texts rank poorly: a plausible word can score below nonsense. Names, abbreviations, unusual vocabulary and non-English messages can also rank badly. Check meaning and context; all 312 keys remain available. The tool transforms A–Z only and leaves other characters unchanged.
The affine formula and its 312 keys
Number letters A = 0 through Z = 25. Encryption maps a plaintext index p to c = (a × p + b) mod 26. Decoding reverses the shift and multiplication: p = a⁻¹(c − b) mod 26.
The multiplier a must be coprime with 26: no common factor other than 1. Since 26 = 2 × 13, exclude even multipliers and 13. This leaves 12 values. Each pairs with one of 26 shifts, b = 0 through 25, giving 12 × 26 = 312 keys. The identity key (1, 0) is included.
An invalid multiplier makes letters collide. For example, a = 2 maps both A (0) and N (13) to the same index b, so decoding cannot distinguish them.
Modular inverses of the 12 valid a values
The inverse a⁻¹ is the number that satisfies a × a⁻¹ mod 26 = 1. It is not the fraction 1/a. Each row below is calculated by finding the integer from 1 to 25 that passes this check.
| a | a⁻¹ | Product mod 26 |
|---|---|---|
| 1 | 1 | 1 mod 26 = 1 |
| 3 | 9 | 27 mod 26 = 1 |
| 5 | 21 | 105 mod 26 = 1 |
| 7 | 15 | 105 mod 26 = 1 |
| 9 | 3 | 27 mod 26 = 1 |
| 11 | 19 | 209 mod 26 = 1 |
| 15 | 7 | 105 mod 26 = 1 |
| 17 | 23 | 391 mod 26 = 1 |
| 19 | 11 | 209 mod 26 = 1 |
| 21 | 5 | 105 mod 26 = 1 |
| 23 | 17 | 391 mod 26 = 1 |
| 25 | 25 | 625 mod 26 = 1 |
Worked decode: a note for the garden
Our example sentence uses a = 7, b = 3. The inverse is 15, since 7 × 15 = 105, which leaves remainder 1 modulo 26.
Ciphertext: Kshqt gaf kcnf qxgfkxxv gx gaf tdsyfq dmgfs cnqra.
Decoded sentence: Bring the blue notebook to the garden after lunch.
For each ciphertext letter, compute c → 15(c − 3) mod 26. The first five letters decode as follows:
| Cipher (c) | 15(c − 3) | mod 26 | Plain |
|---|---|---|---|
| K (10) | 105 | 1 | B |
| S (18) | 225 | 17 | R |
| H (7) | 60 | 8 | I |
| Q (16) | 195 | 13 | N |
| T (19) | 240 | 6 | G |
When a product is negative, use its non-negative remainder from 0 to 25. Repeat for the remaining letters; spaces and punctuation stay in place. Load the keyed example above to check the whole sentence, or load the no-key example to see its ranking.
How much ciphertext helps the ranking?
We tested the first two sentences of at least 100 A–Z letters from each of three public-domain works already used in our text experiments: the Gettysburg Address, Alice’s Adventures in Wonderland and the Declaration of Independence. Four seeded random valid keys per sentence give 24 trials. Every prefix from 1 through 100 letters is tested with the same 312-key solver.
In these trials, the first length where the exact original a and b rank #1 ranges from 7 to 24 letters (median 11). All 24 trials reach #1 at least once. A first win does not mean the key stays first as more letters are added.
| Letters | #1 / trials | #1 rate |
|---|---|---|
| 10 | 8 / 24 | 33.3% |
| 20 | 16 / 24 | 66.7% |
| 30 | 24 / 24 | 100% |
| 50 | 24 / 24 | 100% |
| 100 | 24 / 24 | 100% |
| Sentence | First #1 letters |
|---|---|
| gettysburg-1 | 11, 11, 11, 11 |
| gettysburg-2 | 24, 24, 24, 24 |
| alice-1 | 12, 12, 12, 12 |
| alice-2 | 11, 11, 11, 11 |
| declaration-1 | 7, 7, 7, 7 |
| declaration-2 | 10, 10, 10, 10 |
These percentages describe this small fixed sample, not an accuracy guarantee or a universal minimum length. Prefixes overlap. Rekeying a sentence permutes the same candidate plaintexts, so four keys usually have the same ranking except when scores tie; these are not 24 independent language samples. Ties break by ascending a, then b.
Inspect all 24 keys and their first #1 lengths
| Sentence / trial | a | b | First #1 |
|---|---|---|---|
| gettysburg-1 / 1 | 3 | 7 | 11 |
| gettysburg-1 / 2 | 21 | 10 | 11 |
| gettysburg-1 / 3 | 11 | 16 | 11 |
| gettysburg-1 / 4 | 15 | 12 | 11 |
| gettysburg-2 / 1 | 7 | 15 | 24 |
| gettysburg-2 / 2 | 15 | 3 | 24 |
| gettysburg-2 / 3 | 9 | 18 | 24 |
| gettysburg-2 / 4 | 15 | 22 | 24 |
| alice-1 / 1 | 17 | 2 | 12 |
| alice-1 / 2 | 5 | 21 | 12 |
| alice-1 / 3 | 17 | 2 | 12 |
| alice-1 / 4 | 7 | 21 | 12 |
| alice-2 / 1 | 1 | 2 | 11 |
| alice-2 / 2 | 1 | 10 | 11 |
| alice-2 / 3 | 21 | 22 | 11 |
| alice-2 / 4 | 23 | 2 | 11 |
| declaration-1 / 1 | 3 | 23 | 7 |
| declaration-1 / 2 | 23 | 17 | 7 |
| declaration-1 / 3 | 5 | 20 | 7 |
| declaration-1 / 4 | 1 | 15 | 7 |
| declaration-2 / 1 | 5 | 2 | 10 |
| declaration-2 / 2 | 7 | 11 | 10 |
| declaration-2 / 3 | 21 | 11 | 10 |
| declaration-2 / 4 | 9 | 12 | 10 |
Sentences, sources and how to reproduce the table
The sentences are chosen in source order using only their length. No short sentence is repeated or padded. Text is reduced to uppercase A–Z, and every trial retains its key for all prefix lengths. Keys use xorshift32 with seed 20261030; draw a from the sorted 12 valid multipliers, then b from 0–25, with replacement.
- gettysburg-1: Four score and seven years ago our fathers brought forth, on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Gettysburg Address (1863), Bliss copy
- gettysburg-2: Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. Gettysburg Address (1863), Bliss copy
- alice-1: Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversations?” Alice’s Adventures in Wonderland (1865), opening of chapter I
- alice-2: So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her. Alice’s Adventures in Wonderland (1865), opening of chapter I
- declaration-1: When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation. Declaration of Independence (1776), opening passage
- declaration-2: We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. Declaration of Independence (1776), opening passage
Download all ranks, keys, sentences and methodology (JSON). Recompute from the project source with node --experimental-strip-types scripts/affine-benchmark.ts. The regeneration test checks both saved copies against the calculation.
Use affine for learning and puzzles
Affine is a monoalphabetic substitution: each letter always maps to the same letter. Trying 312 keys is enough to enumerate its entire key space, so it is useful for exploring modular arithmetic and classical puzzles, not protecting sensitive information. Encoding and decoding here run locally in your browser.
Frequently asked questions
What is the affine cipher?
It is a substitution cipher that encrypts each letter with the formula E(x) = (a·x + b) mod 26, using a multiplicative key a and an additive key b. It generalises the Caesar cipher.
Why must a be coprime with 26?
So the mapping is reversible. If a shares a factor with 26 (an even number or a multiple of 13), two letters encrypt to the same one and you cannot decrypt uniquely. The 12 valid values are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25.
How do you decrypt an affine cipher?
With D(y) = a⁻¹·(y − b) mod 26, where a⁻¹ is the modular inverse of a. For a = 5 the inverse is 21, so with b = 8, R (17) decrypts to 21·(17−8) mod 26 = 7 = H.
Is the affine cipher the same as the Caesar cipher?
The Caesar cipher is the special case a = 1, which leaves only the shift b. Any a other than 1 also scales the alphabet, which is what makes it affine rather than a pure shift.
Is the affine cipher secure?
No. With only 312 possible keys it is trivially brute-forced, and as a monoalphabetic cipher it is broken by frequency analysis. It is for learning and puzzles, not real protection.
How do you decode an affine cipher without the key?
Choose Decode, select Try all keys, paste the ciphertext and press Decode all 312 keys. The solver tries every valid a and b and ranks the plaintexts by chi-squared English letter frequency. Read the top five, or expand Show all 312 to inspect the rest. Very short texts rank poorly; the lowest score does not prove that a key is correct.
Why are there only 312 affine keys?
Over A–Z, a must be coprime with 26, giving 12 possible multipliers. The shift b can be any integer from 0 through 25. That gives 12 × 26 = 312 distinct key pairs, including a = 1, b = 0, which leaves the text unchanged.
What is the inverse of a in the affine cipher?
It is the integer a⁻¹ for which a × a⁻¹ leaves remainder 1 when divided by 26. For example, the inverse of 7 is 15 because 7 × 15 = 105 = 4 × 26 + 1. It is a modular inverse, not the ordinary fraction 1/a. Multiply c − b by this inverse and reduce modulo 26 to decode.