Hill's CipherLester S. Hill created the Hill cipher, which uses matrix manipulation. With this we have a matrix operator on the plaintext: \[\begin{equation}A=\begin{bmatrix} a & b \\ c & d \end{bmatrix} \end{equation}\] which is used to encode, and then the decoder is the inverse of this: \[\begin{equation}A^{-1}=\begin{bmatrix} d & -b \\ -c & a \end{bmatrix} \end{equation}\]For example we take the matrix: \[\begin{equation}A=\begin{bmatrix} 2 & 3 \\ 3 & 5 \end{bmatrix} \end{equation}\]Then the reverse will be: \[\begin{equation}A=\begin{bmatrix} 5 & -3 \\ -3 & 2 \end{bmatrix} \end{equation}\]We thus use the first matrix (A) to encode two characters at a time, and then use the inverse of the matrix to decode. Initially we take our characters from the plaintext in two letters at at time. For example for "hello" we have "he", "ll", and "o ". Next we look at our table (where a space is replaced with a #):
So for the message "hello", we take two characters at a time, such as "he" becomes [7,4], and multiply by the matrix given above: \[\begin{equation}Coding=\begin{bmatrix} 2 & 3 \\ 3 & 5 \end{bmatrix} \begin{bmatrix} 7 \\ 4 \end{bmatrix} = \begin{bmatrix} (14+12) \\ (21+20) \end{bmatrix}\ = \begin{bmatrix} (26) \\ (41) \end{bmatrix}\end{equation}\]We then do a (mod 27) on the result to get: \[\begin{equation}Coding= \begin{bmatrix} (26) \\ (14) \end{bmatrix}\end{equation}\]This will be a cipher of "#O" |
Some theory
A matrix of:
\[\begin{equation}A=\begin{bmatrix} a & b \\ c & d \end{bmatrix} \end{equation}\]and a matrix of B:
\[\begin{equation}B=\begin{bmatrix} e \\ f \end{bmatrix} \end{equation}\]when multiplied gives:
\[\begin{equation}Z=\begin{bmatrix} (a.e+b.f) \\ (c.e+d.f) \end{bmatrix} \end{equation}\]For example:
\[\begin{equation}A=\begin{bmatrix} 5 & 4 \\ 3 & 2 \end{bmatrix} \end{equation}\]and a matrix of B:
\[\begin{equation}B=\begin{bmatrix} 3 \\ 5 \end{bmatrix} \end{equation}\]when multiplied gives:
\[\begin{equation}Z=\begin{bmatrix} 5 & 4 \\ 3 & 2 \end{bmatrix} \begin{bmatrix} 3 \\ 5 \end{bmatrix} = \begin{bmatrix} (15+20) \\ (9+10) \end{bmatrix} \end{equation}\]to give:
\[\begin{equation}Z=\begin{bmatrix} 35 \\ 19 \end{bmatrix} \end{equation}\]Some code
The following code is called by (where inp is the input plain text):
tbCode = hillcode(aval, bval, cval, dval, inp);or
StbCode= hillcode(dval, -bval, -cval, aval, inp);
public string hillcode(int a, int b, int c, int d, string word) { word = word.Trim(); word = word.ToLower(); string result=""; if (word.Length % 2 == 1) word += " "; for (int i = 0; i < word.Length; i+=2) { string ss = word[i]+""+word[i+1]; int val1 = word[i]-(char)'a'; int val2 = word[i+1]-(char)'a'; if (val1 < 0) val1 = 26; if (val2 < 0) val2 = 26; int vala = (a * val1 + b * val2) ; int valb = (c * val1 + d * val2); if (vala < 0) { vala = 27-(-vala)%27; } else vala = vala%27; if (valb < 0) { valb = 27-(-valb)%27; } else valb = valb%27; char ch1 = (char)((vala) + (int)'a') ; char ch2 = (char)((valb) + (int)'a'); if (ch1=='{') ch1=' '; if (ch2=='{') ch2=' '; result += "" + ch1 + ch2; } return addlinebreaks(result,32,32); }