[Math] How to find the key matrix of a 2×2 Hill Cipher

cryptographyelementary-number-theorymatricesnumber theory

In the english language, the most common digraph is TH which is then followed by HE. In this particular example let's say the digraphs with the most frequencies are RH and NI. How would I find the a, b, c, and d values for the key matrix:
\begin{pmatrix}a&b\\c&d\end{pmatrix}

We can split TH and HE into pairs \begin{pmatrix}R\\H\end {pmatrix} \begin{pmatrix}N\\I\end{pmatrix}

Which, when converted to their integer values, are equivalent to:
\begin{pmatrix}19\\07\end {pmatrix} \begin{pmatrix}07\\04\end{pmatrix}
If i'm not wrong here, I can use these values to solve for the values of a, b, c, and d. Unfortunately my use of matrix notation is limited and I fear that I would clog up the screen with my poor attempt so I'll just put the result of my work. I basically combined the key matrix of a, b, c, and d with the pairs TH and HE to get:

TH: $(19a+7b)$mod$26$ and $(19c + 7d)$mod$26$

HE: $(7a+4b)$mod$26$ and $(7c+4d)$mod$26$

Assuming this work is correct, I believe that I can just set these values equal to the values of RH and NI and solve for a, b, c, or d.

$19a + 7b = 17$ would be used to find the value of R. However, I'm not entirely sure if this is correct. I am also not entirely sure how I would proceed after creating this equation. Wouldn't I have to find the inverse instead of solving the equation?

Best Answer

You assume that $TH \to RH$ and $HE \to NI$ under the Hill cipher.

Or in matrix notation:

$$\begin{bmatrix} a & b\\ c & d\end{bmatrix}\begin{bmatrix}19\\7 \end{bmatrix}= \begin{bmatrix}17\\7 \end{bmatrix}$$

and

$$\begin{bmatrix} a & b\\ c & d\end{bmatrix}\begin{bmatrix}7\\4 \end{bmatrix}= \begin{bmatrix}13\\8 \end{bmatrix}$$

or in one matrix notation:

$$\begin{bmatrix} a & b\\ c & d\end{bmatrix} \begin{bmatrix} 19 & 7\\ 7 & 4\end{bmatrix} = \begin{bmatrix} 17 & 13\\ 7 & 8\end{bmatrix}$$

which allows us to find the encryption matrix by

$$\begin{bmatrix} a & b\\ c & d\end{bmatrix} = \begin{bmatrix} 17 & 13\\ 7 & 8\end{bmatrix} {\begin{bmatrix} 19 & 7\\ 7 & 4\end{bmatrix}}^{-1}$$

The determinant of $\begin{bmatrix} 19 & 7\\ 7 & 4\end{bmatrix}$ is $19\cdot 4 - 7\cdot 7 = 1 \pmod{26}$, so the inverse exists and equals (using $-7 = 19 \pmod{26}$)

$$\begin{bmatrix} 4 & 19\\ 19 & 19\end{bmatrix}$$

This allows us to compute the encryption matrix, and then the decryption matrix.

Alternatively, as $\begin{bmatrix} 17 & 13\\ 7 & 8\end{bmatrix}$ is also invertible (determinant $19$) we can find the decryption matrix also from (using $A = BC \to A^{-1} = C^{-1}B^{-1}$ etc.)

$${\begin{bmatrix} a & b\\ c & d\end{bmatrix}}^{-1} = \begin{bmatrix} 19 & 7\\ 7 & 4\end{bmatrix} {\begin{bmatrix} 17 & 13\\ 7 & 8\end{bmatrix}}^{-1}$$ as well