## RCM3720 Cryptography, Network and Computer Security

### Laboratory Class 8: Modes of Encryption

We will investigate the different modes of encryption using the Hill (matrix) cryptosystem. Start off by entering some matrices:

- M:=matrix([[15,9,21],[2,10,7],[16,11,12]])::Matrix ZMOD 26
- MI:=matrix([[7,17,19],[24,0,23],[12,25,10]])::Matrix ZMOD 26

Check that you've entered everything correctly with

- M*MI

Note that because the matrices were defined in terms of numbers mod 26, their product is automatically reduced mod 26. Now enter the following column vector:

- zero31:=matrix([[0],[0],[0]])::Matrix ZMOD 26

For this lab, rather than fiddling about with translations between letters and numbers, all our work will be done with numbers alone (in the range 0..25).
ECB
For electronic codebook mode, encryption is performed by multiplying each plaintext block by the matrix, and decryption by multiplying each ciphertext block by the inverse matrix:

                      -1
        C =M.P ,  P =M  C
         i    i    i     i

where all arithmetic is performed mod 26.

- Start by entering a plaintext, which will be a list of column vectors:
  - P:=[matrix([[3*i],[3*i+1],[3*i+2]]) for i in 0..7]
- and a list which will receive the ciphertext:
  - C:=[zero31 for i in 1..8]
- and encrypt it:
  - for i in 1..8 repeat C.i:=M*P.i
- Now decrypt (first make an empty list `D`):
  - D:=[zero31 for i in 1..8]
  - for i in 1..8 repeat D.i:=MI*C.i
- If all has worked out, the list `D` should be the same plaintext you obtained earlier.
- Now change one value in the plaintext:
  - Q:=P
  - Q.3:=matrix([[6],[19],[8]])
- Now encrypt the new plaintext `Q` to a ciphertext `E`. How does this ciphertext differ from the ciphertext `C` obtained from `P`?
- Check that you can decrypt `E` to obtain `Q`.

CBC
For cipherblock chaining mode, the encryption formula for the Hill cryptosystem is

       C =M(P +C   )
        i    i  i-1

and decryption is

           -1
       P =M  C -C
        i     i  i-1

- To enable us to use these formulas, we shall first add an extra column to the front of `P` and `C`:
  - P:=append([zero31],P)
  - C:=append([zero31],C)
- And we need to create a initialization vector:
  - IV:=matrix([[random(26)] for i in 1..3])
- Now for encryption:
  - C.1:=IV
  - for i in 2..9 repeat C.i:=M*(P.i+C.(i-1))
- Let's try to decrypt the ciphertext, using the CBC formula:
  - D:=[zero31 for i in 1..9]
  - for i in 2..9 repeat D.i:=MI*(C.i)-C.(i-1)
- Did it work out?
- As before, change one value in the plaintext:
  - Q:=P
  - Q.4:=matrix([[6],[19],[8]])
- Now encrypt `Q` to `E` following the procedure outlined above. Compare `E` with `C`--- how much difference is there? How does this difference compare with the differences of ciphertexts obtained with ECB?
- Just to make sure you can do it, decrypt `E` and make sure you end up with a list equal to `Q`.

OFB
Output feedback mode works by creating a key stream, and then adding it to the plaintext to obtain the ciphertext. With the Hill system, and an initialization vector `IV`:

       k =IV,   k =Mk
        1        i   i-1

and then

       c =p +k
        i  i  i

- First, the key stream:
  - K:=[zero31 for i in 1..9]
  - K.1:=IV
  - for i in 2..9 repeat K.i:=M*K.(i-1)
- and next the encryption:
  - for i in 2..9 repeat C.i:=K.i+P.i
- What is the formula for decryption? Apply it to your ciphertext `C`.