C.2 Examples for Chapter 2 – Introduction to Cryptography with Coding Theory, 3rd Edition

C.2 Examples for Chapter 2

Example 1

A shift cipher was used to obtain the ciphertext kddkmu.

Decrypt it by trying all possibilities.

>> allshift(’kddkmu’)


kddkmu
leelnv
mffmow
nggnpx
ohhoqy
piiprz
qjjqsa
rkkrtb
sllsuc
tmmtvd
unnuwe
voovxf
wppwyg
xqqxzh
yrryai
zsszbj
attack
buubdl
cvvcem
dwwdfn
exxego
fyyfhp
gzzgiq
haahjr
ibbiks
jccjlt

As you can see, attack is the only word that occurs on this list, so that was the plaintext.

Example 2

Encrypt the plaintext message cleopatra using the affine function 7x+8:

>> affinecrypt(’cleopatra’,7,8)

ans =


’whkcjilxi’

Example 3

The ciphertext mzdvezc was encrypted using the affine function 5x+12. Decrypt it.

SOLUTION

First, solve y5x+12(mod26) for x to obtain x51(y12). We need to find the inverse of 5(mod26):

>> powermod(5,-1,26)

ans =
   21

Therefore, x21(y12)21y1221. To change 1221 to standard form:

>> mod(-12*21,26)

ans =
   8

Therefore, the decryption function is x21y+8. To decrypt the message:

>> affinecrypt(’mzdvezc’,21,8)

ans =


’anthony’

In case you were wondering, the plaintext was encrypted as follows:

>> affinecrypt(’anthony’,5,12)

ans =


’mzdvezc’

Example 4

Here is the example of a Vigenère cipher from the text. Let’s see how to produce the data that was used in Section 2.3 to decrypt the ciphertext. In the file ciphertexts.m, the ciphertext is stored under the name vvhq. If you haven’t already done so, load the file ciphertexts.m:

>> ciphertexts

Now we can use the variable vvhq to obtain the ciphertext:

>> vvhq

vvhqwvvrhmusgjgthkihtssejchlsfcbgvwcrlryqtfsvgahwkcuhwauglqhnslrljs hbltspisprdxljsveeghlqwkasskuwepwqtwvspgoelkcqyfnsvwljsniqkgnrgybwl wgoviokhkazkqkxzgyhcecmeiujoqkwfwvefqhkijrclrlkbienqfrjljsdhgrhlsfq twlauqrhwdmwlgusgikkflryvcwvspgpmlkassjvoqxeggveyggzmljcxxljsvpaivw ikvrdrygfrjljslveggveyggeiapuuisfpbtgnwwmuczrvtwglrwugumnczvile

We now find the frequencies of the letters in the ciphertext. We use the function frequency. The frequency command was written to display automatically the letter and the count next to it. We therefore have put a semicolon at the end of the command to prevent MATLAB from displaying the count twice.

>> fr=frequency(vvhq);
 a   8
 b   5
 c  12
 d   4
 e  15
 f  10
 g  27
 h  16
 i  13
 j  14
 k  17
 l  25
 m   7
 n   7
 o   5
 p   9
 q  14
 r  17
 s  24
 t   8
 u  12
 v  22
 w  22
 x   5
 y   8
 z   5

Let’s compute the coincidences for displacements of 1, 2, 3, 4, 5, 6:

>> coinc(vvhq,1)

ans =
   14

>> coinc(vvhq,2)

ans =
   14

>> coinc(vvhq,3)

ans =
   16

>> coinc(vvhq,4)

ans =
   14

>> coinc(vvhq,5)

ans =
   24

>> coinc(vvhq,6)

ans =
   12

We conclude that the key length is probably 5. Let’s look at the 1st, 6th, 11th, ... letters (namely, the letters in positions congruent to 1 mod 5). The function choose will do this for us. The function choose(txt,m,n) extracts every letter from the string txt that has positions congruent to n mod m.

>> choose(vvhq,5,1)

ans =

vvuttcccqgcunjtpjgkuqpknjkygkkgcjfqrkqjrqudukvpkvggjjivgjgg pfncwuce

We now do a frequency count of the preceding substring. To do this, we use the frequency function and use ans as input. In MATLAB, if a command is issued without declaring a variable for the result, MATLAB will put the output in the variable ans.

>> frequency(ans);


 a   0
 b   0
 c   7
 d   1
 e   1
 f   2
 g   9
 h   0
 i   1
 j   8
 k   8
 l   0
 m   0
 n   3
 o   0
 p   4
 q   5
 r   2
 s   0
 t   3
 u   6
 v   5
 w   1
 x   0
 y   1
 z   0

To express this as a vector of frequencies, we use the vigvec function. The vigvec function will not only display the frequency counts just shown, but will return a vector that contains the frequencies. In the following output, we have suppressed the table of frequency counts since they appear above and have reported the results in the short format.

>> vigvec(vvhq,5,1)

ans =
   0
   0
   0.1045
   0.0149
   0.0149
   0.0299
   0.1343
   0
   0.0149
   0.1194
   0.1194
   0
   0
   0.0448
   0
   0.0597
   0.0746
   0.0299
   0
   0.0448
   0.0896
   0.0746
   0.0149
   0
   0.0149
   0

(If we are working in rational format, these numbers are displayed as rationals.) The dot products of this vector with the displacements of the alphabet frequency vector are computed as follows:

>> corr(ans)

ans =
   0.0250
   0.0391
   0.0713
   0.0388
   0.0275
   0.0380
   0.0512
   0.0301
   0.0325
   0.0430
   0.0338
   0.0299
   0.0343
   0.0446
   0.0356
   0.0402
   0.0434
   0.0502
   0.0392
   0.0296
   0.0326
   0.0392
   0.0366
   0.0316
   0.0488
   0.0349

The third entry is the maximum, but sometimes the largest entry is hard to locate. One way to find it is

>> max(ans)

ans =
   0.0713

Now it is easy to look through the list and find this number (it usually occurs only once). Since it occurs in the third position, the first shift for this Vigenère cipher is by 2, corresponding to the letter c. A procedure similar to the one just used (using vigvec(vvhq, 5,2), . . . , vigvec(vvhq,5,5)) shows that the other shifts are probably 14, 3, 4, 18. Let’s check that we have the correct key by decrypting.

>> vigenere(vvhq,-[2,14,3,4,18])

ans =

themethodusedforthepreparationandreadingofcodemessagesissimpleinthe extremeandatthesametimeimpossibleoftranslationunlessthekeyisknownth eeasewithwhichthekeymaybechangedisanotherpointinfavoroftheadoptiono fthiscodebythosedesiringtotransmitimportantmessageswithouttheslight estdangeroftheirmessagesbeingreadbypoliticalorbusinessrivalsetc

For the record, the plaintext was originally encrypted by the command

>> vigenere(ans,[2,14,3,4,18])

ans =

vvhqwvvrhmusgjgthkihtssejchlsfcbgvwcrlryqtfsvgahwkcuhwauglqhnslrljs hbltspisprdxljsveeghlqwkasskuwepwqtwvspgoelkcqyfnsvwljsniqkgnrgybwl wgoviokhkazkqkxzgyhcecmeiujoqkwfwvefqhkijrclrlkbienqfrjljsdhgrhlsfq twlauqrhwdmwlgusgikkflryvcwvspgpmlkassjvoqxeggveyggzmljcxxljsvpaivw ikvrdrygfrjljslveggveyggeiapuuisfpbtgnwwmuczrvtwglrwugumnczvile