Affine Cipher Tool
Encrypt and decrypt with the Affine cipher, E(x) = (ax + b) mod 26, then see how easily it is broken with a frequency chart, a ranked brute-force attack and a two-letter key solver.
100% client-side · A classical cipher for learning, not for real secrets
Letter frequencies
This textTypical English
English text scores about 0.067; random letters score about 0.038. A substitution cipher keeps the score of the original text.
Try all 312 keys
Decrypts the message with every valid key and ranks the results by how English they look. Choose a result to use that key.
Find the keys from two known letters
If you know that two plaintext letters became two ciphertext letters, the keys can be worked out. Enter one letter in each box.
The Affine cipher has only 312 keys and can be broken in an instant. Use it to learn, never to protect real information.
How the Affine Cipher Works
Number the letters A = 0 to Z = 25. To encrypt, multiply each letter's number by a, add b and take the remainder after dividing by 26. To decrypt, subtract b and multiply by the inverse of a modulo 26. For example, with a = 5 and b = 8, the word AFFINE becomes IHHWVC: A (0) becomes 5×0+8 = 8 = I, and F (5) becomes 5×5+8 = 33, which is 7 after taking away 26, so H.
The Affine cipher is a special case of simple substitution, so it is weaker than a general substitution cipher: it has 312 keys instead of 26! (about 4×1026). It also keeps the letter-frequency pattern of the original text, which is how it is broken.
Live Results
Results update as you type, with the cipher alphabet and inverse of A shown
Keeps Your Text Intact
Case, spaces and punctuation are preserved, or choose letters-only output
Frequency Chart
Compare your text with typical English and see the index of coincidence
Ranked Brute Force
All 312 keys are tried and sorted, with the likeliest plaintext first
Two-Letter Solver
See how two known letters are enough to recover the keys
Private by Design
Runs in your browser. Nothing is uploaded, saved or logged
Explore more free tools in our security tools collection, including our strong random password generator.
Frequently Asked Questions
1. What is the Affine cipher?
The Affine cipher is a classical substitution cipher. Each letter is turned into a number (A = 0 to Z = 25), put through the formula E(x) = (a×x + b) mod 26, and turned back into a letter. The two keys are a, a multiplier, and b, a shift. With a = 1 it becomes the Caesar cipher, and a = 25, b = 25 gives the Atbash cipher.
2. Why must Key A be coprime with 26?
The encryption must map every letter to a different letter, otherwise two plaintext letters would give the same ciphertext letter and decryption would be ambiguous. Since 26 = 2 × 13, any a that is even or a multiple of 13 repeats letters. That leaves 12 valid values: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25.
3. How is a message decrypted?
Decryption is D(y) = a-1 × (y - b) mod 26, where a-1 is the number that satisfies a × a-1 = 1 (mod 26). For example, with a = 5 the inverse is 21, because 5 × 21 = 105 = 4 × 26 + 1. The tool shows the inverse for your current key.
4. How secure is the Affine cipher?
Not at all by modern standards. There are only 12 × 26 = 312 keys (one of them, a = 1 and b = 0, leaves the text unchanged), so a computer tries them all instantly. It also keeps letter frequencies intact, and knowing just two plaintext and ciphertext letters reveals the key. It is a learning tool, not a way to protect real secrets; use AES or another modern cipher for that.
5. What is frequency analysis, and how does the ranking work?
In English some letters are far more common than others (E, T, A and O lead). A substitution cipher only relabels letters, so the most common ciphertext letters point to the most common plaintext letters. The Brute force tab decrypts the text with all 312 keys and scores each result by how closely its letter counts match typical English (a chi-squared score, where lower is better). With fewer than about 25 letters the ranking is unreliable, so check the top few results yourself.
6. How are spaces, punctuation, case and non-English letters handled?
By default only the letters A-Z are changed: upper and lower case are kept, and spaces, digits and punctuation pass through unchanged. Choose "Letters only" to drop everything except letters and get uppercase output. Letters outside A-Z, such as é, ñ or Greek and Cyrillic letters, are not part of the 26-letter alphabet, so they are left as they are.
7. Can the keys be found from just a few known letters?
Yes, and the Solve from two pairs tab shows how. If you know that plaintext letters P1 and P2 became ciphertext letters C1 and C2, the two equations a×P1 + b = C1 and a×P2 + b = C2 (mod 26) usually leave exactly one valid key. Only when the two plaintext letters are the same, or exactly 13 apart (such as A and N), do several keys fit, and the tool then lists them.
8. Is my text sent or stored anywhere?
No. Everything runs in your browser, the page's content security policy blocks outgoing network requests, and nothing you type is saved: there is no history and no local storage. The only thing the site remembers is your language choice for the menu.