Metadata
-
Date
-
Last update
-
Tagged
-
Older post
-
Newer post
Affine cipher
The affine cipher turns letters from one alphabet into letters from the same alphabet.
There are three steps to enciphering/deciphering using the affine cipher.
- Turn a letter into a number
- Do math on that number
- Turn that number into a letter
The second step is where the enciphering and deciphering are different.
Values and techniques used
Keys
The affine cipher uses 2 numerical keys.
These keys are often represented by the variables a
and b
.
Alphabet length
Letters map to a number and the other way around. For the English alphabet that means “a” is 0, “b” is 1, …, “z” is 25.
The length of that sequence is used during both enciphering and deciphering.
The alphabet length is often represented by the variable m
.
For English: m = 26
.
Key a
and alphabet length m
have to be coprime.
That means they’re integers with a greatest common divisor of 1.
This requirement exists because it is used while deciphering. A cipher that can’t ever be deciphered isn’t very useful now, is it?
Modulo
The modulus operation is used both in enciphering and deciphering.
While similar, modulus and remainder are NOT the same thing!
A modulo operation can be thought of as counting on a circle.
That circle has as many steps as the modulus you are applying.
Adding 1 to the number we take the modulo of means taking one step clockwise on the circle.
Starting at 0, and incrementing by one every step.
The result of a modulo calculation starts at 0 and increments by 1 until the maximum is reached.
Instead of incrementing that maximum, it reaches the top, and starts from 0 again.
In our example, we calculate numbers with a modulus of 26 (the length of the English alphabet). That means the maximum number is 25.
The same logic is applied for subtracting 1.
A subtraction by 1 results in 1 step counterclockwise.
mod 26
Modular multiplicative inverse
The modular multiplicative inverse is used during deciphering.
In modular arithmetic, regular division isn’t allowed.
Instead we multiply by the modular inverse.
In regular arithmetic dividing by is equivalent to multiplying by .
In modular arithmetic that inverse is dependent on which modulus is used.
For example, a modular multiplicative inverse of in is .
We want to find a number (which we’ll call ) where multiplying by that number in works out to .
- is
- is a random integer
- in
We want to find such that:
written differently:
What we just wrote, is a rearranged special case of Bézout’s identity. It says that a multiple of 7 plus a multiple of 26 is equal to the greatest common divisor of 7 and 26.
In the affine cipher, the greatest common divisor is always 1.
Written as Bezout's identity
Remember, the number we’re interested in is .
The extended Euclidean algorithm calculates the greatest common divisor of 2 numbers along with both factors of Bézout’s identity.
is one of those factors!
By plugging and into the algorithm, we get 3 things back.
- The greatest common divisor of and , that’s .
- The first integer in Bézout’s identity, that’s
- The second integer in Bézout’s identity, which is irrelevant.
The function to calculate the modular multiplicative inverse checks if the greatest common divisor between key a
, and alphabet length m
is 1.
If it is, it returns the first Bezout coefficient , which is .
Enciphering
Plaintext letter to number
A plaintext character is mapped to a number.
Our example uses the English alphabet and zero indexes letters. It maps “a” to 0, “b” to 1, …, “z” to 25.
In code, we take advantage of the ASCII table.
In ASCII, a lowercase "a"
is represented by the number 97.
As a result, a character maps to the ASCII value of that character minus 97.
Manipulate the number
- and are the key numbers.
- is the length of the used alphabet.
- is the number corresponding to the plaintext letter.
- is the number corresponding to the ciphertext letter before the modulo operation.
The formula for enciphering is .
This is done in , so in our example it’s in .
That means the final ciphertext number is .
Number to enciphered letter
Where we turned a letter into a number previously, we reverse that process.
In code, we use the ASCII table again.
This time, we add 97 to the zero indexed number and look up the resulting letter in the ASCII table.
Demo
Input area
Output area
Plaintext char as number
0
Cipher number before modulo
7
Cipher number after modulo
7
Cipher char
h
Deciphering
Enciphered letter to number
This step is identical to the first one while enciphering.
We look up the ASCII code for a lowercase letter, then we subtract the code for a lowercase ASCII "a"
.
This results in a number that is zero indexed.
Manipulate the number
- and are the key numbers.
- is the length of the used alphabet.
- is the number corresponding to the ciphertext letter.
- is the number corresponding to the plaintext letter before the modulo operation.
The formula for enciphering is .
The formula for deciphering is derived by rewriting that equation. The goal is to isolate the plaintext letter in the equation.
Rewritten with the plaintext number on the left side: The formula for deiphering is .
It is important to remember these calculation are done in .
That is why we are not allowed to divide by . Instead, we multiply by the modular multiplicative inverse of .
Number to plaintext letter
This step is identical to the last one while enciphering.
We add the number for a lowercase ASCII "a"
to our zero indexed number.
Then we look up the letter for that number in the ASCII table.
Demo
Input area
Output area
Cipher char as number
0
Modular inverse of a
21
Plain char number before modulo
-147
Plain char number after modulo
9
Plain char
j
Final code and demo
The demo and code to encipher/decipher entire strings instead of single characters is below.
Demo
Input area
enciphered text