Reference

The Affine Cipher Explained

The affine cipher encrypts each letter with the formula y = (a times x plus b) mod 26, combining a multiplication and a shift into one substitution. It is a direct generalisation of the Caesar cipher, which is simply an affine cipher where a is fixed at 1.

Last updated 14 July 2026

Want to try it? Open the Affine Cipher translator and encode or decode your own text in your browser.

The formula, step by step

Every letter has a position from 0 (A) to 25 (Z). To encrypt, multiply that position by a, add b, then take the remainder after dividing by 26. The result is the position of the new, encrypted letter.

The value a cannot be just any number: it must share no common factor with 26, or two different letters would encrypt to the same result and the cipher could not be reversed. That leaves exactly 12 valid choices for a, combined with any of the 26 values for b.

Decrypting with the modular inverse

Decryption reverses the formula: subtract b, then multiply by the modular inverse of a (a number that undoes the multiplication under mod 26 arithmetic), then reduce modulo 26 again.

Finding that inverse is the one extra step compared with Caesar, but it only needs to be worked out once per key. After that, decoding an affine cipher is exactly as fast as encoding one.

Frequently asked questions

Why must a be coprime with 26?

If a shares a factor with 26, the multiplication step is not reversible: several different letters would map to the same encrypted letter, and decryption could not tell them apart. Only 12 values of a avoid this.

How many possible keys does the affine cipher have?

12 valid values for a multiplied by 26 possible values for b gives 312 keys in total, noticeably more than the 25 usable shifts in a Caesar cipher, though still far too few to resist a computer search.

Keep exploring