canada-dark-flag

Privacy Canada is community-supported. We may earn a commission when make a purchase through one of our links. Learn more.

Affine Cipher

author-image
Ludovic Rembert
Last Updated on June 18, 2021
lock icon

Ciphers are used in a variety of technical applications with varying degrees of complexity. The more complex the cipher, the more secure the stored value will be.

The affine cipher is one of the wider-known, lesser-complex ciphers that provide a good introduction to monoalphabetic substitutions. In other words, the affine cipher can teach those interested in how substitution ciphers work.

Through an algorithm, substitution ciphers replace characters with a new “ciphered” character to disguise the original message. As the algorithm is a basic mathematical formula, the actual encryption is quite weak and is easy to decipher.

The Algorithm


The affine cipher uses a basic formula of (ax + b) modM to encrypt a message. Here, x is the original numeric value of the character, b is the desired shift, and M is the length of the standard English alphabet which equates to 26.

While the full English alphabet of 26 characters is most often used, it may not always be used in full. For example, if you wanted to use half of the alphabet, 13 characters, you could, and you’d use (ax + b) mod 13.

For the value of a, you must choose something that is coprime with m. That means, with a value of 26 for m, you cannot choose a value like 12, as it has a common factor of 2. Rather, a value of 5 works well as it has factors in common with 26.

From the original formula (ax + b) mod26, we can find the decryption cipher as well.

a-1(x-b) modM

where a-1 is the modular multiplicative inverse of a modM.

From there, we find that:

ax= 1 (modM).

In the end, by multiplying a and x and dividing by modM, you’re left with 1. Essentially, x will always be equal to a-1.

Example

In the example, we will assume that M= 26, for the standard English alphabet, that a = 5 and that b =8.

Thus, the formula is as follows:

(5x + 8) mod26

If we want to encrypt the word “affine”, the process is as follows:

AFFINE
0558134

To start ciphering, we’ll solve the first step in the equation: (5x + 8).

AFFINE
83333487328

Next, we must solve the final part of the equation, Mod26, and turn the values back into characters

AFFINE
87722212
IHHWVC

Decrypting the Cipher


To decrypt the cipher, we will use the resultant ciphered message from above. To start, we find that a-1 = 21 and b = 8. Thus we have the following:

IHHWVC
87722212

Now, we must calculate 21(y-8), divide the answer by mod26, and find the remainder. From there, we only have to use the original table to find what value coordinates to what character.

CipherIHHWVC
y87722212
21(y-8)0-21-21294273-126
21(y-8)mod260558134
Original textAFFINE

Using The Affine Cipher


Those who want to create a basic application with encryption can easily do so with Python. Using pycipher, you can use a variety of cryptography algorithms including the Affine Cipher.

To encipher a string of text, all you need to do is run this code:

ciphertext = Affine(a,b).encipher(plaintext)    

To decipher a string, you can run this:

plaintext = Affine(a,b).decipher(ciphertext)    

Problems with Affine Cipher


problem

As briefly mentioned earlier, the affine cipher is not a secure cipher. The fact that it’s a monoalphabetic substitution algorithm means it can be cracked very easily.

When using the standard English alphabet, there are only 12 options for the variable a. Since the alphabet is 26 characters, the value of b can only be 26. Thus, there are only 312 (12 * 26) keys possible.

Additionally, if only 2 characters of the original text are known, the cipher can be cracked using a system of equations.

The Bottom Line


The Affine Cipher is a standard monoalphabetic substitution cipher. When used with the English alphabet, it is easy to cipher a piece of text. Unfortunately, as the cipher is quite basic, it’s just as easy to decipher, and it offers no real security.

If you’re interested in security, encryption, and how to keep your sensitive information safe, consider reading our list for the best VPN to protect your data. Check out our online privacy guide for further information on staying safe online.

You Might Also Like:
Related posts