TTKTheTextKit

Affine Cipher Encoder and Decoder

Type a message, choose the key a and the key b, and encrypt or decrypt it with the affine cipher. Ready-made keys give Atbash, ROT13 and a Caesar shift of 3, and the formula and the inverse of a are shown. When you do not know the key, Try all 312 keys lists the text decrypted with every possible key so you can spot the one that reads as words. Letters keep their case and everything else is left as it is. Everything runs in your browser.

E(x) = (5x + 8) mod 26. The inverse of 5 modulo 26 is 21, so D(x) = 21 (x - 8) mod 26.

12 letters changed, 1 other characters kept as they are.

The affine cipher turns each letter into a number from 0 to 25, A being 0, and encrypts it as (a x + b) mod 26. To decrypt, it multiplies (x - b) by the inverse of a modulo 26. The letters only map one to one when a has no factor in common with 26, which leaves 12 values: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. With 26 values of b that gives 312 keys, so the cipher is easy to break by trying them all, which is what the Try all 312 keys button does. A key of a = 1 is the Caesar cipher, and a = 25 with b = 25 is Atbash, which swaps A with Z, B with Y and so on. Letters keep their case, and numbers, spaces, accents and punctuation are left as they are. This is a puzzle and a teaching tool, not a way to protect anything. Everything runs in your browser.

Related Tools

[6 suggestions]

About the Affine Cipher Encoder and Decoder

The affine cipher turns each letter into a number, A as 0 up to Z as 25, and encrypts it as (a x + b) mod 26. Decryption multiplies (x - b) by the inverse of a modulo 26, where a x inverse = 1 mod 26. The letters only map one to one when a has no factor in common with 26, so there are 12 values for a, namely 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25, and 26 for b, which makes 312 keys. The inverse of 5 is 21 because 5 x 21 = 105 = 4 x 26 + 1. With a = 5 and b = 8, AFFINE CIPHER becomes IHHWVC SWFRCP. Special cases are familiar ciphers: a = 1 is the Caesar cipher, a shift by b, so a = 1 and b = 13 is ROT13, which undoes itself, and a = 25 with b = 25 is Atbash, which swaps A with Z, B with Y and so on and also undoes itself. Because there are only 312 keys, the affine cipher is easy to break by trying them all, which the tool does at the touch of a button, and it keeps the letter frequencies of the original text, so it is a puzzle and a way to learn modular arithmetic and not a way to protect anything. Upper and lower case letters keep their case, and digits, spaces, accents, punctuation and emoji are passed through unchanged.

How does the affine cipher work?▾

Each letter becomes a number from 0 to 25, is put through (a x + b) mod 26 and turns back into a letter. The key is the pair a and b. To decrypt, multiply (x - b) by the inverse of a modulo 26.

Which values of a are allowed?▾

Only values with no factor in common with 26: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. With any other value, two letters would map to the same one and the message could not be read back.

How is the affine cipher related to Caesar and Atbash?▾

With a = 1 it is a Caesar cipher that shifts by b, and with b = 13 it is ROT13. With a = 25 and b = 25 it is Atbash, which swaps A with Z, B with Y, and so on.

How do I decrypt an affine cipher without the key?▾

Choose Decrypt and press Try all 312 keys. It lists the text decrypted with each key, and the one that reads as real words is the key. With a longer text, the most common letter also gives clues.

Is the affine cipher secure?▾

No. With only 312 keys it can be broken by trying them all in moments, and it keeps the letter frequencies. Use it for puzzles and learning, and use modern encryption to protect real data.