Affine Cipher
Apply mathematical transformations for encryption using the Affine cipher algorithm.
How to Use Affine Cipher
- Enter plaintext and leave the tool in Encode mode.
- The current component applies the fixed defaults a = 5 and b = 8 to every letter.
- Switch to Decode and swap the ciphertext back into the input to apply the modular inverse.
- Copy the case-preserving result; spaces and punctuation remain unchanged.
For each letter index x, encryption computes (5x + 8) mod 26. Since 5 is coprime to 26, its modular inverse makes decryption possible.
Examples
Encode with a = 5, b = 8 — input:
AFFINE CIPHER
Output:
IHHWVC SWFRCP
Case-preserving encode — input:
Hello
Output:
Rclla
How the Affine Cipher Works (the formula)
The affine cipher turns each letter into a number, applies a simple line of algebra, and turns the result back into a letter. Number the alphabet A = 0, B = 1, …, Z = 25. For a letter at position x, encryption is:
E(x) = (a · x + b) mod 26
It has two keys: a is the multiplicative key and b is the additive key. Because every letter follows the same equation, the affine cipher is a monoalphabetic substitution — each plaintext letter always maps to the same ciphertext letter. It also generalises the Caesar cipher: set a = 1 and the formula collapses to (x + b) mod 26, which is just a Caesar shift of b.
Why “a” must be coprime with 26
You cannot pick just any a. For the cipher to be reversible, every ciphertext letter must come from exactly one plaintext letter — the mapping has to be one-to-one. That happens only when a shares no factor with 26 (26 = 2 × 13), i.e. the greatest common divisor of a and 26 is 1. If a is even or a multiple of 13, different letters collide on the same output and decryption becomes ambiguous.
That leaves 12 valid values for a: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25. The tool above rejects any other a for exactly this reason. The additive key b has no such restriction — it can be any value from 0 to 25.
Worked example: encrypt and decrypt
Using the tool’s defaults, a = 5, b = 8:
Encrypt the letter H. H is position 7:
E(7) = (5 · 7 + 8) mod 26 = 43 mod 26 = 17 → R
Decrypt R back to H. Decryption uses the modular inverse of a. The inverse of 5 mod 26 is 21 (because 5 × 21 = 105 = 1 mod 26), so:
D(y) = 21 · (y − 8) mod 26 → D(17) = 21 · 9 mod 26 = 189 mod 26 = 7 → H
Apply the same two steps to every letter and HELLO becomes RCLLA — the exact result the tool above produces.
History and security
The affine cipher is a classical substitution cipher — the mathematical generalisation of the Caesar cipher, which the Roman writer Suetonius records Julius Caesar using. It is a teaching tool and a puzzle cipher, not a way to protect real data.
Its whole key space is tiny: 12 choices for a times 26 choices for b gives just 312 possible keys, so a computer can try all of them in an instant. Even by hand it falls to frequency analysis, because it is monoalphabetic — the most common ciphertext letter is very likely the encrypted form of E. Use it to learn how modular arithmetic drives a cipher, not for security.
Frequently asked questions
What is the affine cipher?
It is a substitution cipher that encrypts each letter with the formula E(x) = (a·x + b) mod 26, using a multiplicative key a and an additive key b. It generalises the Caesar cipher.
Why must a be coprime with 26?
So the mapping is reversible. If a shares a factor with 26 (an even number or a multiple of 13), two letters encrypt to the same one and you cannot decrypt uniquely. The 12 valid values are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25.
How do you decrypt an affine cipher?
With D(y) = a⁻¹·(y − b) mod 26, where a⁻¹ is the modular inverse of a. For a = 5 the inverse is 21, so R (17) decrypts to 21·(17−8) mod 26 = 7 = H.
Is the affine cipher the same as the Caesar cipher?
The Caesar cipher is the special case a = 1, which leaves only the shift b. Any a other than 1 also scales the alphabet, which is what makes it “affine” rather than a pure shift.
Is the affine cipher secure?
No. With only 312 possible keys it is trivially brute-forced, and as a monoalphabetic cipher it is broken by frequency analysis. It is for learning and puzzles, not real protection.
Features
Privacy Focused
All processing happens in your browser. No data is sent to servers.
Free Forever
No registration, no limits, completely free to use.