April 2021
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:
g
.g
is 6
.(5*6 + 8) = 38
38 mod 26 = 12
12
, which, when converted back to a letter, becomes m
.
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:
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