- Translator
- Affine cipher decoder
Affine cipher decoder and encoder
Decode an Affine cipher with its multiplier a and shift b, or try all 312 valid A–Z keys. The calculator also encodes messages and shows the arithmetic behind each result.
To decode an Affine cipher, subtract the shift b from each ciphertext letter’s number, multiply by the modular inverse of a, and take the remainder modulo 26. This page uses A=0 through Z=25 (WCSU, definition 6.2.1). Enter both keys below, or try every valid key if they are missing.
Decode or encode an Affine cipher
Your text and keys stay in your browser; the calculator sends no text over the network.
Enable JavaScript to use the calculator. The formulas and worked examples below remain available without it.
How do I use the Affine cipher decoder with a key?
Choose Decode ciphertext, paste your message, and enter the multiplier a and shift b supplied with the puzzle. Press Convert. Keep the original encoding keys when switching direction: the calculator finds the inverse for you. The result shows the inverse and arithmetic for the first 60 letters.
The starting example uses a=11 and b=4. Decode GCRWI to get MONEY. Choose Encode and enter that recovered word to check the return trip. Both directions reproduce the university example (University of Rhode Island). Editing text, direction, or either key clears the previous output so you do not copy a result for an old setting.
What is the Affine cipher formula?
The encryption formula is E(x) = (a × x + b) mod 26. Its reverse is D(y) = a⁻¹ × (y − b) mod 26. Here x is the plaintext letter’s number, y is the ciphertext letter’s number, and a⁻¹ is the multiplicative inverse of a (Waterloo, Example 13.5). The superscript −1 means an inverse in modular arithmetic, not an ordinary fraction.

“Mod 26” means choosing the remainder from 0 through 25. For a negative intermediate result, add multiples of 26 until you reach that range (Hope College, modular arithmetic).
What is a worked Affine cipher example?
Use the University of Rhode Island’s plaintext MONEY and key (11, 4). Number each letter, multiply by 11, add 4, then reduce modulo 26. The table below is calculated by the same module as the tool; its ciphertext is GCRWI (URI, enciphering example).
| Input | Calculation | Output |
|---|---|---|
| M (12) | 11 × 12 + 4 = 136; mod 26 = 6 | G (6) |
| O (14) | 11 × 14 + 4 = 158; mod 26 = 2 | C (2) |
| N (13) | 11 × 13 + 4 = 147; mod 26 = 17 | R (17) |
| E (4) | 11 × 4 + 4 = 48; mod 26 = 22 | W (22) |
| Y (24) | 11 × 24 + 4 = 268; mod 26 = 8 | I (8) |
For the return trip, the inverse of 11 is 19. The check is 11 × 19 mod 26 = 1. Subtract 4 before multiplying by 19; reversing that order changes the answer. URI’s deciphering example gives the recovered word MONEY (URI, deciphering example).
| Input | Calculation | Output |
|---|---|---|
| G (6) | 19 × (6 − 4) = 38; mod 26 = 12 | M (12) |
| C (2) | 19 × (2 − 4) = -38; mod 26 = 14 | O (14) |
| R (17) | 19 × (17 − 4) = 247; mod 26 = 13 | N (13) |
| W (22) | 19 × (22 − 4) = 342; mod 26 = 4 | E (4) |
| I (8) | 19 × (8 − 4) = 76; mod 26 = 24 | Y (24) |
For a second check, encode HELLO using a=5 and b=12. This calculator produces VGPPE, matching Waterloo’s example (Waterloo, page 71). Notice that both occurrences of L produce the same letter. A fixed Affine key makes a monoalphabetic substitution: each plaintext letter always has the same ciphertext partner (WCSU, section 6.2).
Which keys are valid for an Affine cipher?
The multiplier must be relatively prime to 26: its greatest common divisor with 26 must be 1. Otherwise it has no multiplicative inverse. In the range 0–25, the valid multipliers are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25. Each combines with 26 shifts, giving 312 keys (Hope College, slide 7).
These computed inverses undo each valid multiplier.
| Multiplier a | Inverse a⁻¹ |
|---|---|
| 1 | 1 |
| 3 | 9 |
| 5 | 21 |
| 7 | 15 |
| 9 | 3 |
| 11 | 19 |
| 15 | 7 |
| 17 | 23 |
| 19 | 11 |
| 21 | 5 |
| 23 | 17 |
| 25 | 25 |
For a collision you can check yourself, try the invalid rule a=2, b=0 on A and N. Their numbers are 0 and 13; doubling gives 0 and 26, both with remainder 0. Both would become A, so a decoder could not choose the original. This illustrates why coprimality matters (WCSU, multiplier restriction). The tool rejects invalid keys instead of creating an ambiguous ciphertext.
Can I use an Affine cipher decoder without a key?
Open Decode without a key and press Try every key. The calculator lists all 312 possibilities in increasing a, then b order, including the identity key (1, 0). Choose a preview and press Load selected key to inspect the full text and its arithmetic. The key-space size is small enough for exhaustive search (Hope College, slide 7).
These previews are unranked. Read them against the puzzle’s clue; the first is simply the first key tested. A short name or unfamiliar language may leave several plausible readings. This search accepts at most 2,000 UTF-16 code units; ordinary conversion accepts 10,000. Many emoji count as two units (MDN Web Docs).
Does the calculator change spaces, punctuation, I or J?
This implementation preserves case and changes only A–Z and a–z. Spaces, line breaks, digits, punctuation, emoji, and other letters pass through unchanged. I and J remain distinct, and the tool neither inserts nor strips X padding. A precomposed é stays unchanged; an ASCII e followed by a combining accent changes while its accent passes through.
Enter integer keys in the displayed ranges. This calculator rejects fractions and out-of-range values rather than silently reducing them. If another exercise numbers A as 1, removes spaces, or uses a different alphabet, check its convention before comparing answers. This tool implements the A=0 convention used in the worked sources (WCSU, sample enciphering).
How can I send Affine ciphertext in Morse?
For a puzzle-hunt or escape-room clue you can make, first encode DANGER with a=5 and b=8. The module gives XIVMCP, matching Hope College’s example (Hope College, slide 8). The player sends those encrypted letters using International Morse signals (ITU, Annex 1).
Solve the layers in reverse order: read the Morse letters with the Morse code alphabet, then enter them into this decoder. Use the Morse code translator to prepare your own clue. Is Morse code a cipher? explains the two jobs.
How is Affine related to Caesar, and is it secure?
Affine generalizes a shift cipher by adding multiplication (University of Rhode Island). Setting a=1 leaves only the shift, which you can explore in the Caesar cipher translator. For a keyword-based exercise, try the Vigenère cipher decoder.
Use Affine for learning and puzzles. Waterloo identifies its small key space as a reason it is vulnerable to brute-force attacks (Waterloo, page 73). Sending its ciphertext in Morse still leaves the same keys available to try.
Sources
- Affine Cipher: History and Description, Example, and Analysis. University of Rhode Island, checked 20 November 2026.
- PMATH 340: Elementary Number Theory, Example 13.5 and pages 71–73. University of Waterloo, F. Al-Faisal, 14 September 2026; checked 20 November 2026.
- Some Simple Cryptosystems, Part 1, slides 7–8. Hope College, 27 August 2008; checked 20 November 2026.
- Section 6.2: Affine Ciphers. Western Connecticut State University, checked 20 November 2026.
- Recommendation ITU-R M.1677-1, International Morse code. ITU Radiocommunication Sector, October 2009.
- String: length. MDN Web Docs, checked 20 November 2026.