What the affine cipher is
The affine cipher is a simple letter-substitution cipher from school maths. It turns each letter into a number, A is 0, B is 1, all the way to Z is 25, then it applies the formula (a times letter plus b) mod 26. The result is a new letter. The two numbers, A and B, are the key: A multiplies the letter, B shifts it. With A is 5 and B is 8, the letter H, which is 7, becomes (5 times 7 plus 8) mod 26, which is 43 mod 26, which is 17, the letter R.
It is a step up from the Caesar cipher, which only shifts letters. But it is still a simple cipher: there are only 12 working values for A and 26 values for B, so only 312 keys in total. A computer can try them all in a blink, and letter frequency gives the answer away even faster.
How to use the encoder
Type your message in the text box, then pick A and B. A must share no factor with 26, which means it must be 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 or 25. If you pick a number like 2 or 13, the tool will tell you it does not work, because two different letters would map to the same cipher letter and the message could never be decoded. B can be any number from 0 to 25.
Press Encrypt to scramble the message, or Decrypt to turn a scrambled message back into plain text. The tool keeps upper and lower case, and it leaves spaces and punctuation alone, so the shape of the message stays visible. Try the same text with different A and B values and watch the result change completely.
Worked example
Take the word HELLO with A is 5 and B is 8. H is 7, so (5 times 7 plus 8) mod 26 is 43 mod 26, which is 17, the letter R. E is 4, so (5 times 4 plus 8) mod 26 is 28 mod 26, which is 2, the letter C. L is 11, so (5 times 11 plus 8) mod 26 is 63 mod 26, which is 11, the letter L. The second L is also L. O is 14, so (5 times 14 plus 8) mod 26 is 78 mod 26, which is 0, the letter A. HELLO becomes RCLLA.
To decode RCLLA, the tool uses the modular inverse of A. The inverse of 5 mod 26 is 21, because 5 times 21 is 105, which is 1 mod 26. Each cipher letter is turned back with (letter minus B) times 21 mod 26, and RCLLA becomes HELLO again. The word AFFINE with the same key becomes IHHWVC, and it decodes back to AFFINE the same way.
Who uses this tool
Maths students use it most. The affine cipher is a common homework topic, because it ties together modular arithmetic, multiplication, and the idea of an inverse in one small example. Teachers use it to show why some keys work and some do not, and why a cipher with only 312 keys is not safe.
Puzzle fans use it for fun messages and treasure hunts, where a scrambled note is a game, not a secret. Writers use it for coded notes in stories and games. And anyone learning about ciphers starts here before moving on to harder ones, because the affine cipher shows the basic idea of a key in a form you can work out by hand.
Honest limitations
The affine cipher is not secure. A message scrambled with it can be broken in minutes by hand and in a fraction of a second by a computer. Letter frequency gives it away: the most common letter in the scrambled text is usually E, and that alone narrows the key down fast. Never use it for passwords, private messages, or anything you need to keep secret.
It also only works on the 26 English letters. Numbers, accented letters, and other alphabets pass through unchanged, so a message with many numbers leaks information. And if you pick an A that shares a factor with 26, like 2 or 13, the cipher cannot be decoded at all, because two plain letters map to the same cipher letter.
Frequently asked questions
Why must A share no factor with 26? Because the cipher has to be reversible. If A shares a factor with 26, two different letters map to the same cipher letter, and decoding becomes impossible. The working A values are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25.
What is the modular inverse? It is the number that undoes the multiplication. For A is 5, the inverse is 21, because 5 times 21 is 105, which leaves 1 when divided by 26. Decoding multiplies by the inverse instead of by A.
Is the affine cipher the same as Caesar? No. Caesar only shifts letters, which is the affine cipher with A is 1. The multiply step in the affine cipher mixes the alphabet more, but it is still easy to break.
How many keys are there? Only 312: 12 working values for A times 26 values for B. That is why the cipher falls fast to a brute-force try of every key.
También disponible en español: Codificador De Cifrado Afín – Cifra Con Números A Y B
