Hill's Cipher

[Back] Lester 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 #):

a b c d e f g h i j k l m n o p q r s t u v w x y z #
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

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"

Plaintext input: Hill code input:

Enter Sentence:

Coding Matrix:

a value: b value:
c value: d value:

Result

Code:

Frequency Analysis of Text

This table shows the occurances of the letters in the text (ignoring the case of the letters):

Mapping to normal

This table shows how the text matches a normal probability to text (where 'E' has the highest level of occurance and 'Z' has the least). The grey rows show what would be expected for the order, and the red one shows what your text gives for the order:

Frequency Analysis

Predictions

From this we predict:
  • From this I predict that of your cipher text maps to in plaintext.
  • From this I predict that of your cipher text maps to in plaintext.
  • From this I predict that of your cipher text maps to or in plaintext.
  • From this I predict that of your cipher text maps to or in plaintext.

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);
           
        }

Examples