Welcome, codebusters! The Affine cipher uses a math formula to encrypt letters. Don't worry โ the math is simpler than it sounds! Let's walk through it together.
The Affine cipher replaces each letter using this formula:
E(x)โ=โ(aโ รโ xโ +โ b)โmodโย 26
Where:
Think of mod like a clock! If a clock has 26 hours and you're at hour 30, where are you? 30 - 26 = 4. That's mod!
a must be coprime with 26, meaning a and 26 don't share any common factors (other than 1).
Valid values for a: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25
Invalid values: 2, 4, 6, 8, 10, 12, 13, 14... (these share a factor with 26)
| A | B | C | D | E | F | G | H | I | J | K | L | M |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
Encrypt the message: PLANET
Using keys: a = 3, b = 5 Formula:
E(x)โ=โ(3xโ
+โ
5)โmodโย 26
| Letter | P | L | A | N | E | T |
|---|---|---|---|---|---|---|
| Number (x) | 15 | 11 | 0 | 13 | 4 | 19 |
P (x = 15): E(15)โ=โ(3โ รโ 15โ +โ 5)โmodโย 26โ=โ(45โ +โ 5)โmodโย 26โ=โ50โmodโย 26โ=โ24 50 รท 26 = 1 remainder 24 โ Letter Y
L (x = 11): E(11)โ=โ(3โ รโ 11โ +โ 5)โmodโย 26โ=โ(33โ +โ 5)โmodโย 26โ=โ38โmodโย 26โ=โ12 38 รท 26 = 1 remainder 12 โ Letter M
A (x = 0): E(0)โ=โ(3โ รโ 0โ +โ 5)โmodโย 26โ=โ(0โ +โ 5)โmodโย 26โ=โ5โmodโย 26โ=โ5 5 is less than 26, so it stays 5 โ Letter F
N (x = 13): E(13)โ=โ(3โ รโ 13โ +โ 5)โmodโย 26โ=โ(39โ +โ 5)โmodโย 26โ=โ44โmodโย 26โ=โ18 44 รท 26 = 1 remainder 18 โ Letter S
E (x = 4): E(4)โ=โ(3โ รโ 4โ +โ 5)โmodโย 26โ=โ(12โ +โ 5)โmodโย 26โ=โ17โmodโย 26โ=โ17 17 is less than 26, so it stays 17 โ Letter R
T (x = 19): E(19)โ=โ(3โ รโ 19โ +โ 5)โmodโย 26โ=โ(57โ +โ 5)โmodโย 26โ=โ62โmodโย 26โ=โ10 62 รท 26 = 2 remainder 10 โ Letter K
| Plain | P | L | A | N | E | T |
|---|---|---|---|---|---|---|
| x | 15 | 11 | 0 | 13 | 4 | 19 |
| 3x + 5 | 50 | 38 | 5 | 44 | 17 | 62 |
| mod 26 | 24 | 12 | 5 | 18 | 17 | 10 |
| Cipher | Y | M | F | S | R | K |
โ
Ciphertext: YMFSRK
You received the ciphertext: YMFSRK You know the keys
are a = 3, b = 5.
To decrypt, we need to undo the multiplication by 3. We need a special number called the modular inverse of a.
The modular inverse of a is a number aโ1 such that:
aโ รโ aโ1โโกโ1ย (modโย 26)
For a = 3, we need: 3โ รโ ?โ=โ1ย (modโย 26)
Let's test:
So aโ1โ=โ9
๐ก Tip: Here's a handy cheat sheet of modular inverses (mod 26):
| a | 1 | 3 | 5 | 7 | 9 | 11 | 15 | 17 | 19 | 21 | 23 | 25 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| aโปยน | 1 | 9 | 21 | 15 | 3 | 19 | 7 | 23 | 11 | 5 | 17 | 25 |
(Memorize this table โ it shows up a LOT in competition!)
D(y)โ=โaโ1โ รโ (yโ โโ b)โmodโย 26
With our values:
D(y)โ=โ9โ รโ (yโ โโ 5)โmodโย 26
Y (y = 24): D(24)โ=โ9โ รโ (24โ โโ 5)โmodโย 26โ=โ9โ รโ 19โmodโย 26โ=โ171โmodโย 26 171 รท 26 = 6 remainder 15 โ Letter P โ
M (y = 12): D(12)โ=โ9โ รโ (12โ โโ 5)โmodโย 26โ=โ9โ รโ 7โmodโย 26โ=โ63โmodโย 26 63 รท 26 = 2 remainder 11 โ Letter L โ
F (y = 5): D(5)โ=โ9โ รโ (5โ โโ 5)โmodโย 26โ=โ9โ รโ 0โmodโย 26โ=โ0โmodโย 26 0 โ Letter A โ
S (y = 18): D(18)โ=โ9โ รโ (18โ โโ 5)โmodโย 26โ=โ9โ รโ 13โmodโย 26โ=โ117โmodโย 26 117 รท 26 = 4 remainder 13 โ Letter N โ
R (y = 17): D(17)โ=โ9โ รโ (17โ โโ 5)โmodโย 26โ=โ9โ รโ 12โmodโย 26โ=โ108โmodโย 26 108 รท 26 = 4 remainder 4 โ Letter E โ
K (y = 10): D(10)โ=โ9โ รโ (10โ โโ 5)โmodโย 26โ=โ9โ รโ 5โmodโย 26โ=โ45โmodโย 26 45 รท 26 = 1 remainder 19 โ Letter T โ
| Cipher | Y | M | F | S | R | K |
|---|---|---|---|---|---|---|
| y | 24 | 12 | 5 | 18 | 17 | 10 |
| y - 5 | 19 | 7 | 0 | 13 | 12 | 5 |
| ร 9 | 171 | 63 | 0 | 117 | 108 | 45 |
| mod 26 | 15 | 11 | 0 | 13 | 4 | 19 |
| Plain | P | L | A | N | E | T |
โ
Plaintext: PLANET ๐ช๐
Sometimes when you subtract b, you get a negative number. Don't panic!
Example: If y = 2 and b = 5:
To handle negatives with mod 26, just add 26 until the number is positive:
Then continue with 23.
Another example: โ10โmodโย 26โ=โโ10โ +โ 26โ=โ16
Decrypt this message using a = 7, b = 2:
ZEBBW
Hint: Start by finding the modular inverse of 7 from the table above!
Step 1: Find inverse of 7 โ aโปยน = 15 (because 7 ร 15 = 105, and 105 mod 26 = 1)
Step 2: Formula: D(y) = 15 ร (y - 2) mod 26
Step 3: Decrypt each letter:
Plaintext: HELLO ๐
Happy calculating! ๐๐