Affine Cipher Explained: Encrypt and Decrypt with Examples
2026-06-09 · 6 min read
Figure 1: Affine Cipher Explained
What is the Affine Cipher?
The affine cipher is a substitution cipher that uses a simple mathematical formula to encrypt each letter in a message.
It is a type of monoalphabetic cipher, meaning every occurrence of a given letter always maps to the same output letter.
The affine cipher is more complex than the Caesar cipher because it combines two operations: multiplication and addition. This gives it a larger key space and makes it slightly harder to crack by guessing.
How the Affine Cipher Works
The affine cipher uses two keys, typically written as a and b.
The encryption formula is:
E(x) = (a * x + b) mod 26
The decryption formula is:
D(y) = a_inv * (y - b) mod 26
Where:
- x is the numeric value of the plaintext letter (A = 0, B = 1, ..., Z = 25)
- y is the numeric value of the ciphertext letter
- a is the multiplicative key
- b is the additive key (shift)
- a_inv is the modular multiplicative inverse of a modulo 26
Valid Values for the Key a
The key a must be coprime with 26. This means the greatest common divisor of a and 26 must equal 1.
Valid values for a:
1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25
If a is not coprime with 26, some output letters will collide with each other, making decryption impossible.
The key b can be any value from 0 to 25.
This gives a total of 12 valid values for a and 26 values for b, producing 312 unique key combinations.
Modular Inverse of a
To decrypt an affine cipher, you need the modular inverse of the key a modulo 26.
The modular inverse of a is the value a_inv such that:
a * a_inv mod 26 = 1
Common inverse pairs:
| a | a_inv |
|---|---|
| 1 | 1 |
| 3 | 9 |
| 5 | 21 |
| 7 | 15 |
| 9 | 3 |
| 11 | 19 |
| 15 | 7 |
| 17 | 23 |
| 19 | 11 |
| 21 | 5 |
| 23 | 17 |
| 25 | 25 |
Affine Cipher Encryption Example
Keys: a = 5, b = 8
Encrypt the message: HELLO
Step 1: Convert each letter to a number.
| Letter | Value |
|---|---|
| H | 7 |
| E | 4 |
| L | 11 |
| L | 11 |
| O | 14 |
Step 2: Apply E(x) = (5 * x + 8) mod 26
| x | 5x + 8 | mod 26 | Letter |
|---|---|---|---|
| 7 | 43 | 17 | R |
| 4 | 28 | 2 | C |
| 11 | 63 | 11 | L |
| 11 | 63 | 11 | L |
| 14 | 78 | 0 | A |
Encrypted output: RCLLA
Affine Cipher Decryption Example
Keys: a = 5, b = 8, a_inv = 21
Decrypt the message: RCLLA
Step 1: Convert each letter to a number.
| Letter | Value |
|---|---|
| R | 17 |
| C | 2 |
| L | 11 |
| L | 11 |
| A | 0 |
Step 2: Apply D(y) = 21 * (y - 8) mod 26
| y | y - 8 | 21 * (y - 8) | mod 26 | Letter |
|---|---|---|---|---|
| 17 | 9 | 189 | 7 | H |
| 2 | -6 | -126 | 4 | E |
| 11 | 3 | 63 | 11 | L |
| 11 | 3 | 63 | 11 | L |
| 0 | -8 | -168 | 14 | O |
Decrypted output: HELLO
Note: When working with negative numbers modulo 26, add 26 until the result is positive. For example, -126 mod 26: add enough multiples of 26 to reach a positive result. -126 + 130 = 4.
Affine Cipher vs Caesar Cipher
The affine cipher is a generalization of the Caesar cipher.
When the key a equals 1, the affine cipher formula simplifies to:
E(x) = (1 * x + b) mod 26 = (x + b) mod 26
This is exactly the Caesar cipher with a shift of b.
| Feature | Caesar Cipher | Affine Cipher |
|---|---|---|
| Keys | 1 (shift only) | 2 (a and b) |
| Key space | 25 | 312 |
| Formula | x + b | (a * x + b) mod 26 |
| Inverse required | No | Yes (for a) |
| Strength | Very weak | Weak but stronger than Caesar |
How to Crack the Affine Cipher
Because the affine cipher has only 312 possible key combinations, it can be broken by brute force. Try all 312 combinations and check which output looks like readable text.
More targeted approaches:
Frequency analysis:
The affine cipher is monoalphabetic, so the frequency distribution of letters in the ciphertext follows the same pattern as the plaintext language. In English, the most common letters are E, T, A, O, I, N.
If you identify the most frequent ciphertext letter and assume it corresponds to E (position 4), and the second most frequent and assume it corresponds to T (position 19), you can set up two equations and solve for a and b.
Known plaintext:
If you know even two plaintext-ciphertext letter pairs, you can solve the system directly:
y1 = a * x1 + b mod 26
y2 = a * x2 + b mod 26
Subtract the two equations to isolate a, then solve for b.
Affine Cipher in CTF Challenges
The affine cipher appears in CTF challenges as an intermediate-level cryptography task.
Common CTF scenarios:
- A ciphertext is given with the keys a and b. Apply the decryption formula directly.
- Keys are hidden in the challenge description or metadata. Extract them and decrypt.
- No keys are given. Use frequency analysis or brute force all 312 combinations.
- The challenge presents the formula itself. Implement it or use an online tool.
How to identify an affine cipher:
- The ciphertext uses only letters with no numbers or symbols.
- The letter frequency distribution is shifted from normal English but retains a similar pattern.
- The challenge mentions multiplication, modular arithmetic, or two numerical keys.
Decrypt Affine Cipher Online
You can decrypt affine cipher messages instantly at KeyDecryptor Affine Cipher Decoder.
Steps:
- Paste your ciphertext.
- Enter the keys a and b.
- Click Decrypt to recover the original message.
The tool handles the modular inverse calculation automatically.
Frequently Asked Questions
Why must the key a be coprime with 26?
If a shares a common factor with 26, some input letters will map to the same output letter. This makes the cipher non-invertible, meaning decryption becomes impossible because multiple plaintexts would produce the same ciphertext.
What happens when a equals 1 in the affine cipher?
When a equals 1, the affine cipher behaves exactly like the Caesar cipher. The multiplication step becomes a no-op and only the additive shift b remains.
Can the affine cipher be used for real security?
No. The affine cipher has only 312 key combinations and uses a single fixed substitution pattern. It can be broken in seconds by brute force or frequency analysis. It is used for education and puzzles only.
What is frequency analysis?
Frequency analysis is a technique for breaking substitution ciphers by comparing the letter frequency in the ciphertext to known letter frequencies in the target language. In English, E is the most common letter, appearing in roughly 13 percent of text. Mapping the most frequent ciphertext letter to E often reveals the key.
How many keys does the affine cipher have?
There are 12 valid values for a (those coprime with 26) and 26 values for b (0 through 25), giving 12 times 26 equals 312 unique key combinations. This is the total key space.
Is the affine cipher the same as Atbash?
Atbash is a special case of the affine cipher with a equals 25 and b equals 25. In Atbash, A maps to Z, B maps to Y, and so on. Like ROT13, Atbash is self-reversing.