✖

Affine cipher: formula, example and calculator

The affine cipher changes every letter by a formula: it multiplies the letter’s position in the alphabet by a number a and adds a number b. These two numbers are the whole key. With a = 5 and b = 8, the word AFINNI (Czech for “affine”) becomes IHWVVW. The Caesar cipher can only add and Atbash can only reverse the alphabet, but the affine cipher does both at once.

Letters as numbers and counting in a circle

Before we start calculating, we give the letters numbers: A = 0, B = 1, C = 2 and so on up to Z = 25. Why start from zero? So that a shift by 0 means “no change”, and so that the remainders after division by 26, the numbers 0 to 25, land exactly on the letters. We work with 26 letters without Czech accents, so Č counts as C, CH is the two letters C and H (a Czech habit), and spaces are not encrypted.

Imagine the alphabet as a clock face with 26 marks. On a clock, ten hours plus five hours is three o’clock, not fifteen, because the hand passes twelve and starts again. After Z we also carry on from A. This is called counting mod 26 (read “modulo”). The number xmod26 is the remainder of x after division by 26: we subtract 26 as many times as we can and keep what is left:

33mod26=7,

because 33 = 26 + 7. In the picture we take 33 steps from A, one whole lap and seven more, and end up at H.

The alphabet in a circle like a clock face, the letters A to Z with the numbers 0 to 25. A red spiral leads from A through one whole lap to H, which has the number 7. The alphabet in a circle like a clock face, the letters A to Z with the numbers 0 to 25. A red spiral leads from A through one whole lap to H, which has the number 7.

When two numbers give the same remainder, we put ≡ between them, for example 33 ≡ 7. Similarly 73mod26=21: two 26s go into 73, making 52 altogether, and 21 is left. With negative numbers we add 26 instead, so −1mod26=25. One step back from A is Z.

Affine cipher: an encryption example

The letter with the number x is encrypted as the letter with the number

y=(a⋅x+b)mod26.

Let’s try the word AFINNI with the key a = 5, b = 8. We turn each letter into a number, multiply by five, add eight and take the remainder after division by 26:

letter x 5 · x + 8 mod 26 cipher
A 0 8 8 I
F 5 33 7 H
I 8 48 22 W
N 13 73 21 V

The same letter always gives the same result: both Ns become V and both Is become W. The result is IHWVVW.

How to decrypt

The recipient works backwards: they subtract b and should then “divide” by a. This is where it gets stuck. For H, which is 7, they get 7 − 8 = −1, and that cannot be divided by five without a remainder.

Instead of dividing, we multiply by the inverse (the modular inverse). This is the number that gives remainder 1 when multiplied by a. For a = 5 it is 21:

5⋅21=105=4⋅26+1.

Multiplying by 21 therefore undoes, mod 26, what multiplying by 5 did. You can find the inverse by trial: multiply a by 1, 2, 3 and so on and wait for remainder 1. For all usable values of a, the inverses are in the tables:

a 1 3 5 7 9 11
inverse 1 9 21 15 3 19
a 15 17 19 21 23 25
inverse 7 23 11 5 17 25

We write the inverse of a as a−1. Decryption is

x=a−1⋅(y−b)mod26.

Take H. That is 7, and we subtract 8:

7−8=−1

We multiply by the inverse, 21:

21⋅(−1)=−21

Adding 26 gives 5, which is F. For W we get 22 − 8 = 14 and 14 · 21 = 294. Divide the big number by 26 with a remainder: 294÷26=11 remainder 8 (the division with remainder calculator can help too). Eight is I. The remaining letters work the same way, and IHWVVW turns back into AFINNI.

Why not every a will do

What if a = 2 and b = 8? A is 0, so 2 · 0 + 8 = 8, which is I. N is 13 and 2 · 13 + 8 = 34, and after subtracting 26 we again get 8. Both letters are encrypted as I. A recipient who finds an I in the cipher cannot tell whether it was an A or an N.

On the left, with a = 5, b = 8, A leads to I and N leads to V. On the right, with a = 2, b = 8, both A and N lead to the same I. On the left, with a = 5, b = 8, A leads to I and N leads to V. On the right, with a = 2, b = 8, both A and N lead to the same I.

The cause is 2 · 13 = 26, which is zero mod 26. With an even a, letters that are 13 places apart therefore always merge. With a = 13 it is even worse: 13 · 2 = 26, so all the letters in even positions give one cipher letter and all those in odd positions give another.

The number 26 is 2 · 13. Trouble arises whenever a shares the factor 2 or 13 with 26. The cipher works only if a is coprime to 26, that is, if their greatest common divisor is 1. It is exactly for such values of a that an inverse exists. Why? For a coprime a, the multiples a · 0, a · 1 up to a · 25 land on all 26 remainders, each once, so also on 1. For an even a, all the multiples are even and remainder 1 never comes out. Between 1 and 25 there are twelve such values of a, exactly those in the tables: the odd numbers except 13. The shift b can be any of 26, so the number of keys is

12⋅26=312.

One of them, a = 1 and b = 0, leaves the text as it is. I wouldn’t encrypt with that one.

Caesar and Atbash are affine ciphers

The Caesar cipher is an affine cipher with a = 1: each letter is just shifted by b places. Caesar’s shift by three letters is the key a = 1, b = 3, and it turns HELLO into KHOOR.

Atbash corresponds to the key a = 25, b = 25. Multiplying by 25 is the same as multiplying by −1 mod 26, so 25 · x + 25 is the same as 25 − x. A, which is 0, changes to 25, which is Z, B changes to Y and so on. In Atbash, HELLO is SVOOL.

How to break the affine cipher

If you only want to use the cipher, you can skip this part and go straight to the calculator.

A given letter always changes into the same one, as in every substitution cipher. It is therefore given away by frequency analysis: we count which letters are the most common in the cipher and compare them with the language. In Czech, E, A, O and I lead. For the affine cipher, it is enough to guess two letters, and the rest of the key can be worked out.

Say that in a longer intercepted message the most common characters are C and V. We guess that C is E and V is N, both of which are common in Czech. E is 4 and C is 2, N is 13 and V is 21. We get a system of two equations mod 26:

4a+b≡2

13a+b≡21

With such equations about remainders, we may add, subtract and multiply as with ordinary ones, but not divide: instead of dividing, we multiply by an inverse. Subtracting the first equation from the second makes b disappear:

9a≡19

The inverse of 9 is 3 (9 · 3 = 27 = 26 + 1), so we multiply both sides by three: 19 · 3 = 57 and 57mod26=5. We get a ≡ 5. Substituting into the first equation gives b ≡ 2 − 20 = −18 ≡ 8. The key is a = 5, b = 8. If the guess had been wrong, the decrypted text would be nonsense and we would try another pair. Beware of pairs whose guessed letters differ in the alphabet by an even number or by 13: then the system does not have a single solution.

The cipher can also be broken by brute force, without any thinking. For the Caesar cipher you try 25 shifts, here 312 keys. By hand that is an afternoon’s work, while a computer has them done before you blink.

Affine cipher calculator

Type a text and both keys. The calculator removes Czech accents and writes in lower case, and leaves spaces, digits and punctuation in place. The number a must be coprime to 26, that is, odd and not 13. Try decrypting “ihwvvw” with the same key.

Where next

Counting mod 26 returns in the Vernam cipher, which, unlike the affine cipher, cannot be broken at all. The Diffie–Hellman key exchange works with counting modulo large numbers, and the inverse plays a leading role in the RSA cipher.