Join the Official Discord

Affine cipher

Pick a and b, or switch to “Find the key” and let us try all 311 keys.

Mode
Result

Most likely keys

    How the affine cipher works

    Number the alphabet from A=0 to Z=25. Pick two numbers, a and b. Every letter x becomes (a × x + b) mod 26: multiply, add, and wrap round the alphabet. With a=5 and b=8, A (0) becomes 8, which is I, and F (5) becomes 33, which wraps to 7, H. AFFINE becomes IHHWVC.

    It's a family that contains two ciphers you already know. With a=1 it's the Caesar shift by b. With a=25 and b=25 it's Atbash.

    Decoding, and why only some values of a work

    To undo the multiplication you multiply by a's inverse, the number that turns a back into 1 mod 26. For a=5 that's 21, because 5 × 21 = 105 = 4 × 26 + 1. An inverse only exists when a shares no factor with 26, which rules out every even number and 13. That leaves twelve multipliers: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25.

    Breaking it without the key

    Twelve choices of a and twenty-six of b make just 312 keys, so “Find the key” tries them all and ranks the results by how English they read. By hand, two letters are enough. Frequency analysis suggests which cipher letters are E and T, which gives two equations in a and b, and solving them gives you the key.

    Questions

    How does the affine cipher work?

    Number the letters A=0 to Z=25. Each letter x becomes (a·x + b) mod 26. With a=5 and b=8, A (0) becomes 8, which is I. Caesar is the special case a=1, and Atbash is a=25, b=25.

    Why can't a be any number?

    Decoding needs a multiplier that undoes a, which only exists when a shares no factor with 26. That leaves twelve choices: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25.

    How do I decode affine without the key?

    There are only 12 × 26 = 312 keys, so the tool tries every one and ranks the results by how English they read. On anything longer than a sentence the right key comes out on top.

    More cipher tools

    See all cipher tools →