# D.4 Computations for Chapter 6

# Hill ciphers

Let’s encrypt the plaintext `This is the plaintext `

using a $3\times 3$ matrix. First, we need to specify that we are working with such a matrix with entries in the integers mod 26:

`R=IntegerModRing(26)`

`M=MatrixSpace(R,3,3)`

Now we can specify the matrix that is the encryption key:

`K=M([[1,2,3],[4,5,6],[7,8,10]]);K`

Evaluate to obtain

`[ 1 2 3]`

`[ 4 5 6]`

`[ 7 8 10]`

This is the encryption matrix. We can now encrypt:

`H=HillCryptosystem(AlphabeticStrings(),3)`

`P=H.encoding("This is the plaintext")`

`C=H.enciphering(K,P);C`

If the length of the plaintext is not a multiple of 3 (= the size of the matrix), then extra characters need to be appended to achieve this. When the above is evaluated, we obtain the ciphertext

`ZHXUMWXBJHHHLZGVPC`

Decrypt:

`H.deciphering(K,C)`

When this is evaluated, we obtain

`THISISTHEPLAINTEXT`

We could also find the inverse of the encryption matrix mod 26:

`K1=K.inverse();K1`

This evaluates to

`[ 8 16 1]`

`[ 8 21 24]`

`[ 1 24 1]`

When we evaluate

`H.enciphering(K,C):C`

we obtain

`THISISTHEPLAINTEXT`