Affine
Affine
Cryptography Worksheet — The Affine Cipher 1
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
This is an Affine Cipher with Key Func on “ADD 3”. How else could we describe this enciphering?
Does the Affine Cipher work if we use the Key Func on “MULTIPLY BY 3” instead? Look at Example 2.
How does 11 × 3 = 7? Or 14 × 3 = 16?
HINT: When telling the me, what is 10am + 4hours? How does this relate to our situa on?
Complete the enciphering for Example 2.
We have used a key to add and a key to mul ply. Can you use a key to do both?
Encipher the plaintext “Cryptography” using the Key Func on “MULTIPLY BY 3 then ADD 4”.
crypto.interac ve‐maths.com
Cryptography Worksheet — The Affine Cipher 2
We have seen the use of a key when using the Affine Cipher, and we know that we can either add, mul ‐
ply or do both in our func on. Here is how we write the Key Func on for “MULTIPLY BY 9 then ADD 7”.
C = (9P + 7) mod 26 where P = plaintext number and C = ciphertext number
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
7 16 25 8 17 0 9 18 1 10 19 2 11 20 3 12 21 4 13 22 5 14 23 6 15 24
H Q Z I R A J S B K T C L U D M V E N W F O X G P Y
Using the Key C = (7P + 3) mod 26, encipher the plaintext:
“Success is a journey, not a des na on”
Using the Key C = 4P mod 26, encipher the plaintext:
“Never say never”
What is the problem with this key? Can you explain why?
Find all possible keys. What are the requirements for a key to work?
crypto.interac ve‐maths.com
Cryptography Worksheet — The Affine Cipher 3
We now know how to encrypt a message using an Affine Cipher. However, it is just as important that we
can decrypt the message at the other end, otherwise the person we are sending the message to will not
be able to read it. We have received the ciphertext “E” from a friend, and we know that the key used was
C = (3P + 1) mod 26. What do we need to work out?
If the ciphertext is “E”, what is C equal to?
How do we solve this equa on (remember that we are working mod 26)?
Why did we mul ply by 9 instead of dividing by 3?
What would we need to mul ply by if the key was 5P instead of 3P?
The following message was encoded using the key C = (3P + 4) mod 26. Decode it.
(NOTE: it has been split into blocks of 5 le ers to make it easier to read)
URQDC RWJUD MLQJZ QOURQ DCRWJ UDMLQ JZQOU RQDCR WJUTC RNDML QURQD
CRWJU HDCRW DMLQE LLERN CRNCR JZQNE DIRQG GHCRN JZQO
Write a message of your own, and give the ciphertext and the key to a friend to decipher.
crypto.interac ve‐maths.com
Teacher’s Notes — The Affine Cipher
Worksheet 1
The Affine Cipher is a more complex cipher that uses the Mathema cs of func ons to encipher the
plaintext. First explain that the le ers of the alphabet must be converted into numbers, and ask the pu‐
pils for any ideas of how to do this. Note that we start at 0 (not 1), and explain that this is just conven on
as it makes the Maths slightly easier to do (we will be working modulo 26). Show the table on the board,
and go through Example 1 on the board, first conver ng the le ers to their respec ve numbers, then per‐
forming the func on on these numbers, and finally conver ng the numbers back to le ers. Ask if anyone
recognises this enciphering (it is a Caesar Shi of 3).
Ask them in pairs to look at Example 2, and to work out how 11 × 3 = 7 (11 × 3 = 33 mod 26 = 7, it is the
remainder when 33 is divided by 26). For those that are struggling, use Clock Arithme c to help.
The solu on to Example 2 is 7,16,16,4,6,0,25,12,15,8,7,7,20, with a ciphertext HQQEGAZMPIHHY.
Worksheet 2
It is recommended that you spend a li le bit of me going through some modulo arithme c before
star ng this sheet.
Ask the pupils how you could write a formula for C in terms of P given that you need to mul ply P by 9
then add 7. Go through the example of how to convert a plaintext le er to its ciphertext le er, and em‐
phasise that for longer messages, it is usually quicker to first calculate the whole alphabet in table form,
and then perform the subs tu ons. To work out the values for the table, ask them if they can spot any
pa erns in the way they increase (it is adding 9 each me, but working modulo 26, so 25 + 9 = 8). For
higher ability sets, this could be linked to arithme c sequences.
The table for the key C = (7P + 3) mod 26 is shown below, along with the ciphertext.
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
3 10 17 24 5 12 19 0 7 14 21 2 9 16 23 4 11 18 25 6 13 20 1 8 15 22
D K R Y F M T A H O V C J Q X E L S Z G N U B I P W
ZNRRFZZHZDOXNSQFPQXGDYFZGHQDGHXQ.
crypto.interac ve‐maths.com
Teacher’s Notes — The Affine Cipher
Worksheet 2 (cont)
The table for the second encryp on is below.
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
0 4 8 12 16 20 24 2 6 10 14 18 22 0 4 8 12 16 20 24 2 6 10 14 18 22
A E I M Q U Y C G K O S W A E I M Q U Y C G K O S W
This causes problems because more than one plaintext le er are enciphered to the same ciphertext
le er. This is not a problem in encryp ng, but when it comes to decryp ng this message, we won’t know
whether an “A” is meant to be and “a” or an “n”, for example. This is because the greatest common divi‐
sor of 4 and 26 is not 1 (i.e. they have a common factor, namely 2). If you wish to explain in more detail to
a top set then you can briefly men on Group Theory, and it is because 4 has no mul plica ve inverse in
the group of integers mod 26 under mul plica on.
Any number between 0 and 25 that does not share any factors with 26 will make a good mul plica ve
element to the key (NOTE that mul plying by 30 is the same as mul plying by 30 ‐ 26 = 4). Then we can
add on any number between 0 and 25, since they do not affect whether the key works or not (once again
note that adding on 30 is the same as adding on 4). Also, note that subtrac ng 2 is the same as adding 24.
So, in total there are 12 numbers in the range that work as keys (the prime factors of 26 are 2 and 13, so
the mul ples of these in the range will not work as keys), and for each of these there are 26 op ons for
the addi ve element. So there are 12 × 26 = 312 possible keys, of which 26 are the trivial Caesar Shi s.
This ac vity has great poten al
Plaintext Plaintext Num‐ Ciphertext
for the pupils to use a Spread‐ Func on Ciphertext Le er
Le er ber Number
sheet to work out the ciphertext
A =CODE(A2)‐65 =(9*B2)+7 =MOD(C2,26) =CHAR(D2+65)
numbers using the formula. For B =CODE(A3)‐65 =(9*B3)+7 =MOD(C3,26) =CHAR(D3+65)
the Key C = (7P + 3) mod 26, type C =CODE(A4)‐65 =(9*B4)+7 =MOD(C4,26) =CHAR(D4+65)
in what is shown on the right D =CODE(A5)‐65 =(9*B5)+7 =MOD(C5,26) =CHAR(D5+65)
(note—to show formulae in Excel E =CODE(A6)‐65 =(9*B6)+7 =MOD(C6,26) =CHAR(D6+65)
press CTRL + `).
Plaintext Plaintext Ciphertext Ciphertext
Func on
Le er Number Number Le er
A 0 7 7 H
And get as an output what is on the le .
B 1 16 16 Q
C 2 25 25 Z
D 3 34 8 I
E 4 43 17 R
crypto.interac ve‐maths.com
Teacher’s Notes — The Affine Cipher
Worksheet 3
Emphasise that for a cipher to be of any use, then it needs to be easy to encrypt a message, but also easy
to decrypt the message, if you know the key. It should also be hard to decrypt the message for people
who do not have the key used. What we need to do, is solve the equa on for P, since we know that C = 5.
Once the pupils have come up with
5 = (3P + 1) mod 26
Ask them what you need to do to get P by itself, to which they should respond with “subtract 1” (NOTE—
the brackets do not serve their usual purpose here, but indicate we are working modulo 26, which applies
to both sides of the equa on):
4 = (3P) mod 26
Ask them what the next step is, to which the response will come “divide by 3”. Ask if we can get 1P by
mul plying by a whole number (remind them we are working modulo 26). If none of the class can see it,
assert that we want to “mul ply by 9”, and ask them why we would want to do this. This is because
9 × 3 = 27 = 1 mod 26 (or in Group Theore c terms, 9 is the mul plica ve inverse of 3 in the integers
modulo 26).
36 = 27P mod 26
Once again, note that we are working modulo 26, and so we must divide each number by 26, and take the
remainder, to get:
10 = P mod 26
So the plaintext le er is “k”.
For the higher ability, it is probably be er to talk about this in terms of rearranging the formula, so that
when it comes to using the decryp on process for many le ers, they can use the formula instead of hav‐
ing to do this each me. In that case, you should get 9 × (C ‐ 1) = P mod 26.
In the case when it is 5P, you would need to mul ply by 21 to get 105, which is one more than 104, a mul‐
ple of 26.
Make a note of the fact that the long message has been split into blocks of 5 le ers of ciphertext, some‐
thing used regularly in cryptography. It is partly to do with transmi ng messages, and partly to do with
making the ciphertext more easily diges ble for the receiver. Importantly, the spaces are not where the
spaces are in the original message.
The message decodes as:
“One ring to rule them all, One ring to find them, One ring to bring them all
and in the darkness bind them”
crypto.interac ve‐maths.com