Hill Cipher

Nerd Cafe

The Hill Cipher is a polygraphic substitution cipher that uses linear algebra to encrypt and decrypt messages. It operates on blocks of text (usually in groups of 2 or 3 characters), treating each block as a vector of numbers, and applies matrix multiplication to perform the encryption.

Here’s a step-by-step guide to understanding and using the Hill Cipher with a practical example.

Step 1: Understand the Hill Cipher Matrix

The key in the Hill cipher is a square matrix (let's call it key matrix), usually of size 2×2 or 3×3. The matrix is used for both encryption and decryption, so it needs to be invertible (its determinant should not be 0).

  • For a 2×2 matrix: The matrix will look like this:

[abcd]\begin{bmatrix} a & b \\ c & d \end{bmatrix}
  • For a 3×3 matrix: The matrix will look like this:

[abcdefghi]\begin{bmatrix} a & b & c \\ d & e & f \\ g & h & i \end{bmatrix}

Step 2: Choose a Key Matrix

For encryption, we need to choose a key matrix. Let's choose a 2×2 key matrix for simplicity.

Example key matrix 𝐾:

[3211]\begin{bmatrix} 3 & 2 \\ 1 & 1 \end{bmatrix}

Step 3: Assign Numbers to the Letters

The alphabet is mapped to numbers:

Example: "HI" (which we want to encrypt):

So the plaintext "HI" is represented as the vector:

Plaintext  Vector=[78]Plaintext\;Vector=\begin{bmatrix} 7 \\ 8 \end{bmatrix}

Step 4: Perform Matrix Multiplication

Now, the encryption process involves multiplying the key matrix 𝐾 by the plaintext vector 𝑃. The formula for encryption is:

C=K.P=[3211]×[78]=[3715]C=K.P=\begin{bmatrix} 3 & 2 \\ 1 & 1 \end{bmatrix} \times \begin{bmatrix} 7 \\ 8 \end{bmatrix}=\begin{bmatrix} 37 \\ 15 \end{bmatrix}

Step 5: Take Modulo 26

To make sure the result fits into the range of 0 to 25 (the alphabet), we take the modulo 26 of each element.

  • 37 mod 26 = 11

  • 15 mod 26 = 15

Thus, the encrypted vector is:

[1115]\begin{bmatrix} 11 \\ 15 \end{bmatrix}

Step 6: Convert the Encrypted Numbers Back to Letters

Using the same alphabet-to-number mapping, we convert the numbers back to letters:

  • 11 = L

  • 15 = P

So the ciphertext corresponding to "HI" is "LP".

Step 7: Decryption (Optional)

The formula for the inverse of a 2x2 matrix is given by:

K1=1det(K)[dbca]    mod    26K^{-1}=\frac{1}{det(K)}\begin{bmatrix} d & -b \\ -c & a \end{bmatrix}\;\;mod\;\;26

We first need to compute the determinant.

det(K)=(3×1)(2×1)=1det(K)=(3\times 1)-(2\times 1)=1

Since the determinant is 1, the inverse exists and we can proceed. Next, we apply the formula for the inverse:

K1=11[1213]    mod    26K^{-1}=\frac{1}{1}\begin{bmatrix} 1 & -2 \\ -1 & 3 \end{bmatrix}\;\;mod\;\;26

Now, we simplify the matrix modulo 26:

K1=[124253]    mod    26K^{-1}=\begin{bmatrix} 1 & 24 \\ 25 & 3 \end{bmatrix}\;\;mod\;\;26

So the inverse key matrix is:

K1=[124253]K^{-1}=\begin{bmatrix} 1 & 24 \\ 25 & 3 \end{bmatrix}

The ciphertext "LP" corresponds to the following numerical values:

  • L → 11

  • P → 15

So, the ciphertext vector 𝐶 is:

C=[1115]C=\begin{bmatrix} 11 \\ 15 \end{bmatrix}

Now, the decryption process involves multiplying the inverse key matrix 𝐾−1 by the ciphertext vector 𝐶. The formula for decryption is:

P=K1.C    mod    26P=K^{-1}.C\;\;mod\;\;26

Let's perform the matrix multiplication:

P=[124253]×[1115]=[371320]P=\begin{bmatrix} 1 & 24 \\ 25 & 3 \end{bmatrix}\times \begin{bmatrix} 11 \\ 15 \end{bmatrix}=\begin{bmatrix} 371 \\ 320 \end{bmatrix}

and so we have:

[371320]    mod    26=[78]\begin{bmatrix} 371 \\ 320 \end{bmatrix}\;\;mod\;\;26=\begin{bmatrix} 7 \\ 8 \end{bmatrix}

So, the plaintext vector 𝑃 is:

[78]\begin{bmatrix} 7 \\ 8 \end{bmatrix}

Finally, we convert the numbers back to letters:

  • 7 → H

  • 8 → I

Step 8: Python Code for Hill Cipher:

Sure! Below is a Python code implementation for the Hill Cipher encryption and decryption.

Output

Last updated