Affine Cipher Calculator
Encrypt or decrypt text with the key (a, b), see why a must be coprime to 26, and follow the modular inverse and each letter calculation.
What is the affine cipher?
The affine cipher is a substitution cipher that generalises the Caesar cipher. Each letter is first converted to a number (A = 0, …, Z = 25), then multiplied by a key number a, shifted by a second key number b, and reduced modulo 26: E(x) = (a·x + b) mod 26. The Caesar cipher is the special case a = 1.
The key is the pair (a, b). Since b can be any of the 26 values and a can only be one of 12 values, the cipher has 12 × 26 = 312 possible keys.
Why a must be coprime to 26
Encryption must be reversible, which means different letters have to produce different cipher letters. If a shares a factor with 26, that fails. Take a = 2: the letters A (0) and N (13) give 2·0 + b = b and 2·13 + b = 26 + b, which are both b modulo 26, so two letters collide and the receiver cannot tell which one was sent.
The condition is gcd(a, 26) = 1, so a must be odd and not a multiple of 13. The twelve valid values are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. Their count is Euler's totient φ(26) = 12.
Decryption and the modular inverse
To undo the encryption, solve y = a·x + b for x: D(y) = a⁻¹·(y − b) mod 26, where a⁻¹ is the number with a·a⁻¹ ≡ 1 (mod 26). It is found with the extended Euclidean algorithm. For a = 5 the algorithm gives 5·(−5) + 26·1 = 1, so a⁻¹ = −5 mod 26 = 21; check: 5·21 = 105 = 4·26 + 1.
The inverses of the twelve valid values are: 1 → 1, 3 → 9, 5 → 21, 7 → 15, 9 → 3, 11 → 19, 15 → 7, 17 → 23, 19 → 11, 21 → 5, 23 → 17 and 25 → 25.
Special cases and security
With a = 25 and b = 25 the formula becomes 25·x + 25 ≡ 25 − x (mod 26), which reverses the alphabet: this is the Atbash cipher. The affine cipher is easy to break: there are only 312 keys, letter frequencies are preserved, and just two known plaintext letters are enough to solve for a and b.
Worked examples
These examples are computed by the same engine as the calculator above, so you can compare each step with your own working.
a = 5, b = 8
- Mode:
Encrypt - Text:
AFFINE CIPHER - Key a:
5 - Key b:
8
Ciphertext
IHHWVC SWFRCP
key (a, b) = (5, 8), a⁻¹ = 21
Step-by-step solution
Formula used
E(x) = (a·x + b) mod 26
A = 0, …, Z = 25; a = 5, b = 8
NoteNumber the letters
A = 0, B = 1, …, Z = 25. Letters keep their case; other characters are copied unchanged.
Key: a = 5, b = 8.
CheckCheck that a is allowed: gcd(a, 26) = 1
gcd(5, 26) = 1 ✓
Valid values of a: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25 (these are the φ(26) = 12 numbers coprime to 26).
CalculateFind a⁻¹ mod 26 with the extended Euclidean algorithm
a·s + 26·t = gcd(a, 26) = 1 → a⁻¹ = s mod 26
Bézout: 5·(-5) + 26·(1) = 1
a⁻¹ = -5 mod 26 = 21
Check: 5 · 21 = 105 = 4·26 + 1 ≡ 1 (mod 26) ✓
| i | q | r | s | t |
|---|
| 0 | — | 5 | 1 | 0 |
| 1 | — | 26 | 0 | 1 |
| 2 | 0 | 5 | 1 | 0 |
| 3 | 5 | 1 | -5 | 1 |
| 4 | 5 | 0 | 26 | -5 |
CalculateEncrypt every letter
y = (a·x + b) mod 26
| # | Letter | x | a·x + b | mod 26 = y | Result |
|---|
| 1 | A | 0 | 5·0 + 8 = 8 | 8 | I |
| 2 | F | 5 | 5·5 + 8 = 33 | 7 | H |
| 3 | F | 5 | 5·5 + 8 = 33 | 7 | H |
| 4 | I | 8 | 5·8 + 8 = 48 | 22 | W |
| 5 | N | 13 | 5·13 + 8 = 73 | 21 | V |
| 6 | E | 4 | 5·4 + 8 = 28 | 2 | C |
| 7 | C | 2 | 5·2 + 8 = 18 | 18 | S |
| 8 | I | 8 | 5·8 + 8 = 48 | 22 | W |
| 9 | P | 15 | 5·15 + 8 = 83 | 5 | F |
| 10 | H | 7 | 5·7 + 8 = 43 | 17 | R |
| 11 | E | 4 | 5·4 + 8 = 28 | 2 | C |
| 12 | R | 17 | 5·17 + 8 = 93 | 15 | P |
CheckVerify by reversing
Decrypting the result with the same key gives back the original text: ✓ yes
ResultCiphertext
IHHWVC SWFRCP
Decrypt IHHWVCSWFRCP
- Mode:
Decrypt - Text:
IHHWVCSWFRCP - Key a:
5 - Key b:
8
Plaintext
AFFINECIPHER
key (a, b) = (5, 8), a⁻¹ = 21
Step-by-step solution
Formula used
D(y) = a⁻¹·(y − b) mod 26
a⁻¹ is the inverse of a modulo 26: a·a⁻¹ ≡ 1 (mod 26)
A = 0, …, Z = 25; a = 5, b = 8, a⁻¹ = 21
NoteNumber the letters
A = 0, B = 1, …, Z = 25. Letters keep their case; other characters are copied unchanged.
Key: a = 5, b = 8.
CheckCheck that a is allowed: gcd(a, 26) = 1
gcd(5, 26) = 1 ✓
Valid values of a: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25 (these are the φ(26) = 12 numbers coprime to 26).
CalculateFind a⁻¹ mod 26 with the extended Euclidean algorithm
a·s + 26·t = gcd(a, 26) = 1 → a⁻¹ = s mod 26
Bézout: 5·(-5) + 26·(1) = 1
a⁻¹ = -5 mod 26 = 21
Check: 5 · 21 = 105 = 4·26 + 1 ≡ 1 (mod 26) ✓
| i | q | r | s | t |
|---|
| 0 | — | 5 | 1 | 0 |
| 1 | — | 26 | 0 | 1 |
| 2 | 0 | 5 | 1 | 0 |
| 3 | 5 | 1 | -5 | 1 |
| 4 | 5 | 0 | 26 | -5 |
CalculateDecrypt every letter
x = a⁻¹·(y − b) mod 26
| # | Letter | y | y − b | a⁻¹·(y − b) | mod 26 = x | Result |
|---|
| 1 | I | 8 | 8 − 8 = 0 | 21·(0) = 0 | 0 | A |
| 2 | H | 7 | 7 − 8 = -1 | 21·(-1) = -21 | 5 | F |
| 3 | H | 7 | 7 − 8 = -1 | 21·(-1) = -21 | 5 | F |
| 4 | W | 22 | 22 − 8 = 14 | 21·(14) = 294 | 8 | I |
| 5 | V | 21 | 21 − 8 = 13 | 21·(13) = 273 | 13 | N |
| 6 | C | 2 | 2 − 8 = -6 | 21·(-6) = -126 | 4 | E |
| 7 | S | 18 | 18 − 8 = 10 | 21·(10) = 210 | 2 | C |
| 8 | W | 22 | 22 − 8 = 14 | 21·(14) = 294 | 8 | I |
| 9 | F | 5 | 5 − 8 = -3 | 21·(-3) = -63 | 15 | P |
| 10 | R | 17 | 17 − 8 = 9 | 21·(9) = 189 | 7 | H |
| 11 | C | 2 | 2 − 8 = -6 | 21·(-6) = -126 | 4 | E |
| 12 | P | 15 | 15 − 8 = 7 | 21·(7) = 147 | 17 | R |
CheckVerify by reversing
Encrypting the result with the same key gives back the original text: ✓ yes
ResultPlaintext
AFFINECIPHER
Atbash (a = b = 25)
- Mode:
Encrypt - Text:
WIZARD - Key a:
25 - Key b:
25
Ciphertext
DRAZIW
key (a, b) = (25, 25), a⁻¹ = 25
a = 25, b = 25 is the Atbash cipher: the alphabet is reversed (A ↔ Z, B ↔ Y, …).
Step-by-step solution
Formula used
E(x) = (a·x + b) mod 26
A = 0, …, Z = 25; a = 25, b = 25
NoteNumber the letters
A = 0, B = 1, …, Z = 25. Letters keep their case; other characters are copied unchanged.
Key: a = 25, b = 25.
CheckCheck that a is allowed: gcd(a, 26) = 1
gcd(25, 26) = 1 ✓
Valid values of a: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25 (these are the φ(26) = 12 numbers coprime to 26).
CalculateFind a⁻¹ mod 26 with the extended Euclidean algorithm
a·s + 26·t = gcd(a, 26) = 1 → a⁻¹ = s mod 26
Bézout: 25·(-1) + 26·(1) = 1
a⁻¹ = -1 mod 26 = 25
Check: 25 · 25 = 625 = 24·26 + 1 ≡ 1 (mod 26) ✓
| i | q | r | s | t |
|---|
| 0 | — | 25 | 1 | 0 |
| 1 | — | 26 | 0 | 1 |
| 2 | 0 | 25 | 1 | 0 |
| 3 | 1 | 1 | -1 | 1 |
| 4 | 25 | 0 | 26 | -25 |
CalculateEncrypt every letter
y = (a·x + b) mod 26
| # | Letter | x | a·x + b | mod 26 = y | Result |
|---|
| 1 | W | 22 | 25·22 + 25 = 575 | 3 | D |
| 2 | I | 8 | 25·8 + 25 = 225 | 17 | R |
| 3 | Z | 25 | 25·25 + 25 = 650 | 0 | A |
| 4 | A | 0 | 25·0 + 25 = 25 | 25 | Z |
| 5 | R | 17 | 25·17 + 25 = 450 | 8 | I |
| 6 | D | 3 | 25·3 + 25 = 100 | 22 | W |
CheckVerify by reversing
Decrypting the result with the same key gives back the original text: ✓ yes
ResultCiphertext
DRAZIW
Frequently asked questions
How many keys does the affine cipher have?
There are 12 valid choices for a and 26 for b, so 312 keys in total. The identity key a = 1, b = 0 leaves the text unchanged.
Why does the calculator reject even numbers and 13 for a?
Those values share a factor with 26, so the encryption would map two letters to the same cipher letter and could not be decrypted.
What is the Atbash cipher?
Atbash maps A to Z, B to Y and so on. It is the affine cipher with a = 25 and b = 25.
How do I find the modular inverse of a by hand?
Either use the extended Euclidean algorithm on a and 26, as shown in the step table, or look for the number between 1 and 25 whose product with a leaves remainder 1 when divided by 26.
Can the affine cipher be broken with two known letters?
Yes. Two plaintext and ciphertext pairs give two linear congruences in a and b, which can be solved modulo 26.