What is Affine Cipher (with Example)

Affine cipher is a monoalphabetic substitution cipher where each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter.

To put it simply, each letter of value x in a 26 letters alphabet in affine cipher is converted into another letter with the value ax + b modulo 26.

Affine cipher relies on the length of the alphabet used, modulo m. Calculation are performed on the plaintext letters to encode the message.

The difference between affine cipher and other substitution ciphers is that the encryption process is substantially mathematical.

A brief history of affine cipher:

The author and the date that this cipher was invented is not known.

Are you interested in finding out more about ciphers and codes?

The Codebreakers – The Story of Secret Writing book by David Kahn is what I would start with.

How to encrypt using Affine cipher:

First, you need to choose an alphabet and two integers (coefficients or keys), A and B.

Affine cipher example:

Plaintext: Affine Cipher.

Alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ.

Keys: A = 5, B = 8.

For each letter of the alphabet is associated with the value of its position in the alphabet, starting at 0.

This means, A = 0, B =1, and so on.

standard values for the alphabet of 26 letters

For each letter of value x of the plaintext, is associated with the value y, result of the affine function:

y = A*x+B mod 26.

affine cipher encryption

For each value y, corresponds to the letter with the same position in the alphabet, it is the encoded letter.

For example, if x is plaintext letter C, at position 2:

Where y is the result of the affine function operation:

y = 5*2+8 mod 26

y = 18.

The letter at position 18 is the ciphertext letter S.

So the plaintext letter C has been encoded to ciphertext letter S using the mathematical operation.

Thus, ciphertext: IHHWVC SWFRCP.

How to decrypt affine cipher:

First, you need to know the two keys A and B and the alphabet that was used to encode the message.

For each letter of the alphabet is associated with the value of its position in the alphabet, starting at 0.

This means, A = 0, B =1, and so on.

For each letter of value y of the message, corresponds to the value x, a result of the inverse function x= A’*(y-b) mod 26.

The value A’ is an integer such as A*A’ = 1 mod 26.

So, to find A’, calculate a modular inverse.

Here is an example:

Ciphertext: IHHWVC SWFRCP.

Keys: A =5, B =8.

A coefficient A’ for A= 5 with an alphabet size of 26 is 21 because:

5*21 = 105 = 1 mod 26.

affine cipher decryption

For example, if y is ciphertext letter S, at position 18:

Where x is the result of the reverse affine function operation:

x = 21(18-8) mod 26.

x = 2.

The letter at position 18 is the plaintext letter C.

So the ciphertext letter S has been decoded to plaintext letter C using the reverse mathematical operation.

Thus, plaintext: affine cipher.

How to recognize an affine ciphertext

A message encoded by affine has an index of coincidence close to that of the plaintext language used.

How to decipher Affine without coefficient

To break Affine, you need to brute force all A and B coefficients.

If the alphabet is 26 characters long, then A coefficient has only 12 possible values, and B has 26 values.

So only about 312 possible combinations to try out.

How to compute A’ value

Calculate the modular inverse of A, modulo the length of the alphabet.

How to calculate B’ value

B’ has the same value as B.

What are the A’ values:

The value of A’ depends on A, but also on the alphabet length.

For a classic 26 characters long alphabet the values of A’ are:

A = 1 A’ = 1
A = 3 A’ = 9
A = 5 A’ = 21
A = 7 A’ = 15
A = 9 A’ = 3
A = 11 A’ = 19
A = 15 A’ = 7
A = 17 A’ = 23
A = 19 A’ = 11
A = 21 A’ = 5
A = 23 A’ = 17
A = 25 A’ = 25

Why is there a constraint on the value of A

The Bezout’s theorem indicates that A’ only exists if A and 26 (alphabet length are co-prime.

This limits A values to 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25 (if the alphabet is 26 characters long).

Is it possible to use a key A not co-prime with 26

Yes, but an automatic decryption process becomes impossible, a single encrypted letter will have multiple plain letters possible.

Does a negative value for A exists:

Yes, but it exists a positive corresponding value, a value of A = -1 is equal to a value of A = 25 ( because 25 = -1 mod 26).

Is there a limitation on B value

No, B can take any value.

Are you interested in learning how to break codes?

The Elementary Cryptanalysis – A Mathematical Approach book by Abraham Sinkov is what I would recommend.

LEAVE A REPLY

Please enter your comment!
Please enter your name here