Hill Cipher program in C
Introduction
Hill cipher is a polygraphic substitution cipher based on linear algebra.
Here is source code of the C Program to Implement the Hill Cypher. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
Hill Cipher Algorithm
A cipher (or cypher) in cryptography is an ensemble of predetermined procedures that may be used as an approach to perform encryption or decryption. Encipherment is a different, less frequently used term. Data is transformed through a code or cipher when it is encoded or enciphered. In everyday speech, the terms "cipher" and "code" are interchangeable because both of them refer to a series of operations that encrypt a message; however, in cryptography, particularly classical cryptography, the concepts are different.
While ciphers typically replace the same number of characters as the input, codes typically replace distinct strings of characters in the output. There are also exceptions, and certain cipher systems can generate a small number of characters more or less than they did when they were input.
An alphabetic text may be encrypted and decrypted using the Hill Cipher cryptographic algorithm. Each of the letters is denoted by a number in this cipher (for example, A = 0, B = 1, and C = 2). A message is encrypted by multiplying every one block of n letters (which is equivalent to an n-component vector) by an inverted n-n matrix against modulus 26. Each component in the message has been divided by the opposite side of the encryption matrix to decrypt it.
Encryption
The matrix is used to represent the message string and key string that are provided. The key matrix and message matrix have then multiplied. Each matrix element generated through multiplication has its modulus set to 26 at the end. In order for decryption to work, the key matrix we use here must be invertible.
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.
Example
#include<stdio.h>
#include<string.h>
int main() {
unsigned int a[4][4] = { { 6, 24, 1,12 }, { 13, 16, 10,13 }, { 20, 17, 15,13 } };
unsigned int b[4][4] = { { 8, 5, 10 ,11}, { 21, 8, 21,23 }, { 21, 12, 8 ,35} };
int i, j;
unsigned int c[20], d[20];
char msg[20];
int determinant = 0, t = 0;
;
printf("Enter plain text\n ");
scanf("%s", msg);
for (i = 0; i < 3; i++) {
c[i] = msg[i] - 65;
printf("%d ", c[i]);
}
for (i = 0; i < 3; i++) {
t = 0;
for (j = 0; j < 3; j++) {
t = t + (a[i][j] * c[j]);
}
d[i] = t % 26;
}
printf("\nEncrypted Cipher Text :");
for (i = 0; i < 4; i++)
printf(" %c", d[i] + 65);
for (i = 0; i < 4; i++) {
t = 0;
for (j = 0; j < 4; j++) {
t = t + (b[i][j] * d[j]);
}
c[i] = t % 26;
}
printf("\nDecrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", c[i] + 65);
return 0;
}
Output
![Hill Cipher program in C](https://static.tutorialandexample.com/c/hill-cipher-program-in-c1.png)
Example2
#include<stdio.h>
#include<math.h>
float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];
void encryption();
void decryption();
void getKeyMessage();
void inverse();
void main() {
getKeyMessage();
encryption();
decryption();
}
void encryption() {
int i, j, k;
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
encrypt[i][j] = encrypt[i][j] + a[i][k] * mes[k][j];
printf("\nEncrypted string is: ");
for(i = 0; i < 3; i++)
printf("%c", (char)(fmod(encrypt[i][0], 26) + 97));
}
void decryption() {
int i, j, k;
inverse();
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
decrypt[i][j] = decrypt[i][j] + b[i][k] * encrypt[k][j];
printf("\nDecrypted string is: ");
for(i = 0; i < 3; i++)
printf("%c", (char)(fmod(decrypt[i][0], 26) + 97));
printf("\n");
}
void getKeyMessage() {
int i, j;
char msg[3];
printf("Enter 3x3 matrix for key (It should be inversible):\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
scanf("%f", &a[i][j]);
c[i][j] = a[i][j];
}
printf("\nEnter a 3 letter string: ");
scanf("%s", msg);
for(i = 0; i < 3; i++)
mes[i][0] = msg[i] - 97;
}
void inverse() {
int i, j, k;
float p, q;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
if(i == j)
b[i][j]=1;
else
b[i][j]=0;
}
for(k = 0; k < 3; k++) {
for(i = 0; i < 3; i++) {
p = c[i][k];
q = c[k][k];
for(j = 0; j < 3; j++) {
if(i != k) {
c[i][j] = c[i][j]*q - p*c[k][j];
b[i][j] = b[i][j]*q - p*b[k][j];
}
}
}
}
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
b[i][j] = b[i][j] / c[i][i];
printf("\n\nInverse Matrix is:\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
printf("%d ", b[i][j]);
printf("\n");
}}
Output
![Hill Cipher program in C](https://static.tutorialandexample.com/c/hill-cipher-program-in-c2.png)