Guide
The affine cipher: E(x) = (ax + b) mod 26
The affine cipher turns each letter into a number (A = 0, B = 1 … Z = 25), multiplies it by a, adds b and takes the remainder after dividing by 26. The result is turned back into a letter. With a = 5 and b = 8, the word AFFINE becomes IHHWVC. It is a favourite in mathematics and computer science courses because it puts modular arithmetic to work in a few lines.
Choosing a key
The shift b can be any number from 0 to 25. The multiplier a cannot: to decrypt, a must have an inverse modulo 26, which exists only when a shares no factor with 26. Since 26 = 2 × 13, every even number and 13 are ruled out, leaving 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. Only these are offered, so you cannot pick a key that loses information. With a = 2, for example, A and N would both become the same letter.
Decryption and the inverse
Decryption reverses the steps: subtract b, then multiply by the inverse of a, again modulo 26. The formula line under the settings shows both formulas with your numbers, including the inverse — 21 for a = 5, because 5 × 21 = 105 leaves a remainder of 1 when divided by 26. Students can use it to check calculations done by hand.
The substitution table
Because a and b are fixed, the affine cipher is a simple substitution: every A becomes the same letter, every B another, and so on. The table under the tool shows the complete alphabet mapping. It is useful for encrypting by hand and for seeing special cases: with a = 1 the cipher is a Caesar shift by b, and with a = 25 and b = 25 it is exactly the Atbash cipher.
Breaking it
There are only 12 × 26 = 312 key combinations, one of which leaves the text unchanged. The cracker decrypts the message with all 311 others and ranks the results by how English they look, using letter frequencies and common words. On a sentence or two, the right key comes first. Frequency analysis by hand also works: if the two most common cipher letters correspond to E and T, two equations give a and b.
In the classroom
The affine cipher is a compact exercise in modular arithmetic, inverses and the importance of coprime numbers, and a natural step between the Caesar cipher and more complex systems. Encrypt with the tool, decrypt by hand, then compare.
Private
All calculations run in your browser.
A worked example
With a = 5 and b = 8, encrypt the letter F. F is 5, so 5 × 5 + 8 = 33, and 33 mod 26 = 7, which is H. To decrypt H, subtract 8 to get −1, which is 25 modulo 26, then multiply by the inverse 21: 25 × 21 = 525, and 525 mod 26 = 5, which is F again. The substitution table shows the whole mapping at once, and the formula line confirms the inverse. Worked through by hand like this a few times, the role of the modular inverse becomes clear, which is why the cipher is so popular in exam questions.
Related tools
See the special cases in the Caesar Cipher Solver and Atbash Cipher, use a keyword with the Vigenère Cipher, or practise number systems with the Number Base Converter.
Checked against the current version of the Affine Cipher on .

