# 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. is there any encryption and decryption code in php? if yes, what’s the code….

2. Milind Jakati

Hey! I was wondering if there is a code for the above in Ruby?

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

1. it is the ascii value of ‘a’

4. will it work for any dimension??

1. no, it will work for 3×3 matrix key only..

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

5. Encryption is work successfully. But Decryption not works…

6. can you please provide hill cipher 2×2 matrix c++ program?

7. 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!

8. the encryption is work successfully but decryption is not no

9. 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

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

11. 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.

12. 