Affine Cipher
This affine cipher turns AFFINE CIPHER into IHHWVC SWFRCP with a=5 and b=8, shows the a inverse it decrypts with, and ranks all 312 keys by English fit.
Input
0 of 5,000 characters (code points).
Inverse of 5: 21, because 5 x 21 = 105 = 4 x 26 + 1. Decryption: D(y) = 21 (y - 8) mod 26.
Not offered, each shares a factor with 26: 0 (26), 2 (2), 4 (2), 6 (2), 8 (2), 10 (2), 12 (2), 13 (13), 14 (2), 16 (2), 18 (2), 20 (2), 22 (2), 24 (2).
Encrypted text
Type some text first.
Brute force all 312 keys
Type or paste a ciphertext above to rank the keys.
Why use this Affine Cipher?
The Inverse Is Shown
Decryption needs the inverse of a mod 26. The page prints the value and the sum that produces it, so you can follow the arithmetic instead of accepting a number.
Only Valid Multipliers
The multiplier must be coprime with 26. The twelve that work are the only choices on the menu, and the page says why the other fourteen are left out.
All 312 Keys Tried
Brute force tests every a and b pair and ranks the results by chi-squared against English letter frequencies, so the readable line usually sits at the top.
What is the Affine Cipher?
The affine cipher is a monoalphabetic substitution: one rule decides which letter replaces which, and the rule never changes inside a message. Number the letters A to Z from 0 to 25. Each plaintext letter x becomes (a x + b) mod 26, where a and b are the key. With a equal to 1 the rule collapses to a Caesar shift of b, and with a equal to 25 and b equal to 25 it is Atbash, which maps A to Z. So the affine cipher is a family, and those two well-known ciphers are members of it.
Because the mapping must be reversible, a has to be coprime with 26, which leaves 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25. Any other multiplier makes two letters land on the same ciphertext letter and the message cannot be recovered. Decryption is (a inverse times (y minus b)) mod 26. Twelve multipliers times 26 shifts gives 312 possible keys, which is small enough to try all of them, and that is a quick way in when the key is unknown. The caesar cipher is the same formula with a fixed at 1, which is why the 312 keys include every Caesar shift.
Which Variant This Page Uses
- AlphabetA to Z, 26 letters, A is 0 and Z is 25. No I and J merge, so all 26 letters are distinct.
- EncryptionE(x) = (a x + b) mod 26. A with a=5 and b=8 gives I; the whole phrase AFFINE CIPHER becomes IHHWVC SWFRCP.
- Multipliersa must be coprime with 26. The twelve values are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25, with inverses 1, 9, 21, 15, 3, 19, 7, 23, 11, 5, 17, 25.
- DecryptionD(y) = a inverse (y - b) mod 26. With a=7 the inverse is 15, so the page shows D(y) = 15 (y - 3) mod 26 for b=3.
- Brute forceAll 12 x 26 = 312 keys are tried as decryptions and ranked by chi-squared against English letter frequencies, lowest first. The panel shows the best 20 rows, each with a and b, its score and a preview cut at 60 characters, and the best row is labelled Most English-looking.
- Pass-throughCase is kept: a letter takes the case of the letter it replaces. Digits, spaces and punctuation are copied unchanged and are not counted as letters.
- LimitsUp to 5000 code points. Over the limit the page refuses the input and names code points before doing any work.
- Saved inNothing is saved. Reloading clears the text and the keys.
- Not includedOther substitution ciphers, a multiplier outside the twelve, a modulus other than 26 and a history of results.
Common Use Cases
Cryptography Homework
Get the inverse and the arithmetic that produces it, which is what a written answer has to show.
Checking a Hand Calculation
Encrypt by hand, then paste the result and see whether the page agrees before you hand it in.
Teaching Modular Arithmetic
The multiplier list is short enough to work through on a board, and the page names the factor that rules the other fourteen out.
Puzzle Design
Pick a and b, encrypt a clue, and keep the pair as the answer key.
Seeing Why a Must Be Coprime
Read why 0, the even values and 13 are left out, then pick a=3 and watch the inverse 9 appear.
Comparing With Caesar and Atbash
Set a=1 to get any Caesar shift, or a=25 with b=25 to get Atbash, on the same page.
How to Encrypt and Decrypt
Pick a and b
Choose a from the twelve coprime values and b from 0 to 25. The page shows the inverse of a as soon as you pick it.
Type the text
Case is kept and anything that is not a letter passes through, so a sentence keeps its spaces and punctuation.
Press Encrypt or Decrypt
AFFINE CIPHER with a=5 and b=8 becomes IHHWVC SWFRCP. Decrypting that with the same a and b returns the phrase.
Break an unknown key
Paste the ciphertext into the input and open the brute force panel. The 312 results are ranked by chi-squared, and the best 20 are shown.
What the Output Gives You
The Working, Not Just the Answer
The inverse is printed as a number and as a formula, which is what a homework answer usually has to show.
Left-Out Keys Explained
The page lists the fourteen values it does not offer and the factor each shares with 26, instead of producing garbage.
A Ranked Brute Force
312 keys scored, best English fit first, so a wrong guess is visibly wrong.
Case and Punctuation Kept
The output reads like the input, which makes diffing it easy.
Copy the Result
Copy the output in one click, or press Use on a brute-force row to load its key and read that decryption in the output.
Tips for Affine Cipher Work
- •Count letters before scoring Chi-squared is noisy under about 20 letters, so a short message can rank the wrong key first.
- •a=1 is a shift With a=1 it is a plain Caesar, and a=25 with b=25 is Atbash.
- •Even a values always fail 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24 share a factor with 26.
- •13 is odd but still out It shares the factor 13 with 26, so every letter would land on one of two ciphertext letters.
- •Write the inverse down If you have to show work, the page's inverse line is the line a marker looks for.
- •Case is kept A capital encrypts to a capital, so the case pattern of the plaintext is still visible in the ciphertext.
Encrypt Something
Pick a multiplier and a shift above, or paste a ciphertext and read the ranked keys.
For the shift-only version see the caesar cipher, and for the fixed a=25, b=25 case see the atbash cipher.
Related tools
Frequently Asked Questions
Why are there only twelve multipliers?
Because a must be coprime with 26, and 26 is 2 times 13. Any even a shares 2 and a=13 shares 13, so 14 of the 26 values are out. What remains are the twelve listed on the page.
How do I find the a inverse without a computer?
Look for the number that multiplies a to give 1 more than a multiple of 26. For a=7, 15 x 7 is 105, and 104 is 4 x 26. Extended Euclid does the same search in a few steps and always finds the inverse when one exists.
Does the affine cipher keep spaces?
It has no cell for them, so this page copies them through and leaves them out of the arithmetic. Only A to Z count as letters in the chi-squared score, which is why punctuation does not skew it.
Is 312 keys the same as 26 keys?
No. A Caesar cipher has 26 shifts, one of them the do-nothing shift of 0. The affine cipher adds the multiplier, so 12 valid multipliers times 26 shifts gives 312 distinct mappings.
Can I break a long affine ciphertext by hand?
Yes, with frequency analysis. Find the most common ciphertext letter, guess it is E, then use a second frequent letter to solve for a. Brute force is faster here because the page already ranks the 312 candidates.