Affine Cipher
Encode and decode the affine cipher using the formula E(x) = (a·x + b) mod 26. Step the multiplier a and shift b, watch the substitution alphabet and the live formula update, and copy, download, or share the result. Everything runs privately in your browser.
Multiplier a
Shift b
E(x) = (5x + 8) mod 26
The multiplier a must be coprime with 26, so only twelve values are allowed: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. The shift b can be any number from 0 to 25.
Enter text above to see the result here.
Substitution alphabet
Plain
Cipher
How to use Affine Cipher
- 1
Choose encode or decode
Pick Encode to turn plain text into affine ciphertext, or Decode to turn ciphertext back into readable text using the same two keys.
- 2
Set the multiplier a
Step the multiplier a through its twelve valid values. Only numbers coprime with 26 are offered, so the cipher can always be reversed.
- 3
Set the shift b
Step the shift b to any value from 0 to 25. The live formula and the substitution alphabet table update the moment you change it.
- 4
Type or paste your text
Enter your message and the cipher runs as you type. Letters are enciphered while spaces, digits, and punctuation pass through unchanged, and capitalisation is preserved.
- 5
Copy, download, or share
Copy the result, download it as a text file, or share a link that reopens the tool with your exact keys, text, and direction ready to go.
Understanding the Affine Cipher
What is the affine cipher?
The affine cipher is a monoalphabetic substitution cipher built from simple modular arithmetic. Each letter is turned into a number, with A = 0 up to Z = 25, passed through the linear function E(x) = (a·x + b) mod 26, and turned back into a letter. The two numbers a and b are the key: a is the multiplier and b is the shift. Because the same letter always maps to the same cipher letter, the affine cipher belongs to the same family as the Caesar and Atbash ciphers, but its multiply-then-shift step makes it more general than either.
The name comes from the affine function a·x + b, the combination of a multiplication and an addition that gives the cipher its structure. It is one of the most popular ciphers for teaching the mathematics behind cryptography, because it shows how modular arithmetic, coprime numbers, and modular inverses come together in a working code. It is also a regular guest in puzzles, escape rooms, and capture-the-flag challenges, where its small key space makes it satisfying to crack by hand.
The affine encryption formula
To encrypt, every letter is converted to its position in the alphabet, with A = 0, B = 1, and so on to Z = 25. That number x is fed into the formula E(x) = (a·x + b) mod 26: multiply by a, add b, and take the remainder after dividing by 26 so the result wraps back into the range 0 to 25. Converting that number back to a letter gives the ciphertext. Multiplying by a stretches and scrambles the alphabet, while adding b slides it along, and the mod 26 keeps everything inside the 26-letter ring.
The tool above shows the whole mapping at a glance. The substitution alphabet table lists every plain letter A–Z with the cipher letter it becomes under your current a and b, and it updates the instant you change either key. Letter case is preserved, and anything that is not a letter — spaces, digits, punctuation, emoji — passes straight through untouched, so the shape of your message stays readable even after it is enciphered.
Choosing the keys a and b
The shift b is easy: it can be any whole number from 0 to 25. The multiplier a is the interesting one, because not every value works. For the cipher to be reversible, a must be coprime with 26, meaning it shares no common factor with 26 other than 1. Since 26 = 2 × 13, any even number and any multiple of 13 is ruled out, which leaves exactly twelve usable values: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25.
If a shares a factor with 26 — say a = 2 — then several different plain letters collide on the same cipher letter, and the message can no longer be decoded uniquely. That is why the multiplier control above only steps through the twelve valid values: it is impossible to choose a key that cannot be undone. With twelve choices for a and twenty-six for b, there are 312 possible key combinations in total, one of which, a = 1 and b = 0, leaves the text unchanged.
A worked affine example
Take the keys a = 5 and b = 8 and encrypt the word AFFINECIPHER. The first letter A is 0, so E(0) = (5·0 + 8) mod 26 = 8, which is I. The next letter F is 5, so E(5) = (5·5 + 8) mod 26 = 33 mod 26 = 7, which is H. Continuing letter by letter through the whole word gives the ciphertext IHHWVCSWFRCP.
You can follow every step on the substitution alphabet table above: set a to 5 and b to 8, and the cipher row shows A becoming I, B becoming N, C becoming S, and so on across the alphabet. Because the mapping is fixed, enciphering the same letter always gives the same result — both F letters in AFFINE become H — which is exactly the property that lets an attacker use frequency analysis against it.
Decrypting an affine cipher
Decryption runs the formula backwards with D(y) = a⁻¹·(y − b) mod 26. The new ingredient is a⁻¹, the modular inverse of a: the number that, multiplied by a, gives 1 mod 26. For a = 5 the inverse is 21, because 5 × 21 = 105 = 4 × 26 + 1. To decode you subtract the shift b, multiply by that inverse, and take the result mod 26. Carrying the example back, the cipher letter I is 8, and D(8) = 21·(8 − 8) mod 26 = 0, which is A again.
You never have to work the inverse out yourself. Choose Decode above, enter the same a and b that were used to encrypt, and paste the ciphertext — the tool finds the modular inverse and recovers the message instantly. Because the affine cipher is a clean one-to-one mapping, decoding returns your original text exactly, with its spaces, capitalisation, and punctuation all intact.
Caesar and Atbash as special cases
The affine cipher quietly contains two of the most famous classical ciphers. Set the multiplier a to 1 and the formula collapses to E(x) = (x + b) mod 26, which is precisely the Caesar cipher — a pure shift of every letter by b places. With a = 1 and b = 3 you get the classic shift Julius Caesar is said to have used, turning A into D and HELLO into KHOOR.
Set a = 25 and b = 25 and something just as familiar appears. Since 25 is the same as −1 mod 26, the formula becomes E(x) = (25 − x) mod 26, which reverses the alphabet so A becomes Z, B becomes Y, and so on. That is the Atbash cipher, used by Hebrew scribes thousands of years ago. Seeing both ciphers fall out of one formula is the clearest way to understand how the affine cipher generalises them — try each setting above and watch the substitution table confirm it.
How secure is the affine cipher?
By any modern measure the affine cipher offers no real security. With only 312 possible keys — and fewer still that actually scramble the text — a computer can try every one in a fraction of a second, and even a patient person can brute-force it by hand. There is simply not enough secret information in two small numbers to resist attack.
It is also vulnerable to frequency analysis, because it is monoalphabetic: each letter always maps to the same cipher letter, so the most common ciphertext letter is very likely to stand for E, the most common letter in English. An attacker who can guess or learn just two plaintext-to-ciphertext letter pairs can even solve for a and b directly with a little algebra. None of this is a flaw to fix — it is what makes the affine cipher such a good teaching tool — but for protecting real information you should always use a modern, peer-reviewed algorithm such as AES.
Frequently asked questions
What is the affine cipher?
How does the affine cipher work?
What values can the multiplier a take?
What are the keys a and b in the affine cipher?
How do you decrypt an affine cipher?
Can you show a worked affine cipher example?
How is the affine cipher related to the Caesar cipher?
How is the affine cipher related to the Atbash cipher?
How many possible affine cipher keys are there?
How do you crack the affine cipher?
Does the affine cipher keep spaces, numbers, and punctuation?
Is my text uploaded to a server?
Related tools
Keep going with these handy tools