Hill Cipher In C#

In my previous article, we saw about monoalphabetic cipher. Today, we will discuss yet another substitution technique – Hill Cipher which is far better than monoalphabetic cipher.

Hill cipher works on a mathematical concept of matrix. So, before going further, we should throw some light on the matrix.

According to Wikipedia,
"A matrix is a rectangular array of number, symbols or expressions arranged in rows and columns."



For example, a 2X2 matrix is comprised of two rows and two columns. 



For example, a 2x3 matrix is comprised of two rows and three columns.



We will skip other things like how the actual matrix works. It is very simple and if someone has a doubt about matrix operations, comment in the comment box. I’ll arrange an article on Matrix operations.

How the Hill cipher works

Suppose, you want to encrypt the message – “Dr Greer Rocks“. See the steps, mentioned below, to work on the encryption part of your plaintext.

Plaintext = = > Dr Greer Rocks

Step 1

From the Square matrix of 2X2 with a non-negative integer, where each element is less than 26, the matrix actually is given in the analysis part. Right now, we assume that we have the encryption matrix, as shown below.


Step 2

Check that its determinant does not factor by 2 or 3 and if it exists, return to the step, mentioned above, to change the Encryption matrix.



Determinant of Matrix M is = -5 which is not actually the factor of 2 or 13, so we move on to next step.

Step 3

Make a pair of your plaintext, if you have an odd number of letters then repeat the last letter or just embed “X” into the plaintext. (You can add any other letter too.)

Pair the letters and declare their identity alphabetically, where A=1, B=2, C=3 and so on.

DR GREER ROCKS => [DR], [GR], [EE], [RR], [OC], [KS]



Step 4

Convert each pair into plaintext vector and multiply with the encrypted matrix (M).





Step 5

Replace each new vector by residue module 26.



Step 6

Convert each Ciphertext vector into its corresponding position in the alphabet.



Step 7

Align the letter in a single line and you will get your Ciphertext.



Implementation of Encryption of Hill Cipher in C#
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace Hill_Cipher {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             int i, j, sum = 0, end = 0;  
  10.             int[, ] mtrx = new int[25, 25];  
  11.             int[, ] ans = new int[25, 1];  
  12.             string text = "";  
  13.   
  14.             Console.WriteLine("Enter your Plaintext");  
  15.             Console.Write("\n");  
  16.             text = Console.ReadLine();  
  17.             Console.Write("\n");  
  18.             char[] txt = text.ToCharArray();  
  19.             end = txt.Length;  
  20.   
  21.   
  22.             for (i = 0; i < end; i++) {  
  23.                 txt[i] = Convert.ToChar(txt[i] - 'a');  
  24.             }  
  25.   
  26.             Random rnd = new Random();  
  27.             for (i = 0; i < end; i++) {  
  28.                 for (j = 0; j < end; j++) {  
  29.   
  30.                     mtrx[i, j] = rnd.Next();  
  31.                 }  
  32.   
  33.             }  
  34.   
  35.             for (i = 0; i < end; i++) {  
  36.   
  37.                 sum = 0;  
  38.                 for (j = 0; j < end; j++) {  
  39.                     sum += mtrx[i, j] * (int) txt[j];  
  40.                 }  
  41.                 ans[i, 0] = sum;  
  42.             }  
  43.   
  44.             Console.Write("Your CipherText is:");  
  45.             for (i = 0; i < end; i++) {  
  46.                 char cipher = (char)(((ans[i, 0]) % 26) + 97);  
  47.                 Console.Write("\t" + cipher);  
  48.   
  49.             }  
  50.   
  51.             Console.ReadKey();  
  52.   
  53.         }  
  54.   
  55.     }  
  56. }  
Output Window



Hope you like it. Thank you for reading. Have a good day.

Up Next
    Ebook Download
    View all
    Learn
    View all