1001Ferramentas
Security

Affine Cipher with Explicit Keys (a, b)

Encrypts text with E(x) = (a·x + b) mod 26, refuses any key a that shares a factor with 26, and prints the modular inverse needed to decrypt.

Ciphertext

The affine cipher: multiply and add inside the alphabet

The affine cipher treats each letter as a number from 0 to 25 and applies a first-degree function: multiply by a, add b, take the remainder modulo 26. It generalises the Caesar cipher, which is the case where a equals 1. The interesting part lies in the restriction: not every value of a will do.

Enter the text and the two parameters and the page enciphers, also showing the deciphering formula — which needs the inverse of a modulo 26, the number that undoes the multiplication. With a equal to 5, that inverse is 21, because 5 times 21 is 105, which leaves remainder 1 when divided by 26. That is the number appearing in the inverse formula.

When a is not coprime with 26, the cipher stops being reversible: two different letters land on the same one and information is lost. With a equal to 2, for instance, half the alphabet vanishes from the result. The page refuses those values instead of enciphering silently, and lists the twelve that work: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25.

Frequently asked questions

Why exactly those values of a?
Because they are the ones sharing no divisor with 26. Since 26 is 2 times 13, any even a or multiple of 13 fails. That leaves the twelve odd numbers other than 13, and each of them has an inverse modulo 26 — which is what makes deciphering possible.
How many keys are there in total?
Twelve values of a times twenty-six of b give three hundred and twelve combinations, discounting the trivial one where nothing changes. That is tiny: a computer tries them all in a fraction of a second, and even by hand you can sweep through looking for the text that makes sense.
Does it protect anything?
No. It is teaching material for modular arithmetic, where it illustrates multiplicative inverses and the coprimality condition nicely. To protect real data, the instrument is modern cryptography with an adequate key size.

Related Tools