# B.3 Examples for Chapter 2

# Example 1

A shift cipher was used to obtain the ciphertext `kddkmu`

. Decrypt it by trying all possibilities.

`> 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\text{:}$

`> affinecrypt("cleopatra", 7, 8)`

`"whkcjilxi"`

# Example 3

The ciphertext `mzdvezc`

was encrypted using the affine function $5x+12\text{.}$ Decrypt it.

SOLUTION

First, solve $y\equiv 5x+12\text{\hspace{0.17em}}(\text{mod}\text{\hspace{0.17em}}26)$ for $x$ to obtain $x\equiv {5}^{-1}(y-12)\text{.}$ We need to find the inverse of $5\text{\hspace{0.17em}}(\text{mod}\text{\hspace{0.17em}}26)\text{:}$

`$>5\&^\text{\hspace{1em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\left(-1\right)\text{\hspace{0.17em}}\mathrm{mod}\text{\hspace{0.17em}}26$`

`21`

(On some computers, the $\stackrel{\mathbf{\u02c6}}{\phantom{\mathbf{a}}}$ doesn’t work. Instead, type a backslash $\mathrm{\setminus}$ and then $\stackrel{\mathbf{\u02c6}}{\phantom{\mathbf{a}}}\text{.}$ Use the right arrow key to escape from the exponent before typing `mod`

. For some reason, a space is needed before a parenthesis in an exponent.)

Therefore, $x\equiv 21(y-12)\equiv 21y-12\cdot 21\text{.}$ To change $-12\cdot 21$ to standard form:

`$>-12*21\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\mathrm{mod}\text{\hspace{0.17em}}26$`

`8`

Therefore, the decryption function is $x\equiv 21y+8\text{.}$ To decrypt the message:

`> affinecrypt("mzdvezc", 21, 8)`

`"anthony"`

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

`> affinecrypt("anthony", 5, 12)`

`"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`

.

`> vvhq`

`vvhqwvvrhmusgjgthkihtssejchlsfcbgvwcrlryqtfsvgahwkcuhwauglqhnslrljs`

`hbltspisprdxljsveeghlqwkasskuwepwqtwvspgoelkcqyfnsvwljsniqkgnrgybwl`

`wgoviokhkazkqkxzgyhcecmeiujoqkwfwvefqhkijrclrlkbienqfrjljsdhgrhlsfq`

`twlauqrhwdmwlgusgikkflryvcwvspgpmlkassjvoqxeggveyggzmljcxxljsvpaivw`

`ikvrdrygfrjljslveggveyggeiapuuisfpbtgnwwmuczrvtwglrwugumnczvile`

Find the frequencies of the letters in the ciphertext:

`> frequency(vvhq)`

`[ 8, 5, 12, 4, 15, 10, 27, 16, 13, 14, 17, 25, 7, 7, 5, 9, 14, 17,`

`24, 8, 12, 22, 22, 5, 8, 5]`

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

`> coinc(vvhq,1)`

`14`

`> coinc(vvhq,2)`

`14`

`> coinc(vvhq,3)`

`16`

`> coinc(vvhq,4)`

`14`

`> coinc(vvhq,5)`

`24`

`> coinc(vvhq,6)`

`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):

`> choose(vvhq, 5, 1)`

`"vvuttcccqgcunjtpjgkuqpknjkygkkgcjfqrkqjrqudukvpkvggjjivgjggpfncwuce"`

`> frequency(%)`

`[0, 0, 7, 1, 1, 2, 9, 0, 1, 8, 8, 0, 0, 3, 0, 4, 5, 2, 0, 3, 6, 5, 1, 0, 1, 0]`

To express this as a vector of frequencies:

`> vigvec(vvhq, 5, 1)`

`[0., 0., .1044776119, .01492537313, .01492537313,`

`.02985074627, .1343283582, 0., .01492537313, .1194029851,`

`.1194029851, 0., 0., .04477611940, 0., .05970149254,`

`.07462686567, .02985074627, 0., .04477611940, .08955223881,`

`.07462686567, .01492537313, 0., .01492537313, 0.]`

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

`> corr(%)`

`.02501492539, .03910447762, .07132835821, .03882089552,`

`.02749253732, .03801492538, .05120895523, .03014925374,`

`.03247761194, .04302985074, .03377611940, .02985074628,`

`.03426865672, .04456716420, .03555223882, .04022388058,`

`.04343283582, .05017910450, .03917910447, .02958208957,`

`.03262686569, .03917910448, .03655223881, .03161194031,`

`.04883582088, .03494029848`

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

`> max(%)`

`.07132835821`

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\text{.}$ 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])`

`themethodusedforthepreparationandreadingofcodemessagesissimpleinthe`

`extremeandatthesametimeimpossibleoftranslationunlessthekeyisknownth`

`eeasewithwhichthekeymaybechangedisanotherpointinfavoroftheadoptiono`

`fthiscodebythosedesiringtotransmitimportantmessageswithouttheslight`

`estdangeroftheirmessagesbeingreadbypoliticalorbusinessrivalsetc`

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

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

`vvhqwvvrhmusgjgthkihtssejchlsfcbgvwcrlryqtfsvgahwkcuhwauglqhnslrljs`

`hbltspisprdxljsveeghlqwkasskuwepwqtwvspgoelkcqyfnsvwljsniqkgnrgybwl`

`wgoviokhkazkqkxzgyhcecmeiujoqkwfwvefqhkijrclrlkbienqfrjljsdhgrhlsfq`

`twlauqrhwdmwlgusgikkflryvcwvspgpmlkassjvoqxeggveyggzmljcxxljsvpaivw`

`ikvrdrygfrjljslveggveyggeiapuuisfpbtgnwwmuczrvtwglrwugumnczvile`