## Affine Cipher

The Affine Cipher is another example of a Monoalphabetic Substituiton cipher. It is slightly different to the other examples encountered here, since the encryption process is substantially mathematical. The whole process relies on working

*modulo**m*(the length of the alphabet used). By performing a calculation on the plaintext letters, we encipher the plaintext.

**Encryption**The first step in the encryption process is to transform each of the letters in the plaintext alphabet to the corresponding integer in the range 0 to

*m*-1. With this done, the encryption process for each letter is given by

**E(**

*x*) = (*ax*+*b*) mod*m*where

*a*and*b*are the key for the cipher. This means that we multiply our integer value for the plaintext letter by*a*, and then add*b*to the result. Finally, we take this modulus*m*(that is we take the remainder when the solution is divided by*m*, or we take away the length of the alphabet until we get a number less than this length).As an example, let us encrypt the plaintext "affine cipher", using the key

*a*= 5,*b*= 8. Firstly we must find the integer value of each of the letters in the plaintext alphabet (the standard alphabet of 26 letters in this case). The table below gives these values.With the integer values of the plaintext letters found, the next step is to perform the calculations on those values. In this instance, the calculation needed is (5x+8). Finally, we must ensure that all our answers are calculated mod 26 and convert the integers back to ciphertext lettes. All this informaion is shown in the table below.

Thus the ciphertext produced is "IHHWVC SWFRCP".

**Decryption**In deciphering the ciphertext, we must perform the opposite (or inverse) functions on the ciphertext to retrieve the plaintext. Once again, the first step is to convert each of the ciphertext letters into their integer values. We must now perform the following calculation on each integer

**D(**(*x*) =*c**x*-*b*) mod*m*where

*c*is the modular multiplicative inverse of*a*. That is,*a*x*c*= 1 mod*m*(*c*is the number such that when you multiply a by it, and keep taking away the length of the alphabet, you get to 1).Continuing our example, we shall decrypt the ciphertext "IHHWVC SWFRCP", using a key of

*a*= 5,*b*= 8. The first step here is to find the inverse of*a*, which in this case is 21 (since 21 x 5 = 105 = 1 mod 26, as 26 x 4 = 104, and 105 - 104 = 1). We must now perform the inverse calculations on the integer values of the ciphertext. In this case the calculation in 21(*y*- 8). Once again, we must take these answers modulo 26, and finally convert the integers back to plaintext letters. This is shown in the table below.We retrieve our plaintext of "affine cipher".

**Discussion**Possibly a simpler way for encrypting and decrypting, especially for large messages, is to create the ciphertext alphabet using the encryption process on each letter of the plaintext alphabet. This can then be used to encrypt (or decrypt) each individual letter of the plaintext. The table below shows this for the example considered above.

One of the peculiarities of the Affine Cipher is the fact that not all keys will work. Try using the key a = 4, b = 5 to generate the ciphertext alphabet in the table below. You can check the answers you get.

**What is the problem with using the key a = 4 and b = 5?**

This key creates the situation where more than one plaintext letter is encrypted to the same ciphertext letter (for example, above both "e" and "r" encipher to "V"). This means that when it comes to decrypting, the recipient will be unable to know which one of the plaintext letters has been used. Clearly this is a huge problem in using the Affine Cipher, and it is essential for the key to be chosen carefully.

**Can you find other keys that have the same problem?**

This problem occurs since the multiplicative inverse of

*a*does not exist modulo*m*. That is, there is more than one number that can be multiplied by 4 to get 1 modulo 26.There is a quick way to check if an inverse exists for a given

*m*and*a*(relying on an advanced mathematics topic called group theory). The inverse of a modulo*m*exists if and only if*a*and*m*are coprime (that is they have no common factor other than 1). Hence,*a*= 4 does not work when*m*= 26, as they have a common factor of 2 (2 goes in to both 4 and 26), but*a*= 5 does work, since 5 and 26 are coprime.We can also calculate all the possible keys for the Affine Cipher. Since, for the standard alphabet, there are 12 numbers less than 26 which are coprime to 26, and for each of these there are 26 possibilities for the value of b, we have a total of 12 x 26 = 312 possible keys for the Affine Cipher. Due to this relatively low number of possible keys (we shall compare this with more secure ciphers later), the Affine Cipher is once again susceptible to a Brute Force Attack, especially in the age of computers, and is hence not a particularly secure cipher.

Given this, we can also make the cipher a bit more secure by choosing an alphabet with a prime number of elements (since then all the numbers less than our prime are coprime to it, by definition). Thus, with our alphabet of 31 elements (the 26 letters, space and 4 punctuation marks), we have 30 possible values for

*a*, and still 26 values for*b*each time, and hence there are 30 x 26 = 780 possible keys for this alphabet. Although this is significantly more secure keys than the standard alphabet, with computing power we can still perform a brute force attack (trying every possible key) within a few minutes.One other way to adapt the Affine Cipher quite simply is to change the numbering of the letters. The simplest way to do this (and perhaps a more expected way) is to label "a" as 1, "b" as 2 and so on, as in the table below.

Using this will clearly give different results compared to the normal way of assigning values to letters, but the process is identical.