Cipher Exercises - Material - Cipher-Exercises-Material
Cipher Exercises - Material - Cipher-Exercises-Material
(b) Use a key N to shift “CAESAR”. What key is need to shift back?
What do you notice? Is this true for all texts?
(c) How can we nd the decryption key from the encryption key
Caesar ciphers are very simple to create but are also quite easy to crack.
One method we can use to crack ciphers is called Frequency Analysis.
This is where we look at the frequency (i.e. the number of times) that each
letter appears. The most common letters in the ciphertext are related to
the most common letters in the plaintext. The most common letters in the
plaintext are likely to be the most common letters in the language.
Question 1.3.
(a) What do you think are the most common letters in English?
(b) What do you think are the least common letters in English?
Question 1.4.
(a) What do you think are the commonest two letter words?
(b) What do you think are the most common repeated letters? What
about the least common?
Question 1.5.
(b) Make you own ciphertext using the Caesar cipher. Can you crack other
people’s ciphertexts?
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
Question 2.3. Use the table below to make your own substitution ciphers.
Can you crack other people’s ciphers?
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
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
Key C O D E C O D E C O D E C
Plaintext T O B E O R NO T T O B E
Ciphertext V F R
(b) Use your own key and phrase and encrypt it using the Vignère cipher.
Question 3.2.
(a) With the Caesar and Substitution ciphers we can use frequency analy-
sis to guess some of the letters in the ciphertext. Can we use frequency
analysis with the Vignère cipher? Explain your answer using the some
of the ciphertexts that you’ve created.
Question 3.3.
(a) Earlier we used the key “KEY” to encrypt “CRYPTOGRAPHY” to
get “MVWZXMQVYZLW”. What key word would turn the ciphertext
back into the plaintext? Use Table 1 to help.
(b) Now work out the keys to reverse the other ciphertexts.
(c) [Very Hard!] Here is some text that has been encrypted:
OINCHHFIAFYASZGUIVJCRKIWLLKJ
The plaintext begins with the letters WELLDONE. Use this piece of text,
called a crib, to work out the key and the whole of the plaintext. Can
this technique still be used if the crib is not at the start of the text?
(c) Pick a public key e which does not have any factors in common (apart
from 1 of course) with the number k
You will see that there is lots of Maths involved to use this encryption.
Let us have a look at the Maths behind this encryption method.
First, a reminder about factors. Let us look at the factors of 12. A
number is a factor of 12 if it divides exactly into 12 with no remainder. So,
the factors of 12 are 1,2,3,4,6 and 12. Two things to note: (a) 1 and the
number itself are always factors and (b) factors usually occur in pairs.
Question 4.1.
You should notice that 23 has exactly two factors. A number is prime
if it has exactly two factors — so, for example, 2 and 3 are prime but 1 and
4 are not prime.
Question 4.2.
(a) nd all the prime numbers that are less than 20
(c) can we nd prime numbers bigger than 1 million? bigger than 1 billion?
a (mod n)
The word “mod” stands for modulus which is another word for remainder.
So, if we had
17 (mod 5)
this means “give me the remainder when 17 is divided by 5” and so
17 (mod 5) = 2
because
17 = (3 × 5) + 2
Some more examples for you:
Some calculators allow you to work out mod using the fractions button
a b/c . So if you type
2 9 a b/c 6 =
you get an answer 456 and the penultimate number 5 gives us the re-
mainder. This does not always work though as sometimes the fraction is
cancelled down.
18 (mod ∗ ) = 5 ∗ (mod 3) = 1
(e) Let’s encrypt 23 so we need to work out 235 (mod 91). If we work out
235 then we get 6436343 — it quite hard then to work out what the
remainder would be when we divide this by 91. Instead we can work
the power in stages and nd the remainder at each stage. So rst do
(f) Let’s now undo this encryption, so when we use the private key to
work 429 (mod 91) we should get our original number of 23. To work
429 we could use the same method as before but this would take 28
steps. However we can do it fewer steps. Since 29 = 1 + 4 + 8 + 16 we
can write:
So
44 = 256 = 74 (mod 91)
8 4 4
4 = 4 × 4 = 74 × 74 (mod 91) = 5476 = 16 (mod 91)
416 = 48 × 48 = 16 × 16 (mod 91) = 256 = 74 (mod 91)
So,
429 = 4 × 74 × 16 × 74 (mod 91)
Question 4.4.
(a) Using the same public key 5 encrypt the number 5 using RSA
(b) Encrypt 18 using the public key 3. You should nd that your nal
answer is 2.
(c) What should be the answer to 227 (mod 55)? Check that this is true.
Main> 35 + 78
113
Main> 4 * $$
452
The rst one contains all 26 letters and the second has some short words.
Caesar cipher
To shift the word “HELLO” using a key ’R’, type
This gives us a list of pairs (frequency, letter). It is not always easy to see
the results so we can use the function printPairs to print the results:
(We can change 4 to another number between 1 and 26.) Remember that
we can replace qwe by any text string in the commands above.
To nd the most frequent letter in a string, we can type:
Substitution Cipher
We can check a string is suitable as a key by using checkKey:
The rst key is suitable, the second has two few letters and the third has G
twice.
We can create a substitution key by giving the rst few letters:
Vignère
To use the Vignère with key “KEY” and plaintext “HELLO WORLD”:
Main> vig "KEY" "HELLO WORLD"
"RIJVSUYVJN"
Note that the space has been removed.
To undo the cipher
and thus
Main> vig "QWC" "RIJVSUYVJN"
"HELLOWORLD"
RSA Encryption
To do RSA encryption we need to work out an expression such as:
Main> 13^8
815730721
and then
Main> mod $$ 55
36
Main> power 13 8 55
36
Main> private 5 7 13
29
If we do not pick a good public key e then we will get an error message
Main> private 6 7 13
Program error: Not coprime
Now let’s encrypt the number 23 using the public key e = 5. We want
to work out 235 (mod 91) so we type
Main> power 23 5 91
4
Main> power 4 29 91
23
There are some sample keys which can be used. Typing in p1 and q1 will
give you two prime numbers and their product is dened to n1. A suitable
public key is e1 and the corresponding private key is d1.
Main> p1
241679
Main> q1
8123471
Main> n1
1963272347809
Main> p1 * q1
So we can try
Then to decode
There’s some larger primes p2 and q2 with public key e2 and private
key d2. An example using the larger primes and shake:
Main> unrsa $$ d2 n2
"TO BE OR NOT TO BE THAT IS THE QUESTION"
Sample texts
In the program, there are a few sample texts that you can use to try things
out. As well as qwe and shake there is the opening paragraph from a Poirot
story by Agatha Christie:
Main> poirot
"Pure chance led my friend Hercule Poirot, formerly chief of
the Belgian force, to be connected with the Styles case. His
success brought him notoriety, and he decided to devote
This means that the key is likely to have length 8, 16 or 24. Since 8 is a
factor of both 16 and 24 then the most likely key length is 8. You may also
try to nd the keys by using a crib.
There is another Vignère encrypted text:
Main> vigExample
"OVPWAAAVGVDAHEDSUHPHZIRAKYXMSLIIIMRKYGLSEDHZGOVPQEDFFPHCZQPUH
CTISQRVMGVVJBOWLQMJMJCOTYUHVLOWWOAMBULYWCHGFSUTOSLFLAHYXMGNKIE
HJMCSYUIOCXGDGTQKQOTAJHSHVFVVRNHZZIPHYXDFHDRPGSXISLWLFVVBMQWRE
WGRGYGEDVFSUPVZVLRSPIBNYWBMZHYXNOXHHUFVVOWZQ"
Question 1.2
(a) We need a shift of ‘L’
(b) After a shift of ‘N’, “CAESAR” becomes “PNRFNE”. The same key
‘N’ shifts the ciphertext back to “CAESAR”. This is true for all texts.
It is also true for the key ‘A’
(c) The encryption and decryption keys occur in pairs (apart from ‘A’ and
‘N’). The pairs are (‘B’,‘Z’), (‘C’,‘Y’) and so until (‘M’,‘O’).
Question 1.3
(a) The most common letters are E,T,A and I.
(b) The least common letters are Z, X, J and Q.
(c) This is not true for all languages.
Question 1.4
(a) Words such as “of”, “to”, “he”, “we”, “it” and “is” are common in
English.
(b) Letters such as ‘T’, ‘P’, ‘S’, ‘L’, ‘E’ and ‘O’ are commonly repeated let-
ters. But letters such as ‘Q’, ‘J’ and ”W” are not commonly repeated
in English words.
Question 1.5
(a) The plaintext decrypts to “CAESAR CIPHERS ARE QUITE EASY
TO CRACK” and the key was ‘R’. Note that ‘V’ and ‘R’ are the
most common letters in the ciphertext: ‘V’ corresponds to ‘E’ and ‘R’
corresponds to ‘A’.
A B C D E F G H I J K L M
D E C R Y P T I O N Z X W
N O P Q R S T U V W X Y Z
V U S Q M L K J H G F B A
It was created by using the word “DECRYPTION” and the rest of the
alphabet in reverse order.
(a) VCEIQFQSVHRFG
Question 3.2
(a) We cannot use frequency analysis for this cipher. Look at the encryp-
tion of TOBEORNOTTOBE in the previous question. The letter O
is encrypted to the letters C, Q, S and R. While in the ciphertext, we
have two Qs which come from the letters O and N.
(b) We can sometimes guess the length of the key by looking at letters
which are repeated in the ciphertext and nding the length of the
repeat. In the CRYPTOGRAPHY example earlier, R is encrypted to
V twice — with a repeat length of 6 — Y is encrypted to W twice —
with a repeat length of 9. So since 3 is a factor of 6 and 9 we could
guess that the key length is 3 (which it is!).
We have to be careful with this method as sometimes we can get false
matches — for example, there are two letter Ms in the ciphertext
(with a repeat length of 5) but these come from dierent letters in the
plaintext.
Question 3.3
(a) The reverse key for “KEY” is “QWC”. Using the table, for each letter
of the key you look along the row of the table for that letter. When
you nd the letter A, you look at what column you are in. So looking
at the row for ‘K’, you will see that ’A’ is in column ’Q’.
(c) You know that the rst letter in the plaintext is W, the letter in the
cipher text is O, so using the table, looking along row ‘W’ you nd ‘O’
in column ‘S’. So the rst letter of the key is ‘S’.
Now look at row ‘E’ until you nd ‘I’ - this happens in column ‘E’ —
so the second letter of the key is ‘E’. Continuing this process gets you
the characters “SECRETSE” — so it looks like the key is “SECRET”.
Using this key, the plaintext is “WELLDONEYOUHAVEDECRYPT-
EDTHIS”.
(a) 1,2,3,5,6,10,15,30
(b) 1,2,4,5,10,20,25,50,100
(c) 1,23
Question 4.2
(a) 2,3,5,7,11,13,17,19
(c) There are primes numbers that are bigger than any number you can
think of as there are innitely many primes. Whether you can actu-
ally nd such a prime is a dierent matter. Many computer scientist
regularly try to nd the next biggest prime number.
Question 4.3
For the last one, there are many dierent possible answers: 1,4,7,11 etc.
Question 4.4
Question 4.5
(a) We need 3 × 27 = 1 (mod 40) and
3 × 27 = 81 = 2 × 40 + 1 = 1 (mod 40)
(b)