# What is an Affine Cipher ?

What is an Affine Cipher?

According to Wikipedia: "The affine cipher is a type of monoalphabetic substitution cipher, wherein each letter of an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter, and back again, meaning the cipher is essentially a standard substitution cipher with a rule governing which letter goes to which. As such, it has the weaknesses of all substitution ciphers. Each letter is enciphered with the function (ax + b) mod 26, where b is the magnitude of the shift."

Encryption:
In the affine cipher, the letters of an alphabet are mapped to the integers in the range 0 to m-1.

At this point, the each letter of the given text is converted to the ciphertext letter using Modular arithmetic.

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

Where a and b are the key of the cipher and m  is the modulus of the size of the alphabet.

Decryption:
Decryption is derived from the above Encryption function.

D(x) = a-1(x-b) mod m

Where a and b are the key of the cipher and  is the modulus of the size of the alphabet.
a-1: modular multiplicative inverse of a modulo m. It Satisfies the equation
1= aa-1 mod m

The multiplicative inverse of a only exists if a and m are coprime.

Example: I have written a program using an example where the input string is:
"This is the input that will be encrypted and decrypted using Affine Cipher". Currently this program takes a sentence and returns in all uppercase letters after encryption and decryption.

Code Snippet:
Input: This is the input that will be encrypted and decrypted using Affine Cipher.
Output: THIS IS THE INPUT THAT WILL BE ENCRYPTED AND DECRYPTED USING AFFINE CIPHER

Have any questions or suggestions ? Lets discuss in the comments.