Skip to main content
The National Cipher Challenge

Cracking an affine cipher

Those of you still stuck on Challenge 2A might like to know that Charles has encrypted it using what is known in the trade as an Affine Shift cipher. These are a bit more complicated than the classical Caesar shifts where the alphabet is just shifted along and use a bit more mathematics, but in this case they are still reasonably straightforward to crack, especially if you have a crib. Jodie has given you one in Case File 2.1, so you could head over there to see what I mean, but I thought you might like an explanation of how to use it, so here goes. (This is all explained in our codebreaking handbook, so do take a look there too.)

The first step is to understand how the cipher works. As with the Caesar shift we will work with the position of each letter in the alphabet and use a mathematical function to move them. For this news item I am going to assume A is in position 1, B is in position 2 and so on, though you could also put A in position 0, B in position 1 and so on if you prefer. The calculations change a bit, but the idea is just the same.

With the Caesar shift cipher you choose a fixed number, say 5, and move every letter on by that amount, so A moves from position 1 to position 6 (which is where F used to live) B moves to position 7 (where G used to live) and so on. With this encryption BABBAGE would shift to the string GFGGFLJ, so if you knew that a message signed by Charles was enciphered as a Caesar shift cipher then you could work out from seeing GFGGFLJ at the end that the shift used was 5, and then unshift the rest of the letters in the message (moving them back by 5) in order to decrypt it. There is a small issue with this Caesar shift which involves deciding what to do with the letters V,W,X,Y,Z which appear in positions 22, 23,24, 25 and 26, since shifting them by 5 puts them in positions 27, 28, 29, 30 and 31, and there are no letters in those positions, but we just wrap them round to positions 1,2,3 and 4 which are occupied by A, B, C and D. Nothing else has been shifted there, so that is safe. So in the Caesar shift cipher V is encrypted by A, W by B, X by C, Y by D and Z by E, and we shift them back to decipher, so wherever we see and D in the cipher text we replace it by Y for example.

Now how about the affine shift cipher? Here we do something similar, but to decide where the letter A goes, we choose two numbers, a and b, and we move A to position a+b in the alphabet, where if a+b is 26 or higher then we wrap around. So, for example if a=11 and b=18 then A maps to position 11+18 = 29, which wraps around to position 3 telling us to encrypt A as C.

What about the other letters? Well we want to use the same recipe for all of them, and we will do that by saying that the affine shift corresponding to the numbers a, b will map the letter in position x in the alphabet to the letter in position ax+b, by which we mean we will multiply the position by a and then add on b. So with a=11 and b=18 the letter B, which starts in position 2, will move to position 11*2+18 =22+18=40. Since this is bigger than 26 we need to wrap around as usual, which involved subtracting 26 to give the answer 14, so B will move to position 14 and be encrypted as N. Notice that N, which is the encryption of B is 11 steps beyond C which is the encryption of A. This is because the new position of A was 1*11+18 and the new position of B was 2*11+18. C will move to position 3*11+18, so will be a further 11 steps beyond the new position of B, ie, at position 25 represented by Y.

If you are presented with a ciphertext encrypted using an affine shift cipher then you need to work out the pair of numbers a,b used to encrypt it and a good crib is what you need to help with that. The crib Jodie has provided is perfect! We see that A has been encrypted as V and B has been encrypted by M. Following the argument in the previous paragraphs we see that a+b = 22 (V is the letter in the 22nd position in the alphabet) and 2*a+b =13 (M is the thirteenth letter in the alphabet) so their difference gives a = (2a+b)-(a+b) = 13-22 = -9, in other words the multiplication factor a in the cipher is -9.

You might find it hard to believe that you should or even could get a negative multiplier, but stick with this explanation for a minute and hopefully all will become clear.

Now we “know” what the number a is, we should be able to work out the number b. We know from the crib that a+b=22, and now we have worked out that a = -9, so it looks like -9+b=22. We solve this to get b = 31. So our affine cipher take the letter in position x to -9*x + 31. In particular A (in position 1) goes to position -9*1+31 = 22 is encrypted by V, and B (in position 2) is moved to position -9*2 + 31 = -18+31 = 13 so is encrypted by M.

What about the letter C? It start is position 3, so our rule tells us that it moves to position -9*3+31 = -27 +31 = 4. Hence C moves to position 4 and gets encrypted by D.

And now what about the letter D? It is in position 4, so moves to position -9*4+31 = -36 +31 =-5. Now this is a bit (perhaps a lot) surprising, because it isn’ obvious what position -5 could mean? But just as we could wrap position 29 to position 3 in the Caesar shift cipher by subtracting 26, we can wrap position -5 to position 21 by adding 26. We are doing what is known in the trade as modular arithmetic with modulus 26, and two numbers in this system act can be thought of as the same number provided they differ by a multiple of 26. It follows that the letter D should be encrypted as U, which is the 21st letter of the alphabet. If this is right then Ada’s name will be encrypted by the sequence VUV

Guessing that the Charles’s letter to Ada has her name near the top we can look for that sequence of letters in the text to confirm our guess! Jodie has underlined it in Case File 2.2.

To decipher the message we can now construct an encryption table using the recipe described above Letter in position x moves to position -9x+31

I hope you found this helpful in cracking the cipher in Chapter 2 part A. Part B is a different challenge entirely. There is no nice formula for the encryption used, but it is still a substitution cipher and you can use the same idea of finding a crib to help you to crack it. The most useful is to work out which of the three letter words you see in the cipher text might represent the words AND and THE. The latter is the most common three letter word in English and E and T duke it out for the title of most common letter, so you can use that to get started. The shape of the words can then help you to bootstrap the process, especially if you can start to see parts of words that you recognise.

Good luck!

Harry’s signature
Report a problem