C++ Program To Perform Message Encoding Using Matrix Multiplication - C++ Programming Tutorial
C++ Course / C++ Programs / C++ Program To Perform Message Encoding Using Matrix Multiplication

C++ Program To Perform Message Encoding Using Matrix Multiplication

BLUF: Mastering C++ Program To Perform Message Encoding Using Matrix Multiplication is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Program To Perform Message Encoding Using Matrix Multiplication

C++ is renowned for its efficiency. Learn how C++ Program To Perform Message Encoding Using Matrix Multiplication enables low-level control and high-performance computing in the tutorial below.

This C program employs matrix multiplication to cipher a message. This encryption technique involves a large matrix to encode a message, providing a high level of security. To decode the message, the recipient utilizes the inverse of the matrix. The original matrix serves as the encoding matrix, while its inverse acts as the decoding matrix.

It presents the source code of a C program for encrypting and decrypting messages through matrix multiplication technique. The C program effectively compiles and executes on a Linux platform. Here's the output of the program:

Program:

Example

#include <stdio.h>
#include <string.h>

void mul(int key[3][3], int message[3][10], int result[3][10])
{
 int c, d, sum, k;
 for (c = 0; c < 3; c++)
 {
 for (d = 0; d < 10; d++)
 {
 sum = 0;
 for (k = 0; k < 3; k++)
 {
 sum = sum + key[c][k] * message[k][d];
 }
 result[c][d] = sum;
 }
 }
}

int main()
{
 char str[30] = "example program to demonstrate";
 int len;
 int i, j;
 int result[3][10] = {0};
 int key[3][3] = {
 {1, 2, 3},
 {0, -1, 1},
 {-1, 1, 2}
 };
 int decode_key[3][3] = {
 {4, 5, 6},
 {7, 8, 9},
 {10, 11, 12}
 };
 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("Encoded 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(decode_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(" ");
 }
 }

 return 0;
}

Output:

Output

Encoded message to be sent: 15, 22, 37, 17, 29, 50, 18, 24, 39, 23, 
Decoded message is: example program to demonstrate

Explanation:

This software application performs multiplication of the message with a matrix using an encryption key matrix, and subsequently utilizes a distinct decryption key matrix to decode the message.

Utilize the key matrix (key) along with the decode key matrix (decode_key) to encrypt and decrypt a message. The software encrypts the message and then displays the encrypted data as a series of integers. Subsequently, the message is decrypted using the decode key matrix, and the resulting decoded message is showcased.

For the purpose of encoding and decoding, the software converts lowercase letters into corresponding numerical values ranging from 1 to 26. This process assumes that the provided text is entirely in lowercase and does not contain any special characters.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience