Ciphers and Such...

April 2021

The Affine Cipher

Wikipedia | Try it online!

The Affine cipher is also a monoalphabetic substitution cipher, but a more generalized one. The cipher takes two keys, the first of which must be co-prime with the size of your alphabet (have no common divisors besides 1) and a message. It encrypts the message by taking the numeric value of each letter (0-25, not 1-26, at least for the English alphabet) and applying a short algebraic function to it:

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

Where:

NOTE: mod refers to the ‘modulus’ operator, which in many programming languages is represented by the percent sign (%). This operator works by returning the remainder of a division operation rather than the quotient.

NOTE 2: a and m must be co-prime because non-co-prime values there lead to encrypted text that can have multiple decrypted possibilities, which is obviously not desirable.

In fact, you could define the Caesar cipher as an Affine cipher where a = 1, since a = 1 translates to a linear shift of letters.

So, let’s take github as an example again. Using the Affine cipher, and keys 5 and 8, our encrypted message would be mwzren.

Let’s break that down:

  1. Take a letter, our first letter again being g.
  2. Convert the letter to its numeric equivalent, starting the count from 0 (like in an array). Numeric equivalent of g is 6.
  3. Now we apply our encryption function:
    1. (5*6 + 8) = 38
    2. 38 mod 26 = 12
  4. Now, we simply convert our encrypted numeric value back to a letter. In this case, the numeric value is 12, which, when converted back to a letter, becomes m.
  5. Repeat for the rest of your message.

Again, as this is a monoalphabetic substitution cipher, it is very weak and is vulnerable to nearly all methods of forceful decryption such as frequency analysis, brute force, and even guessing. Mathematically, there are 12 numbers co-prime with 26 that are less than 26 (a values) as well as 26 possible values for each value of a (b values). Therefore there are 312 (12 * 26) possible encryptions using the Affine cipher. However, this number also includes trivial solutions that the Caesar cipher could also produce (where a = 1). Without the trivial solutions, this number drops to 286 unique encryptions, which is very low and very vulnerable.

My program takes 2 integers (the 2 keys) and a string (the message), checks if the first key is co-prime with m, and then encrypts using the encryption function after converting each letter to a number. It then converts each number to a character and adds it to an empty string initialized when we called the affine function. After converting the entire message it returns and prints the string. Again, for simplicity’s sake, this program keeps spaces and punctuation.

Let’s break that down, because, as always, the code is much more involved than the actual cipher:

  1. Get the message and the keys.
  2. If the a (key1) is co-prime with m (26), continue. If not get new keys from the user.
  3. Now, take a character from your message. If the character is a letter, continue. Otherwise, skip to step 4.
    1. Convert the character to its ASCII value and subtract 97 from it to get its 0 - 25 value.
    2. Apply the encryption function to the character value to get its encrypted character value.
    3. Add 97 back to the encrypted character value to bring it back into the ASCII letter value range, and convert it back into a character.
  4. Add the character to the encrypted message string (initialized as an empty string just before step 3).
  5. Repeat for the rest of the message.

The decryption function for this cipher isn’t done yet but it’s also a mathematical function, so it should be trivial to program (famous last words?). In fact, it’s almost the same function as the encryption function:

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

Full explanation including the decryption function coming soon!

More code can be found on this project's Github repo.

-js