The Black Chamber RU

Tools · Classical ciphers

Affine cipher

The affine cipher E(x) = a·x + b: encode, decode and brute-force every key.

Runs in your browser. Your text goes nowhere.

How to use it

Pick a multiplier a and a shift b, and the text is encrypted at once. Number the letters from zero: A = 0, B = 1, … Z = 25. Each letter x becomes (a·x + b) mod 26, that is, the remainder after dividing by the number of letters. The strip above the text shows where every letter goes. The “⇅” button swaps the fields and flips the mode.

The alphabet is detected from the text: Latin (26 letters) or Russian (33, with ё). Case is kept and other characters are left alone.

Why not every a works

For a cipher to be decryptable, different letters must turn into different letters. That only happens when a and the number of letters share no common factor except 1. For the Latin alphabet (26 = 2 × 13) every even a and 13 are ruled out, leaving 12 choices. For Russian (33 = 3 × 11) multiples of 3 and 11 are out, leaving 20. The tool shows only the ones that work.

Decryption needs a⁻¹, the inverse of a: the number for which a × a⁻¹ leaves a remainder of 1. Then D(y) = a⁻¹ × (y − b). The formula under the buttons shows this number and the check.

Brute force

The affine cipher has few keys: 12 × 26 = 312 for Latin and 20 × 33 = 660 for Russian. The “Brute force” panel tries them all and ranks them by χ², a measure of how closely the letter frequencies match the language. For a phrase of a few words the right key is almost always on top. Click a row to use that key.

FAQ

How is the affine cipher related to Caesar and Atbash?

With a = 1 it is a Caesar cipher with shift b. With a = b = N − 1 (N being the number of letters) it is Atbash. Both are special cases of the affine cipher, which is itself a special case of simple substitution.

Why are a = 1 and a = N − 1 in the list?

They are coprime with N too. They are valid keys, just very weak ones.

Is it safe to paste my text here?

Yes. Everything runs in your browser and nothing is sent to a server.