# Hill Cipher in C and C++ (Encryption and Decryption)

Here you get encryption and decryption program for hill cipher in C and C++.

What is Hill Cipher?

In cryptography (field related to encryption-decryption) hill cipher is a polygraphic cipher based on linear algebra. Invented by Lester S. Hill in 1929 and thus got it’s name. It was the first cipher that was able to operate on 3 symbols at once.

Also Read: Caesar Cipher in C and C++ [Encryption & Decryption]

Encryption: The given message string and key string is represented in the form of matrix. Then key and message matrix are multiplied. Finally modulo 26 is taken for each element of matrix obtained by multiplication. The key matrix that we take here should be invertible, otherwise decryption will not be possible.

Decryption: The encrypted message matrix is multiplied by the inverse of key matrix and finally its modulo 26 is taken to get the original message.

https://en.wikipedia.org/wiki/Hill_cipher

## Hill Cipher Program in C++

Output

Comment below if you have any queries related to above program for hill cipher in C and C++.

### 17 thoughts on “Hill Cipher in C and C++ (Encryption and Decryption)”

1. Explain this line: I know fmod is used to calculate modulo but why you are adding +97……
printf(“%c”, (char)(fmod(encrypt[i][0], 26) + 97));

1. can you please explain the logic behind q and p constants in the nested for loops at the beginning ?, I want to understand so i can use it for any dimension key

1. Yes- I appreciate that this is out there, but I am trying to figure out how to write my own program, not just use a hill cipher program. Meaningful variables and more comments would be very helpful!

2. The inverse matric is not showing the correct result it shows only
0 0 0
0 0 0
0 0 0
pls i want the crt program

3. inverse matrix = inverse_modulo(determinant of matrix)*(adjoint)^t
in decryption matrix value cannot be a flaot

4. hi the code in c++ is wrong – on line no.57 the code is wrong, it is of c language not c++ so

code mentioned in the above program is :- scanf(“%f”, &a[i][j]);

this is a c language code not a c++ one make necessary changes to this line and the c++ code will work smoothly.