Affine cipher

The Affine cipher is a type of substitution cipher rooted in modular arithmetic, which falls under the category of monoalphabetic ciphers. It has origins in classical cryptography, dating back to the use of basic substitution techniques by ancient civilizations. While there isn’t a single, clear creator or exact date for the invention of the affine cipher specifically, it embodies methods commonly attributed to early cipher systems used throughout history, such as by Roman and Greek scholars.

The Affine cipher was used primarily for its simplicity and ease of implementation, especially before more advanced encryption techniques became prevalent. Its structure allows a single mathematical transformation to be applied to each letter in the alphabet, making it a basic but effective way of encoding messages in settings where highly complex ciphers were impractical. Its use has dwindled with the advent of modern encryption methods, but it still serves as a fundamental example in the study of classical cryptography and modular mathematics.

In the affine cipher, each letter in the plaintext message is transformed using the formula:

E(x)=(ax+b)modmE(x) = (ax + b) \mod m

where:

  • E(x)E(x) is the encrypted letter,
  • xx is the numerical position of the plaintext letter (e.g., A=0A = 0, B=1B = 1, ..., Z=25Z = 25 in a 26-letter alphabet),
  • aa and bb are keys that define the cipher (with aa being coprime to mm),
  • mm is the size of the alphabet (typically 26 for the English alphabet).

To decrypt the message, the formula:

D(y)=a1(yb)modmD(y) = a^{-1}(y - b) \mod m

is used, where a1a^{-1} is the modular multiplicative inverse of aa modulo mm.

Example

Let’s say we choose a=5a = 5 and b=8b = 8 as our keys, and we want to encrypt the word "HELLO".

  1. Convert each letter to its numeric position:
    • H = 7
    • E = 4
    • L = 11
    • L = 11
    • O = 14
  2. Apply the encryption formula E(x)=(5x+8)mod26E(x) = (5x + 8) \mod 26:
    • For H (7): (5×7+8)mod26=43mod26=17(5 \times 7 + 8) \mod 26 = 43 \mod 26 = 17 → R
    • For E (4): (5×4+8)mod26=28mod26=2(5 \times 4 + 8) \mod 26 = 28 \mod 26 = 2 → C
    • For L (11): (5×11+8)mod26=63mod26=11(5 \times 11 + 8) \mod 26 = 63 \mod 26 = 11 → L
    • For L (11): (5×11+8)mod26=63mod26=11(5 \times 11 + 8) \mod 26 = 63 \mod 26 = 11 → L
    • For O (14): (5×14+8)mod26=78mod26=0(5 \times 14 + 8) \mod 26 = 78 \mod 26 = 0 → A

So, "HELLO" becomes "RCLLA" after encryption.

Affine Cipher Table (a=5, b=8)

PlaintextNumeric ValueEncrypted (5x + 8)Encrypted Letter
A08I
B113N
C218S
D323X
E42C
F57H
G612M
H717R
I822W
J91B
K106G
L1111L
M1216Q
N1321V
O140A
P155F
Q1610K
R1715P
S1820U
T1925Z
U204E
V219J
W2214O
X2319T
Y2424Y
Z253D

This table illustrates how each letter in the alphabet is transformed when using a=5a = 5 and b=8b = 8. The affine cipher provides a systematic way to substitute letters and is foundational in understanding the mechanics of basic cryptographic transformations.