Toolstia / Affine cipher

Affine cipher

Encrypt text with the formula (a x letter + b) mod 26, or decrypt it with the inverse. Pick a from the list of valid multipliers and a shift b from 0 to 25.

The affine cipher turns each letter into a number from 0 to 25, multiplies it by a, adds b and wraps around at 26. Only some values of a work, namely those with no common factor with 26, so that each letter has a unique partner. With a = 1 it is the Caesar cipher, and with a = 25 and b = 25 it is Atbash.

How it works

  1. Choose encrypt or decrypt.
  2. Pick a and b.
  3. Type your text and copy the result.

The formula

Each letter becomes a number from 0 to 25, is multiplied by a, has b added and is reduced modulo 26.

Valid values of a

Only numbers with no common factor with 26 work: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. Otherwise two letters would map to the same one.

Decryption

Decrypting multiplies by the modular inverse of a. The tool shows it for you, for example the inverse of 5 is 21.

Special cases

With a = 1 it is a Caesar shift, and with a = 25 and b = 25 it is the Atbash cipher.

Questions

Is it secure?

No. There are only 312 keys, so it is easy to break by trying them all. Everything happens in your browser, and nothing you enter is sent anywhere.

What happens to capital letters?

They stay capital. Digits and punctuation are not changed.

Why are some a values missing?

They would not give each letter a unique partner, so decryption would be impossible.