Hill Cipher in Java [Encryption and Decryption]

Here you will learn about hill cipher in java with program and algorithm.

To start with the program directly is not a good idea here. Until you don’t have a brief understanding of Hill cipher algorithm, their programs will merely a code to copy paste.

As per Wikipedia, Hill cipher is a polygraphic substitution cipher based on linear algebra, invented by Lester S. Hill in 1929. Basically Hill cipher is a cryptography algorithm to encrypt and decrypt data to ensure data security.

Also Read: Caesar Cipher in Java

Also Read: Java Vigenere Cipher

Hill cipher uses the calculations of matrices used in Linear Algebra but it’s simple to understand if one has the basic knowledge of matrix multiplication, modulo calculation and inverse calculation of matrices.

In this example we are going to take up a 2X2 matrix for better understanding and simplification. The same method can be applied to 3X3 matrix to get the desired results.

Encryption

So the first thing we have to do in encrypting the data using hill cipher is to take up a string of characters as key matrix to encrypt data and convert this key matrix to number matrix.

For this context we will be using the string –“mble” (this is just any random combination of letters) and on converting this string to a number matrix following the rule of numbering alphabets from 0 to 25 (i.e. A=0, B=1…), we get our key matrix as:

hill chipher in java 1

We will try to encrypt “helloworld” here.

For this purpose we will need to convert this plain text into diagraphs. For this purpose we will write this from the starting of our first column vector having first letter at the top and second letter at the bottom and after this jumping on to the second column vector having third letter at the top and fourth letter at the bottom and so on.

hill cipher in java 2

If there were any left places then we can put any wild character at that place say ‘x’.

Next step is to convert these column vectors into their corresponding number codes.

hill cipher in java 3

Now we need to multiple each column vector from the key matrix and obtain the result. The result after multiplication is shown down here:

hill cipher in java 4

After this, as all the numbers are greater than 26 so we need to divide these column vectors with 26 and note the remainder i.e. calculate mod26 of these vectors.

So the resultant vector becomes:

hill cipher in java 5

And now let’s convert these numbers back to letters, following the old rule, so our column vector becomes:

hill cipher in java 6

This gives us the encoded text as – “KPNJIIDOFD”

The same process can be repeated for 3X3 matrix to encrypt the data.

Decryption

To decrypt the data using the Hill Cipher, first we need to find the inverse of our key matrix.

To do this first find the determinant of our key matrix. In our case determinant evaluates to 37, which is again greater than 26 so we will find mod26 of out determinant i.e., 37 = 11 mod 26

The next step is to find a number which gives the answer 1 when mod26 is found after multiplying that number by the modulo of our determinant. This can be done with the help of hit and trial method.

Applying this, we get 11 x 19 = 1 mod 26 and now we need to find the adjoint of our matrix and convert the negative numbers into positive numbers by finding mod26:

hill chiper in java 7

Next step is to multiply this adjoint with the number we found above (19) and find mod26 to keep the range under 26. On doing this, we get

hill cipher in java 8

 

“KPNJIIDOFD” was our encoded string, and now we have to repeat the steps of encryption to decrypt this string. So first we will write this string in column vectors, and next convert this column vectors into corresponding number and multiply it with the inverse of key matrix we found above and find mod26 then.

After that we need to transfer these numbers back to letters to get our actual string. One is done here.

hill cipher in java 9

Now comes the program to have this algorithm implemented.

Hill Cipher Program in Java

Output

Menu:
1: Encryption
2: Decryption
1
Enter the line:
helloworld
Enter the key:
mble
Result:
kpnjiidofd

Menu:
1: Encryption
2: Decryption
2
Enter the line:
kpnjiidofd
Enter the key:
mble
Result:
helloworld

Reference: https://www.sanfoundry.com/java-program-implement-hill-cypher/

Comment below if you have queries regarding hill cipher in java.

One comment

  • Shubham

    Can this problem could be solved in c++,if yes then what is the code.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *