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

A.3 Examples for Chapter 2

Example 1

A shift cipher was used to obtain the ciphertext kddmu. Decrypt it by trying all possibilities.

In[1]:= allshifts[”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:

In[2]:=affinecrypt["cleopatra", 7, 8]

Out[2]=whkcjilxi

Example 3

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

SOLUTION

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

In[3]:= PowerMod[5, -1, 26]

Out[3]= 21

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

In[4]:= Mod[-12*21, 26]

Out[4]= 8

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

In[5]:= affinecrypt["mzdvezc", 21, 8]

Out[5]= anthony

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

In[6]:= affinecrypt["anthony", 5, 12]

Out[6]= 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 it. For convenience, we’ve already stored the ciphertext under the name vvhq.

In[7]:= vvhq

Out[7]=

vvhqwvvrhmusgjgthkihtssejchlsfcbgvwcrlryqtfsvgahwkcuhwauglqhnslrljs hbltspisprdxljsveeghlqwkasskuwepwqtwvspgoelkcqyfnsvwljsniqkgnrgybwl wgoviokhkazkqkxzgyhcecmeiujoqkwfwvefqhkijrclrlkbienqfrjljsdhgrhlsfq twlauqrhwdmwlgusgikkflryvcwvspgpmlkassjvoqxeggveyggzmljcxxljsvpaivw ikvrdrygfrjljslveggveyggeiapuuisfpbtgnwwmuczrvtwglrwugumnczvile

Find the frequencies of the letters in the ciphertext:

In[8]:= frequency[vvhq]

Out[8]=

{{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:

In[9]:= coinc[vvhq, 1]

Out[9]= 14

In[10]:= coinc[vvhq, 2]

Out[10]= 14

In[11]:= coinc[vvhq, 3]

Out[11]= 16

In[12]:= coinc[vvhq, 4]

Out[12]= 14

In[13]:= coinc[vvhq, 5]

Out[13]= 24

In[14]:= coinc[vvhq, 6]

Out[14]= 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):

In[15]:= choose[vvhq, 5, 1]

Out[15]= vvuttcccqgcunjtpjgkuqpknjkygkkgcjfqrkqjrqudukvpkvggjjivgjggpfncwuce

In[16]:= frequency[%]

Out[16]= {{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:

In[17]:= vigvec[vvhq, 5, 1]

Out[17]= {0, 0, 0.104478, 0.0149254, 0.0149254, 0.0298507, 0.134328, 0, 0.0149254, 0.119403, 0.119403, 0, 0, 0.0447761, 0, 0.0597015, 0.0746269, 0.0298507, 0, 0.0447761, 0.0895522, 0.0746269, 0.0149254, 0, 0.0149254, 0}

The dot products of this vector with the displacements of the alphabet frequency vector are computed as follows:

In[18]:= corr[%]

Out[18]=

{0.0250149, 0.0391045, 0.0713284, 0.0388209, 0.0274925, 0.0380149, 0.051209, 0.0301493, 0.0324776, 0.0430299, 0.0337761, 0.0298507, 0.0342687, 0.0445672, 0.0355522, 0.0402239, 0.0434328, 0.0501791, 0.0391791, 0.0295821, 0.0326269, 0.0391791, 0.0365522, 0.0316119, 0.0488358, 0.0349403}

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

In[19]:= Max[%]

Out[19]= 0.0713284

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.

In[20]:= vigenere[vvhq, -{2, 14, 3, 4, 18}]

Out[20]=

themethodusedforthepreparationandreadingofcodemessagesissimpleinthe extremeandatthesametimeimpossibleoftranslationunlessthekeyisknownth eeasewithwhichthekeymaybechangedisanotherpointinfavoroftheadoptiono fthiscodebythosedesiringtotransmitimportantmessageswithouttheslight estdangeroftheirmessagesbeingreadbypoliticalorbusinessrivalsetc

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

In[21]:= vigenere[%, {2, 14, 3, 4, 18}]

Out[21]=

vvhqwvvrhmusgjgthkihtssejchlsfcbgvwcrlryqtfsvgahwkcuhwauglqhnslrljs hbltspisprdxljsveeghlqwkasskuwepwqtwvspgoelkcqyfnsvwljsniqkgnrgybwl wgoviokhkazkqkxzgyhcecmeiujoqkwfwvefqhkijrclrlkbienqfrjljsdhgrhlsfq twlauqrhwdmwlgusgikkflryvcwvspgpmlkassjvoqxeggveyggzmljcxxljsvpaivw ikvrdrygfrjljslveggveyggeiapuuisfpbtgnwwmuczrvtwglrwugumnczvile