# Affine Cipher

An **affine cipher** is a type of substitution cipher, which encodes each letter of the alphabet by applying a mathematical transformation. The affine cipher uses a linear function to determine the substitution of each letter.

## Encryption

The encryption function of an affine cipher is:

E(x) = (a * x + b) mod m

Where:

`x`

is the numerical value of the letter to be encrypted (A = 0, B = 1, ..., Z = 25).`a`

and`b`

are the keys of the cipher.`a`

must be coprime with`m`

(the size of the alphabet, typically 26 for English).`b`

is the shift value.`m`

is the number of letters in the alphabet (26 for English).

## Decryption

The decryption function is:

D(x) = a

^{-1}* (x - b) mod mWhere:

`a`

is the modular multiplicative inverse of^{-1}`a`

mod`m`

.

## Example

For an affine cipher with `a = 5`

, `b = 8`

, and using the English alphabet (where `m = 26`

):

### Encryption

Encrypt the letter **A** (where `x = 0`

):

E(0) = (5 * 0 + 8) mod 26 = 8 (I)

So, **A** is encrypted as **I**.

### Decryption

Decrypt the letter **I** (where `x = 8`

):

First, find `a`

for ^{-1} mod 26`a = 5`

, which is `a`

. Then:^{-1} = 21

D(8) = 21 * (8 - 8) mod 26 = 0 (A)

So, **I** is decrypted back to **A**.