Demystifying Affine cipher
The Affine cipher maps each letter in an alphabet to its numeric equivalent, encrypts it using a linear mathematical function, and converts it back to a letter.
The Affine cipher is a type of simple substitution cipher and as such uses a fixed ciphertext alphabet over the entire message. The ciphertext alphabet gets derived from the plaintext alphabet using an affine mathematical function (also called linear function) as the key.
Simply put, an affine function is the process of multiplying a value by a constant , then adding a constant to it.
As we want to make sure we don’t leave the scope of our alphabet with length , we apply the operation at the end (we subtract from the result until we get a value smaller than ).
Hence, an affine function can be represented as .
How does Affine cipher work?
For this example, we use the Latin alphabet A-Z with length as our plaintext alphabet and the affine function with .
First, we map each letter in the alphabet to its numeric equivalent by taking its position in the alphabet (starting with 0), like so:
Now we apply the affine function to each of the values above (modulo ):
Finally, we convert back the numbers to their letter equivalents:
This gives us the map to encrypt and decrypt any message using the Affine cipher key and the Latin alphabet A-Z.