Cipher
Affine Cipher Encoder and Decoder
The affine cipher combines multiplication and a shift into a single substitution: each letter's position is multiplied by a value called a, then shifted by a value called b. Set a and b, type your message, and read the result live.
Result appears here
It generalises the Caesar cipher, which is really just an affine cipher with a fixed at 1. Everything runs locally in your browser.
How the Affine Cipher works
Each letter's position (A is 0 through Z is 25) is transformed by the formula y = (a times x plus b) mod 26, where x is the plaintext position and y is the ciphertext position. The value a must share no common factor with 26, otherwise two different letters could encrypt to the same result and the cipher could not be reversed.
There are exactly 12 valid values for a (1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25) and 26 possible values for b, giving 312 usable keys in total. To decrypt, the tool uses the modular inverse of a to undo the multiplication before reversing the shift.
Examples
History and origins
The affine cipher is a straightforward extension of the Caesar shift, formalised as cryptography began to describe encryption with modular arithmetic. It is a standard early exercise in cryptography courses because it introduces the idea of a mathematical key, not just a shift amount.
Because it is still a simple substitution, every plaintext letter always maps to the same ciphertext letter, so it falls to the same frequency analysis that breaks the Caesar cipher, just with a larger space of valid keys to search.
Frequently asked questions
Why can't a be any number from 1 to 25?
a must share no common factor with 26 (the alphabet length), or the encryption would not be reversible; two different letters would scramble to the same result. Only 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25 work.
How is the affine cipher different from Caesar?
Caesar only shifts letters by a fixed amount, which is the same as an affine cipher with a fixed at 1. Affine adds a multiplication step, which spreads letters around the alphabet less predictably and gives a larger key space to search.
How do you decrypt an affine cipher?
You need the same a and b used to encrypt. The tool finds the modular inverse of a, subtracts b from each ciphertext position, multiplies by that inverse, and reduces the result modulo 26.
Learn more
Go deeper on the ideas behind this tool.