Introduction
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 a 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 does the Hill cipher work?
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.
The determinant of Matrix M is = -5, which is not actually the factor of 2 or 13, so we move on to the 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 a plaintext vector and multiply with the encrypted matrix (M).
Step 5. Replace each new vector with 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#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hill_Cipher
{
class Program
{
static void Main(string[] args)
{
int i, j, sum = 0, end = 0;
int[,] mtrx = new int[25, 25];
int[,] ans = new int[25, 1];
string text = "";
Console.WriteLine("Enter your Plaintext");
Console.Write("\n");
text = Console.ReadLine();
Console.Write("\n");
char[] txt = text.ToCharArray();
end = txt.Length;
for (i = 0; i < end; i++)
{
txt[i] = Convert.ToChar(txt[i] - 'a');
}
Random rnd = new Random();
for (i = 0; i < end; i++)
{
for (j = 0; j < end; j++)
{
mtrx[i, j] = rnd.Next();
}
}
for (i = 0; i < end; i++)
{
sum = 0;
for (j = 0; j < end; j++)
{
sum += mtrx[i, j] * (int)txt[j];
}
ans[i, 0] = sum;
}
Console.Write("Your CipherText is:");
for (i = 0; i < end; i++)
{
char cipher = (char)(((ans[i, 0]) % 26) + 97);
Console.Write("\t" + cipher);
}
Console.ReadKey();
}
}
}
Output Window
Hope you like it. Thank you for reading. Have a good day.