Affine cipher — encrypt and decrypt with a multiplier and a shift

The affine cipher multiplies each letter's position by a and adds b, wrapping round the alphabet; choose a from the 12 valid multipliers and b from 0 to 25 to encrypt, or switch to Decrypt.

Live demo · real result from this tool
Direction

Result

Ihhwvc swfrcp

Processed on your device — nothing is uploaded.

How to use the affine cipher tool

  1. Choose the Direction: Encrypt or Decrypt.
  2. Pick Multiplier a (coprime to 26) from the list. The default is 5.
  3. Enter Shift b, from 0 to 25. The default is 8.
  4. Type into Your text and copy the Result. To decrypt, keep the same a and b and switch the direction.

What it does and when to use it

The affine cipher is a substitution cipher: each letter is always replaced by the same other letter, but the replacement table comes from a small formula instead of a shift alone. It is a common exercise in maths and computing classes because it combines modular arithmetic with a real, if weak, cipher. It also shows up in puzzle hunts and capture-the-flag warm-ups.

It is a classical cipher for learning and puzzles, not for protecting anything. With only 312 keys and letter frequencies left intact, it falls to a quick search or frequency analysis. To see the two special cases on their own, try the Caesar cipher and the Atbash cipher. For a cipher where the shift changes letter by letter, compare the Gronsfeld cipher and the autokey Vigenère cipher.

How it works

Number the letters A = 0 to Z = 25. For a letter x:

Encrypt:  E(x) = (a × x + b) mod 26
Decrypt:  D(y) = a⁻¹ × (y − b) mod 26

Here a⁻¹ is the number that gives 1 when multiplied by a, modulo 26. The tool finds it from the list of valid values; for a = 5 it is 21, because 5 × 21 = 105 = 4 × 26 + 1. Each letter keeps its case, and everything that is not A to Z or a to z is copied across. If a value of a shares a factor with 26, the tool explains why and lists the valid values. The work is done in your browser.

Inverses for each 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.

Worked examples

Results from the tool:

AFFINE CIPHER       a=5,  b=8            →  IHHWVC SWFRCP
Meet at 9, café!    a=7,  b=3            →  Jffg dg 9, rdmé!
Jffg dg 9, rdmé!    a=7,  b=3, Decrypt   →  Meet at 9, café!
Hello               a=25, b=25           →  Svool

Check the first letter by hand: A is 0, and 5 × 0 + 8 = 8, which is I. F is 5, and 5 × 5 + 8 = 33, and 33 mod 26 = 7, which is H. In the second line the 9, the comma and the é pass through. The last line is the Atbash case.

Limits and tips

  • Double letters stay double (the FF in AFFINE becomes HH), which helps anyone breaking it.
  • If you know two plaintext letters and what they became, you can solve for a and b with two equations.
  • Share a and b together. A message encrypted with a = 5 cannot be decrypted with b alone.

Frequently asked questions

Why can a only be 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 or 25?
To decrypt, a must have an inverse modulo 26, which only exists when a shares no factor with 26. Even numbers share the factor 2 and 13 shares 13. With a = 2, for example, A and N would both become the same letter, so the message could not be recovered.
How many different keys are there?
12 choices of a times 26 choices of b, which is 312 keys. One of them (a = 1, b = 0) leaves the text unchanged. That is few enough to try every key by computer in an instant.
How is the affine cipher related to Caesar and Atbash?
With a = 1 it is a Caesar shift by b, so a = 1, b = 3 turns Hello into Khoor. With a = 25 and b = 25 it is Atbash, the mirror alphabet, so Hello becomes Svool.
What happens to numbers, spaces and accented letters?
They pass through unchanged. Only A to Z and a to z are encrypted, and each keeps its case. In café the c, a and f change but the é stays as it is.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close