1001Ferramentas
🧮 Security

Affine Cipher

Encode text with the Affine cipher using E(x) = (a·x + b) mod 26. Here uses a=5, b=8 (coprime with 26). Combines multiplicative and additive substitution.

Multiply and add, all modulo 26

A modular arithmetic problem set or a first class in classical cryptography: applying E(x) = (5x + 8) mod 26 letter by letter on paper goes wrong somewhere, almost always at the wraparound past Z. Type the word and the result appears immediately. AFFINE CIPHER comes out as IHHWVC SWFRCP, the same example printed in most textbooks. The key is fixed on this page at a=5, b=8, with no field to change it.

The part that trips people up: a must be coprime with 26, otherwise two different letters map to the same output and the cipher stops being reversible. 5 is coprime, so a multiplicative inverse exists, and it is 21, because 5 times 21 is 105, which is 1 modulo 26. Set a=1 and the formula collapses into a Caesar shift. With only 12 valid values for a and 26 for b, the entire key space is 312 combinations, brute forced in milliseconds.

Spaces and punctuation pass through untouched, which keeps word shapes visible. Accented letters also pass through unchanged: ATENÇÃO comes out as IZCVÇÃA, with the Ç and the à giving away the original for free, so type without accents. There is no decrypt mode here, and running the cipher twice does not restore the text: to reverse it use D(y) = 21(y − 8) mod 26. Fine for checking homework, useless as real protection. Everything runs in the browser.

Frequently asked questions

Why is the key fixed at a=5 and b=8?
It is the pair used in most textbooks, which makes checking exercises easier. On this page both values are hardcoded and cannot be changed from the interface.
How do I decrypt an affine cipher message?
Apply D(y) = 21(y − 8) mod 26, where 21 is the inverse of 5 modulo 26. Since the multiplication step is not undone by repetition, encrypting again will not bring the text back.
Can a be any number?
No. It must be coprime with 26, which leaves 12 options: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. Values such as 2 or 13 collapse different letters onto the same output.

Related Tools