๐Ÿงฎ Affine Cipher โ€” Step-by-Step Example

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.


๐Ÿค” What is the Affine Cipher?

The Affine cipher replaces each letter using this formula:

E(x)โ€„=โ€„(aโ€…ร—โ€…xโ€…+โ€…b)โ€€modโ€†ย 26

Where:

What does "mod" mean? ๐Ÿ•

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!

What does "coprime" mean?

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)

Letter-to-Number Table

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

๐Ÿ“œ The Problem

Encrypt the message: PLANET Using keys: a = 3, b = 5 Formula: E(x)โ€„=โ€„(3xโ€…+โ€…5)โ€€modโ€†ย 26


๐Ÿ•ต๏ธ Step-by-Step Encryption

Step 1: Convert each letter to a number

Letter P L A N E T
Number (x) 15 11 0 13 4 19

Step 2: Apply the formula to each number

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


Step 3: Write the result!

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


๐Ÿ”“ Now Let's Decrypt It!

You received the ciphertext: YMFSRK You know the keys are a = 3, b = 5.

Step 1: Find the "modular inverse" of a

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!)


Step 2: The decryption formula

D(y)โ€„=โ€„aโˆ’1โ€…ร—โ€…(yโ€…โˆ’โ€…b)โ€€modโ€†ย 26

With our values:

D(y)โ€„=โ€„9โ€…ร—โ€…(yโ€…โˆ’โ€…5)โ€€modโ€†ย 26


Step 3: Decrypt each letter

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 โœ“


Step 4: Read the answer!

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 ๐Ÿช๐ŸŽ‰


โš ๏ธ What About Negative Numbers?

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


๐Ÿง  Key Things to Remember

  1. Encryption: E(x)โ€„=โ€„(axโ€…+โ€…b)โ€€modโ€†ย 26
  2. Decryption: D(y)โ€„=โ€„aโˆ’1(yโ€…โˆ’โ€…b)โ€€modโ€†ย 26
  3. a must be coprime with 26 โ€” valid values: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25
  4. b can be any number from 0 to 25
  5. Memorize the inverse table! It saves tons of time in competition
  6. Negative mod: Just add 26 until positive

๐ŸŽฏ Practice Problem

Decrypt this message using a = 7, b = 2:

ZEBBW

Hint: Start by finding the modular inverse of 7 from the table above!

Click to reveal the answer

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! ๐Ÿ”๐Ÿ†