Affine Cipher

Yaser Rahmati | یاسر رحمتی

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)    mod    my=(a\times x+b)\;\;mod\;\;m

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=a1×(yb)    mod    mx=a^{-1}\times (y−b)\;\;mod\;\;m

where:

a1×a1    mod  ma^{-1}\times a≡1\;\;mod\;m

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)  mod  26=(35+8)  mod  26=43  mod  26=17Ry=(5\times 7+8)\;mod\;26=(35+8)\;mod\;26=43\;mod\;26=17 → R
  • For E:

y=(5×4+8)  mod  26=(20+8)  mod  26=28  mod  26=2Cy=(5\times 4+8)\;mod\;26=(20+8)\;mod\;26=28\;mod\;26=2 → C
  • For L:

y=(5×11+8)  mod  26=(55+8)  mod  26=63  mod  26=11Ly=(5\times 11+8)\;mod\;26=(55+8)\;mod\;26=63\;mod\;26=11 → L
  • For L:

y=11L  (same  as  above)y=11 → L \;(same\;as\;above)
  • For O:

y=(5×14+8)  mod  26=(70+8)  mod  26=78  mod  26=0Ay=(5\times 14+8)\;mod\;26=(70+8)\;mod\;26=78\;mod\;26=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:

𝑎1=21    since    (5×21)  mod  26=1𝑎^{-1}=21\;\; since\;\;(5\times21)\;mod\;26=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×(178)  mod  26=(21×9)  mod  26=189  mod  26=7Hx=21\times (17−8)\;mod\;26=(21\times9)\;mod\;26=189\;mod\;26=7 → H
  • For C:

x=21×(28)  mod  26=21×(6)  mod  26=126  mod  26=4Ex=21\times(2−8)\;mod\;26=21\times (−6)\;mod\;26=−126\;mod\;26=4 → E
  • For L:

x=21×(118)  mod  26=21×3  mod  26=63  mod  26=11Lx=21\times (11−8)\;mod\;26=21\times 3\;mod\;26=63\;mod\;26=11 → L
  • For L:

x=11L  (same  as  above)x=11 → L \; (same\;as\;above)
  • For A:

x=21×(08)  mod  26=21×(8)  mod  26=168  mod  26=14Ox=21\times (0−8)\;mod\;26=21\times (−8)\;mod\;26=−168\;mod\;26=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.

Keywords

Affine Cipher, encryption, decryption, monoalphabetic, substitution cipher, plaintext, ciphertext, Caesar Cipher, Multiplicative Cipher, modular arithmetic, coprime, keys, alphabet, modulo, encryption formula, decryption formula, modular inverse, cryptography, security, key space, frequency analysis, multiplicative key, additive key, cryptographic algorithm, affine transformation, letter mapping, numerical equivalent, affine function, Euclidean algorithm, Extended Euclidean Algorithm, modular multiplicative inverse, cipher text, letter positions, mathematical function, alphabet size, modular reduction, brute-force attack, computational complexity, modular operations, inverse calculation, cipher keys, cryptanalysis, encryption process, plaintext conversion, inverse function, mathematical encryption, alphabetic encryption, substitution technique, modular system , Yaser Rahmati , یاسر رحمتی

🌐 Personal Website 📄 Resume 🎥 Video Archive 💼 Finance Blog 🔐 Network & Security Notebook 🎬 Aparat Channel

Last updated