Skip to content

Affine Cipher

Encode and decode with E(x) = (ax + b) mod 26. Enter a and b, and the tool checks that a can be undone, finds its modular inverse and works through every letter; if you don't know the key, decode mode tries all 312 and ranks them by how English the results look.

Mode

Must share no factor with 26 — pick one of the 12 values below.

Any whole number; it is taken mod 26.

The 12 valid values of a
Keep

Working for this key

Encoding: E(x) = (5x + 8) mod 26

Decoding: D(y) = 21(y − 8) mod 26

21 is the inverse of 5 because 5 × 21 = 105, which is 1 more than a multiple of 26.

Every valid a and its inverse

a1357911151719212325
a⁻¹1921153197231151725

Letter by letter — the first 12 letters

Letterxa·x + bmod 26Result
A088I
F5337H
F5337H
I84822W
N137321V
E4282C
C21818S
I84822W
P15835F
H74317R
E4282C
R179315P

How to Use the Affine Cipher

  1. 1. Pick a mode and enter your text. Encode turns plain text into cipher text; Decode reverses it.
  2. 2. Set the key. Enter a, the multiplier, and b, the shift. a must be one of the 12 values listed under the fields — click one to use it. b can be any whole number; it is reduced mod 26.
  3. 3. Read the working. The panel below the result gives both formulas for your key, the inverse of a, and each letter's arithmetic.
  4. 4. No key? Try them all. In Decode mode, tick Try all 312 keys. The results are ranked by how closely their letter counts match English, and Use this key loads any one of them.

The Affine Cipher Formula

Number the letters from zero: A = 0, B = 1 … Z = 25. To encipher a letter x, multiply it by a, add b and keep the remainder after dividing by 26: E(x) = (a·x + b) mod 26. To decipher a letter y, undo those steps in reverse — subtract b, then multiply by a⁻¹, the modular inverse of a: D(y) = a⁻¹·(y − b) mod 26. The inverse is the number that, multiplied by a, leaves a remainder of 1. For a = 5 it is 21, because 5 × 21 = 105 = 4 × 26 + 1.

Why only 12 values of a work

Multiplying by a must never send two letters to the same place, or decoding would have two answers. That happens whenever a shares a factor with 26 = 2 × 13. With a = 2, for example, A (0) and N (13) both land on b, because 2 × 13 = 26, which is 0 mod 26. So a must be odd and must not be 13, leaving the twelve numbers below — exactly the ones that have an inverse mod 26. With 26 choices of b, that makes 12 × 26 = 312 keys, one of which (a = 1, b = 0) changes nothing.

The valid values of a and their inverses mod 26
a1357911151719212325
a⁻¹1921153197231151725

Worked Example: a = 5, b = 8

With a = 5 and b = 8, AFFINE CIPHER enciphers to IHHWVC SWFRCP — the tool's sample. The letters of AFFINE, worked through:

Letterx5x + 8mod 26Result
A088I
F5337H
I84822W
N137321V
E4282C

To decode, the tool multiplies by a⁻¹ = 21. The I (8) becomes 21 × (8 − 8) = 0, which is A; the H (7) becomes 21 × (7 − 8) = −21, which is 5 mod 26, which is F. Negative values are normal at this step — mod 26 just means adding or subtracting 26 until the number lands between 0 and 25.

Two familiar ciphers are affine ciphers in disguise. With a = 1 the formula is x + b, a Caesar shift of b; with a = 25 and b = 25 it is the Atbash cipher, the alphabet read backwards.

How to Break an Affine Cipher

With only 311 keys that change anything, the bluntest attack is to try them all, which the brute-force option does in a fraction of a second. The hard part is recognising the answer, so the tool ranks every result with a chi-squared comparison of its letter counts against typical English. On a sentence or more the right key comes out on top; on a short phrase it may not — for IHHWVC SWFRCP alone, the true key (5, 8) ranks fourth — so read down the list rather than trusting the first line.

Cryptanalysts can do better with algebra. The affine cipher is a monoalphabetic substitution — each letter always becomes the same letter — so frequency analysis suggests which cipher letters stand for E and T, and two such guesses give two equations. Under the key above, E (4) becomes C (2) and T (19) becomes Z (25), so 4a + b ≡ 2 and 19a + b ≡ 25 (mod 26). Subtracting the first from the second gives 15a ≡ 23; multiplying by 7, the inverse of 15, gives a ≡ 161 ≡ 5, and then b ≡ 2 − 20 ≡ 8. Two letters of known plain text recover the whole key.

Frequently Asked Questions

What is an affine cipher?
A substitution cipher that turns each letter's number x into (a·x + b) mod 26, so every letter is multiplied by a and then shifted by b. The key is the pair (a, b). Because a given letter always becomes the same cipher letter, it is monoalphabetic — a stronger-looking relative of the Caesar cipher, and no harder to break.
Why must a be coprime to 26?
Decoding multiplies by the inverse of a modulo 26, and that inverse only exists when a and 26 share no common factor. If they do share one, two different letters encipher to the same letter and the message cannot be read back with certainty. That rules out every even number and 13, leaving 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25.
How do I find the modular inverse of a?
Find the number between 1 and 25 that, multiplied by a, leaves a remainder of 1 when divided by 26. For a = 7 that is 15, because 7 × 15 = 105 = 4 × 26 + 1. With only twelve cases it is quickest to read them from the table above; for large moduli, the extended Euclidean algorithm finds the inverse directly.
How many keys does the affine cipher have?
312 — twelve choices of a times 26 choices of b. One of them, a = 1 with b = 0, leaves the text unchanged, so 311 actually encipher. That is few enough to try every key by hand in an afternoon, and by computer in an instant.
Is the Caesar cipher a kind of affine cipher?
Yes. Setting a = 1 turns the formula into x + b, which is a Caesar shift of b; ROT13 is a = 1, b = 13. Atbash is affine too, with a = 25 and b = 25. Both sit among the 312 keys the brute force tries.
Is the affine cipher secure?
No. It falls to brute force, to frequency analysis, and to as little as two letters of known plain text, as the algebra above shows. It is excellent practice in modular arithmetic, but anything that matters needs modern encryption.

Related Tools

More in Codes & Ciphers

See all Codes & Ciphers