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) \mod m$where:

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

To decrypt the message, the formula:

$D(y) = a^{-1}(y - b) \mod m$is used, where $a^{-1}$ is the modular multiplicative inverse of $a$ modulo $m$ .

### Example

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

**Convert each letter to its numeric position:**- H = 7
- E = 4
- L = 11
- L = 11
- O = 14

**Apply the encryption formula****$E(x) = (5x + 8) \mod 26$****:**- For H (7): $(5 \times 7 + 8) \mod 26 = 43 \mod 26 = 17$ → R
- For E (4): $(5 \times 4 + 8) \mod 26 = 28 \mod 26 = 2$ → C
- For L (11): $(5 \times 11 + 8) \mod 26 = 63 \mod 26 = 11$ → L
- For L (11): $(5 \times 11 + 8) \mod 26 = 63 \mod 26 = 11$ → L
- For O (14): $(5 \times 14 + 8) \mod 26 = 78 \mod 26 = 0$ → A

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

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

Plaintext | Numeric Value | Encrypted (5x + 8) | Encrypted Letter |
---|---|---|---|

A | 0 | 8 | I |

B | 1 | 13 | N |

C | 2 | 18 | S |

D | 3 | 23 | X |

E | 4 | 2 | C |

F | 5 | 7 | H |

G | 6 | 12 | M |

H | 7 | 17 | R |

I | 8 | 22 | W |

J | 9 | 1 | B |

K | 10 | 6 | G |

L | 11 | 11 | L |

M | 12 | 16 | Q |

N | 13 | 21 | V |

O | 14 | 0 | A |

P | 15 | 5 | F |

Q | 16 | 10 | K |

R | 17 | 15 | P |

S | 18 | 20 | U |

T | 19 | 25 | Z |

U | 20 | 4 | E |

V | 21 | 9 | J |

W | 22 | 14 | O |

X | 23 | 19 | T |

Y | 24 | 24 | Y |

Z | 25 | 3 | D |

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