How to use the affine and atbash cipher tool
- Choose Affine or Atbash.
- For affine, choose Encrypt or Decrypt, then set the multiplier a and the shift b. The multiplier must have no common factor with 26.
- Type or paste the message. Letters change and keep their case; other characters stay as they are.
- Read the result and the table showing what each plain letter becomes.
Formula
Number the letters A = 0 to Z = 25
Affine encryption: y = (a x + b) mod 26, with a one of 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25
Affine decryption: x = a^(-1) (y - b) mod 26, where a x a^(-1) = 1 mod 26
Atbash: x becomes 25 - x, so A and Z, B and Y swap; it is affine with a = 25 and b = 25
With 12 allowed values of a and 26 of b there are 312 affine keys. A shift cipher such as Caesar is the case a = 1.
A published affine example and Atbash
With a = 5 and b = 8, the message AFFINE CIPHER becomes IHHWVC SWFRCP. A is 0, so 5 x 0 + 8 = 8, which is I; F is 5, so 25 + 8 = 33, which is 7 mod 26, which is H. Decrypting uses the inverse of 5 modulo 26, which is 21, and returns AFFINE CIPHER.
Atbash turns Wizard Of Oz! into Draziw Lu La!, keeping capital and small letters and the exclamation mark. Applying Atbash again returns the original, because swapping the two ends of the alphabet twice does nothing.
Why a must be coprime to 26
Multiplying by an a that shares a factor with 26 sends two different letters to the same letter. For example a = 2 turns both A (0) and N (13) into A (0), so the message could not be read back. The twelve values that work are the numbers with no factor in common with 26, that is, odd numbers other than 13. The page rejects any other value and explains why.
The inverse of a, used to decrypt, is found with modular arithmetic. For a general calculation of inverses and powers, see the modular arithmetic calculator.
Breaking the ciphers
Both ciphers replace each letter by a fixed other letter, so letter frequencies survive. In English text, the most common letters give away the key quickly. With only 312 keys for affine and a single key for Atbash, a computer tries them all in an instant. Use these ciphers for puzzles, for geocaches and for learning about modular arithmetic.
To count the letters of a ciphertext, use the letter frequency counter. For the plain shift, use the Caesar cipher, and for ciphers that work on pairs of letters, try the Playfair cipher tool or the rail fence cipher tool.
Assumptions and limits
- Only the 26 letters of the English alphabet are changed; accented letters, digits and symbols are left alone.
- The multiplier is checked against the 12 allowed values; the shift is 0 to 25.
- Atbash has no key, and Decrypt is the same as Encrypt.
- Messages can be up to 5,000 characters.
Frequently asked questions
What is the affine cipher?
A substitution cipher that turns each letter number x into (a x + b) mod 26. It combines a multiplication with a shift.
Which values of a can I use?
1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. These have no common factor with 26, so the cipher can be reversed.
What is Atbash?
A cipher that swaps the alphabet end to end, so A becomes Z, B becomes Y and so on. It is its own inverse.
How is the message decrypted?
Using the inverse of a modulo 26. For a = 5 the inverse is 21, because 5 x 21 = 105 = 1 mod 26.