The Affine Cipher is a type of monoalphabetic substitution cipher, which means each letter in the plaintext is mapped to a single corresponding letter in the ciphertext. The Affine Cipher combines both the Caesar Cipher and Multiplicative Cipher to create a more complex encryption method.
How the Affine Cipher Works
The encryption process for the Affine Cipher is a function that maps a letter x (where x is the position of the letter in the alphabet, starting with 0 for 'A') to a letter y using the formula:
y=(a×x+b)modm
Here:
y is the encrypted letter.
a and b are keys used in the cipher.
m is the size of the alphabet (for English, m=26).
x is the position of the plaintext letter in the alphabet.
To decrypt, you use the inverse of the function:
x=a−1×(y−b)modm
where:
a−1×a≡1modm
Example: Encrypting the Word "HELLO"
1. Choose keys a and b:
Let a=5 and b=8.
Check that a and m=26 are coprime (i.e., they have no common divisors other than 1). For this example, 5 and 26 are coprime, so we can use 5 as a.
2. Convert each letter of "HELLO" to its numerical equivalent:
H = 7, E = 4, L = 11, L = 11, O = 14.
3. Apply the encryption formula:
For H:
y=(5×7+8)mod26=(35+8)mod26=43mod26=17→R
For E:
y=(5×4+8)mod26=(20+8)mod26=28mod26=2→C
For L:
y=(5×11+8)mod26=(55+8)mod26=63mod26=11→L
For L:
y=11→L(sameasabove)
For O:
y=(5×14+8)mod26=(70+8)mod26=78mod26=0→A
The ciphertext for "HELLO" is "RCLLA".
Decrypting the Ciphertext "RCLLA"
1. Find the modular inverse of 𝑎 = 5 modulo 𝑚 = 26
By trial or using the Extended Euclidean Algorithm:
a−1=21since(5×21)mod26=1
2. Convert each letter of "RCLLA" back to its numerical equivalent:
R = 17, C = 2, L = 11, L = 11, A = 0.
3. Apply the decryption formula:
For R:
x=21×(17−8)mod26=(21×9)mod26=189mod26=7→H
For C:
x=21×(2−8)mod26=21×(−6)mod26=−126mod26=4→E
For L:
x=21×(11−8)mod26=21×3mod26=63mod26=11→L
For L:
x=11→L(sameasabove)
For A:
x=21×(0−8)mod26=21×(−8)mod26=−168mod26=14→O
4. The decrypted plaintext is "HELLO".
Summary
The Affine Cipher encryption is more secure than the Caesar Cipher because it adds both multiplication and addition, making it harder to break without knowing both keys. However, it is still vulnerable to frequency analysis because it is a substitution cipher.