# B.6 Examples for Chapter 6

# Example 21

The ciphertext

was encrypted using a Hill cipher with matrix

Decrypt it.

SOLUTION

There are several ways to input a matrix. One way is the following. A $2\times 2$ matrix $\phantom{\rule{negativethinmathspace}{0ex}}\left(\begin{array}{cc}\phantom{\rule{negativethinmathspace}{0ex}}a& b\phantom{\rule{negativethinmathspace}{0ex}}\\ \phantom{\rule{negativethinmathspace}{0ex}}c& d\phantom{\rule{negativethinmathspace}{0ex}}\end{array}\right)\phantom{\rule{negativethinmathspace}{0ex}}$ can be entered as `matrix(2,2,[a,b,c,d])`

. Type `evalm(M&*N)`

to multiply matrices $M$ and $N\text{.}$ Type `evalm(v&*M)`

to multiply a vector $v$ on the right by a matrix $M\text{.}$

Here is the encryption matrix.

`> M:=matrix(3,3,[1,2,3,4,5,6,7,8,10])`

We need to invert the matrix mod 26:

`> invM:=map(x->x mod 26, inverse(M))`

The command `map(x->x mod 26, E)`

takes each number in an expression `E`

and reduces it mod 26.

This is the inverse of the matrix mod 26. We can check this as follows:

`> evalm(M&*invM)`

`> map(x->x mod 26, %)`

To decrypt, we break the ciphertext into blocks of three numbers and multiply each block on the right by the inverse matrix we just calculated:

`> map(x->x mod 26, evalm([22,09,00]&*invM))`

`[14, 21, 4]`

`> map(x->x mod 26, evalm([12,03,01]&*invM))`

`[17, 19, 7]`

`> map(x->x mod 26, evalm([10,03,04]&*invM))`

`[4, 7, 8]`

`> map(x->x mod 26, evalm([08,01,17]&*invM))`

`[11, 11, 23]`

Therefore, the plaintext is 14, 21, 4, 17, 19, 7, 4, 7, 8, 11, 11, 23. Changing this back to letters, we obtain `overthehillx`

. Note that the final `x`

was appended to the plaintext in order to complete a block of three letters.