C Program to Perform Message Encoding Using Matrix Multiplication

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.

  1. /*
  2. Restriction: Input string has to be in lower case without any special characters
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. void mul(int first[3][3], int second[3][10], int result[3][10])
  7. {
  8.     int c, d, sum, k;
  9.     int i, j;
  10.     for ( c = 0 ; c < 3 ; c++ )
  11.     {
  12.       for ( d = 0 ; d < 10 ; d++ )
  13.       {
  14.         sum = 0;
  15.         for ( k = 0 ; k < 3 ; k++ )
  16.         {
  17.           sum = sum + first[c][k] * second[k][d];
  18.         }
  19.         result[c][d] = sum;
  20.       }
  21.     }
  22.  
  23. }
  24. int main()
  25. {
  26.     char str[29] = "this message is to be encoded";
  27.     int len;
  28.     int i, j;
  29.     int result[3][10] = {0};
  30.     int key[3][3] = {
  31.                       {-3, -3, -4},
  32.                       {0, 1, 1},
  33.                       {4, 3, 4}
  34.                     };
  35.     int inv_key[3][3] = {
  36.                         {1, 0, 1},
  37.                         {4, 4, 3},
  38.                         {-4, -3, -3}
  39.                         };
  40.     int encode[3][10] = {32};
  41.     int decode[3][10] = {0};
  42.     len = strlen(str);
  43.  
  44.     for (i = 0; i < 10; i++)
  45.     {
  46.         for(j = 0; j < 3; j++)
  47.         {
  48.             if (str[j + i*3] >='a'  && str[j + i*3] <='z')
  49.             {
  50.                 encode[j][i] = str[j + i*3] - 96;
  51.  
  52.             }
  53.             if (str[j + i*3] == 32)
  54.             {
  55.                 encode[j][i] = 32;
  56.             }
  57.             if (str[j + i*3] == '\0')
  58.                 break;
  59.         }
  60.          if (str[j + i*3] == '\0')
  61.                 break;
  62.     }
  63.     mul( key, encode, result);
  64.     printf("\nEncoded message to be sent: ");
  65.     for (i = 0; i < 10; i++)
  66.     {
  67.         for ( j = 0 ; j < 3; j++)
  68.             printf("%d, ", result[j][i]);
  69.     }
  70.     printf("\nDecoded message is: ");
  71.     mul(inv_key, result, decode);
  72.     for (i = 0; i < 10; i++)
  73.     {
  74.         for ( j = 0; j < 3; j++)
  75.         {
  76.             if ( ((decode[j][i]+96)) >= 97 && ((decode[j][i]+96) <= 123))
  77.                 printf("%c", (decode[j][i] + 96) );
  78.             else if ( decode[j][i] == 32)
  79.                 printf(" ");
  80.             else
  81.                 return;
  82.         }
  83.     }
  84.     return 0;
  85. }

$ 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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.