How to use the affine cipher tool
- Choose the Direction: Encrypt or Decrypt.
- Pick Multiplier a (coprime to 26) from the list. The default is 5.
- Enter Shift b, from 0 to 25. The default is 8.
- 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?
How many different keys are there?
How is the affine cipher related to Caesar and Atbash?
What happens to numbers, spaces and accented letters?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.