This C program encodes a message using matrix multiplication. One type of code, which is extremely difficult to break, makes use of a large matrix to encode a message. The receiver of the message decodes it using the inverse of the matrix. This first matrix is called the encoding matrix and its inverse is called the decoding matrix.
Here is the source code of the C program to encode and decode a message using matrix multiplication technique. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
Restriction: Input string has to be in lower case without any special characters
*/
#include <stdio.h>
#include <string.h>
void mul(int first[3][3], int second[3][10], int result[3][10])
{
int c, d, sum, k;
int i, j;
for ( c = 0 ; c < 3 ; c++ )
{
for ( d = 0 ; d < 10 ; d++ )
{
sum = 0;
for ( k = 0 ; k < 3 ; k++ )
{
sum = sum + first[c][k] * second[k][d];
}
result[c][d] = sum;
}
}
}
int main()
{
char str[29] = "this message is to be encoded";
int len;
int i, j;
int result[3][10] = {0};
int key[3][3] = {
{-3, -3, -4},
{0, 1, 1},
{4, 3, 4}
};
int inv_key[3][3] = {
{1, 0, 1},
{4, 4, 3},
{-4, -3, -3}
};
int encode[3][10] = {32};
int decode[3][10] = {0};
len = strlen(str);
for (i = 0; i < 10; i++)
{
for(j = 0; j < 3; j++)
{
if (str[j + i*3] >='a' && str[j + i*3] <='z')
{
encode[j][i] = str[j + i*3] - 96;
}
if (str[j + i*3] == 32)
{
encode[j][i] = 32;
}
if (str[j + i*3] == '\0')
break;
}
if (str[j + i*3] == '\0')
break;
}
mul( key, encode, result);
printf("\nEncoded message to be sent: ");
for (i = 0; i < 10; i++)
{
for ( j = 0 ; j < 3; j++)
printf("%d, ", result[j][i]);
}
printf("\nDecoded message is: ");
mul(inv_key, result, decode);
for (i = 0; i < 10; i++)
{
for ( j = 0; j < 3; j++)
{
if ( ((decode[j][i]+96)) >= 97 && ((decode[j][i]+96) <= 123))
printf("%c", (decode[j][i] + 96) );
else if ( decode[j][i] == 32)
printf(" ");
else
return;
}
}
return 0;
}
$ gcc encode.c -o encode $ ./encode Encoded message to be sent: -120, 17, 140, -205, 45, 224, -148, 38, 153, -44, 12, 45, -199, 28, 231, -216, 35, 248, -122, 7, 154, -167, 19, 199, -70, 19, 73, -27, 4, 32, Decoded message is: this message is to be encoded
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Watch Advanced C Programming Videos
- Check C Books
- Practice BCA MCQs
- Check Computer Science Books
- Apply for Computer Science Internship