The Affine Cipher is a type of substitution cipher that uses a mathematical function to encrypt and decrypt messages. Here's a step-by-step guide with a practical example:
Step 1: Understand the Encryption Formula
The Affine Cipher encryption formula is:
E(x)=(a.x+b)modm
Where:
π₯ is the numerical value of a letter (e.g., A = 0, B = 1, ..., Z = 25 for English).
π and π are keys of the cipher.
π must be coprime with π, meaning gcd(π,π)=1.
π is the size of the alphabet (26 for English).
Step 2: Choose Keys
Select values for π and π. For this example:
π=5
π=8
π=26 (English alphabet)
Step 3: Encryption Example
Plaintext: "HELLO"
Convert Letters to Numbers:
Using π΄=0,π΅=1,...,π=25:
π»=7,πΈ=4,πΏ=11,π=14
Apply Encryption Formula:
For each letter, compute:
E(x)=(5.x+8)mod26
π»(7):πΈ(7)=(5β 7+8) mod 26 =43 mod 26 = 17 (R)
πΈ(4):πΈ(4)=(5β 4+8) mod 26 =28 mod 26 = 2 (C)
πΏ(11):πΈ(11)=(5β 11+8) mod 26 =63 mod 26 = 11 (L)
πΏ(11):πΈ(11)=(5β 11+8) mod 26 =63 mod 26 = 11 (L)
π(14):πΈ(14)=(5β 14+8) mod 26 =78 mod 26 = 0 (A)
Encrypted Message: "RCLLA"
Step 4: Understand the Decryption Formula
The decryption formula is:
D(y)=ainvβ.(yβb)modm
Where:
π¦ is the numerical value of the ciphertext letter.
πinv is the modular multiplicative inverse of π modulo π, satisfying:
a.ainvββ‘1modm
Step 5: Find πinv
For π=5 and π=26:
Find πinv such that 5β πinvβ‘1 mod 26.
πinv=21 (calculated using the Extended Euclidean Algorithm).
Step 6: Decrypt Example
Ciphertext: "RCLLA"
Convert Letters to Numbers:
π =17, πΆ=2, πΏ=11, π΄=0
Apply Decryption Formula:
For each letter, compute:
D(y)=21.(yβ8)mod26
R(17):D(17)=21β (17β8) mod 26=21β 9 mod 26=189 mod 26=7 (H)
C(2):D(2)=21β (2β8 ) mod 26=21β (β6) mod 26=β126 mod 26=4 (E)
L(11):D(11)=21β (11β8) mod 26=21β 3 mod 26=63 mod 26=11 (L)
L(11):D(11)=21β (11β8) mod 26=21β 3 mod 26=63 mod 26=11 (L)
A(0):D(0)=21β (0β8) mod 26=21β (β8) mod 26=β168 mod 26=14 (O)
Decrypted Message: "HELLO"
Step 7: Python Code
Hereβs a Python implementation of the Affine Cipher, using the example provided. The code includes functions for both encryption and decryption.
from math import gcd
def affine_encrypt(plaintext, a, b, m):
"""Encrypts plaintext using the Affine Cipher."""
if gcd(a, m) != 1:
raise ValueError("Key 'a' must be coprime with 'm'.")
# Convert plaintext to uppercase and remove non-alphabetic characters
plaintext = ''.join(filter(str.isalpha, plaintext.upper()))
ciphertext = ''
for char in plaintext:
x = ord(char) - ord('A') # Convert character to number (A=0, B=1, ..., Z=25)
y = (a * x + b) % m # Apply encryption formula
ciphertext += chr(y + ord('A')) # Convert number back to character
return ciphertext
def mod_inverse(a, m):
"""Finds the modular multiplicative inverse of 'a' modulo 'm'."""
for x in range(1, m):
if (a * x) % m == 1:
return x
raise ValueError("Modular inverse does not exist.")
def affine_decrypt(ciphertext, a, b, m):
"""Decrypts ciphertext using the Affine Cipher."""
if gcd(a, m) != 1:
raise ValueError("Key 'a' must be coprime with 'm'.")
a_inv = mod_inverse(a, m) # Find modular inverse of 'a'
plaintext = ''
for char in ciphertext:
y = ord(char) - ord('A') # Convert character to number (A=0, B=1, ..., Z=25)
x = (a_inv * (y - b)) % m # Apply decryption formula
plaintext += chr(x + ord('A')) # Convert number back to character
return plaintext
# Example usage
def main():
m = 26 # Size of the alphabet
a = 5 # Key 'a'
b = 8 # Key 'b'
plaintext = "HELLO"
print(f"Plaintext: {plaintext}")
# Encrypt the plaintext
ciphertext = affine_encrypt(plaintext, a, b, m)
print(f"Ciphertext: {ciphertext}")
# Decrypt the ciphertext
decrypted_text = affine_decrypt(ciphertext, a, b, m)
print(f"Decrypted Text: {decrypted_text}")
if __name__ == "__main__":
main()