Cipher Table

Affine cipher

Multiply each letter's number by one key, add a second, and wrap round the alphabet. A Caesar cipher with one extra twist.

Key

How the affine cipher works

Number the letters A = 0 to Z = 25. Choose two keys, a and b. Each plain letter x becomes

cipher = (a × x + b) mod 26

“mod 26” means take the remainder after dividing by 26, which wraps the result back into the alphabet. With a = 5 and b = 8, the letter C (2) becomes 5 × 2 + 8 = 18, which is S.

When a = 1 the multiplication does nothing and you have a plain Caesar cipher with shift b. When a = 25 and b = 25 you have Atbash. The affine cipher is the family that contains both.

To decrypt, reverse the steps: subtract b, then multiply by the inverse of a.

How to break it

The affine cipher looks more scrambled than a Caesar shift, because neighbouring letters no longer stay neighbours. But it is still a simple substitution, and it falls the same two ways:

Try every key. There are only 312. The breaker on this page tests them all and ranks the results by how English they read.

Solve for the key. If you can guess just two letters (say, that the most common cipher letter is E and the second most common is T) you get two equations in two unknowns, and school algebra gives you a and b.

Why it matters

Nobody ever relied on the affine cipher for serious secrets. Its value is as a teaching step: it is the simplest cipher that needs modular arithmetic and the idea of an inverse, both of which sit at the heart of modern cryptography such as RSA.

Questions people ask

Why can't a be any number?

The multiplier must share no factors with 26, otherwise two different letters would be enciphered to the same letter and the message could not be decoded. That leaves 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25.

How many keys does the affine cipher have?

Twelve choices for a times twenty-six for b gives 312 keys, one of which (a = 1, b = 0) changes nothing. That is few enough to try them all in a blink.

How do I decrypt an affine cipher by hand?

Find the number that undoes a (its modular inverse, the value that multiplied by a gives 1 more than a multiple of 26), then for each letter subtract b and multiply by that inverse. For a = 5 the inverse is 21.