Cipher 101 Tutorials

Affine Cipher Tutorial

Intermediate 25 minutes

What is an Affine Cipher?

The Affine cipher is a type of monoalphabetic substitution cipher, where each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The encryption function is `E(x) = (ax + b) mod 26`, where 'x' is the numeric equivalent of the letter, and 'a' and 'b' are the keys of the cipher.

The Math Behind It

For the cipher to be unbreakable, 'a' must be coprime with 26. This means that the greatest common divisor of 'a' and 26 must be 1. The possible values for 'a' are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. The value of 'b' can be any integer from 0 to 25.

To decrypt, we use the function `D(y) = a_inv * (y - b) mod 26`, where `a_inv` is the modular multiplicative inverse of 'a' modulo 26.

Practice Time!

Let's try to encrypt the word "HELLO" with a=5 and b=8.

H -> 7 -> (5*7 + 8) mod 26 = 43 mod 26 = 17 -> R

E -> 4 -> (5*4 + 8) mod 26 = 28 mod 26 = 2 -> C

L -> 11 -> (5*11 + 8) mod 26 = 63 mod 26 = 11 -> L

L -> 11 -> (5*11 + 8) mod 26 = 63 mod 26 = 11 -> L

O -> 14 -> (5*14 + 8) mod 26 = 78 mod 26 = 0 -> A

So, "HELLO" becomes "RCLLA".