Affine Cipher Encoder
Encode text with the affine cipher right in your browser, or switch to any other classical cipher.
Input
Output
Guide
What is the affine cipher?
The affine cipher swaps each letter for another one using a small formula. Its key is two numbers, a multiplier a and a shift b. Each letter's number, from A = 0 to Z = 25, is multiplied by a, then b is added, counting around the alphabet. A given letter turns into the same cipher letter every time, so it's a substitution cipher, and the Caesar and Atbash ciphers are affine ciphers with special keys. Today it turns up in math classes, puzzle hunts and escape rooms, as it shows how a cipher can be written as a formula.
How the formula works
The formula is (a × x + b) mod 26, where x is the letter's number. Mod 26 means counting around the alphabet: take away 26 until the result is below 26. With multiplier 5 and shift 8, the word cake works out like this:
| Letter | x | 5 × x + 8 | Mod 26 | Cipher letter |
|---|---|---|---|---|
| c | 2 | 18 | 18 | s |
| a | 0 | 8 | 8 | i |
| k | 10 | 58 | 6 | g |
| e | 4 | 28 | 2 | c |
So cake becomes sigc. For the whole alphabet, this key gives the table below, where L and Y happen to stay the same:
Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher: INSXCHMRWBGLQVAFKPUZEJOTYDTool description
Write your message in the Plain text box, and its affine cipher text shows up in the Cipher text box while you type. Below the text, the Cipher menu shows Affine Cipher, and under it you set the key: the Multiplier (a), which starts at 5, and the Shift (b), which starts at 8. Insert sample puts in See you at the park at noon and sets the key back to 5 and 8, which gives Ucc yae iz zrc fipg iz vaav. The Cipher menu also offers other classical ciphers, such as Beaufort or Playfair. The swap button between the boxes sends the result back into the input to decode it, and the Affine Cipher Decoder starts that way. The cipher runs in your browser.
Examples
| Plain text | Multiplier (a) | Shift (b) | Cipher text |
|---|---|---|---|
Lemonade at the lake |
5 | 8 | Lcqavixc iz zrc ligc |
Bake 12 muffins |
3 | 0 | Daem 12 kippync |
Fresh bread, warm rolls |
11 | 4 | Hjwud pjwel, mejg jcvvu |
The garden is in bloom |
9 | 20 | Jfe wurveh oa oh dpqqy |
Bubbles in the bath |
21 | 11 | Gpggirz xy ucr gluc |
Picking the key
- The Multiplier (a) list skips the even numbers and 13, which share a factor with 26. They would turn two letters into the same one, so the message couldn't be decoded: with 2, A (0) and N (13) would both become the letter of the shift. That leaves 312 keys, 12 multipliers times 26 shifts.
- Every A becomes the letter of the shift, since 0 times any multiplier is 0.
- Multiplier 1 gives a Caesar cipher: multiplier 1 and shift 3 turn
Picnic at noonintoSlfqlf dw qrrq, as the Caesar Cipher Encoder does with shift 3. Multiplier 25 with shift 25 gives Atbash.
What changes and what stays
- Only A to Z go through the formula, and capitals stay capitals. Spaces, digits, punctuation and line breaks are left alone.
- Accents are dropped before encoding:
café crèmebecomessihc spcqc, which decodes tocafe creme. Greek, Cyrillic and letters like ß pass through unchanged. - Double letters stay double: with the starting key,
noonbecomesvaav.