Implement a cryptographic system using the Multiplicative cipher method. - C Data Structure

C examples for Data Structure:Encrypt Decrypt

Description

Implement a cryptographic system using the Multiplicative cipher method.

Demo Code

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

char *msgOriginal = "this is a test";
char msgEncrypt[100];
char msgDecrypt[100];
int length, intChoice, a = 3;    /* a is the KEY */

void encryptMsg(){
  int i ;/*from w w w.j a  va2s  . c  o m*/
  length = strlen(msgOriginal);
  for (i = 0; i < length; i++)
    msgEncrypt[i] = (((a * msgOriginal[i]) % 26) + 65);
  msgEncrypt[length] = '\0';
  printf("\nEncrypted Message: %s", msgEncrypt);
}

void decryptMsg()
{
  int i;
  int aInv = 0;
  int flag = 0;
  printf("Enter the Message to Decrypt (upto 100 characters): \n");
  fflush(stdin);
  gets_s(msgEncrypt);
  length = strlen(msgEncrypt);
   for (i = 0; i < 26; i++) {
     flag = (a * i) % 26;
            if (flag == 1)
                aInv = i;
   }
   for (i = 0; i < length; i++)
      msgDecrypt[i] = (((aInv * msgEncrypt[i]) % 26) + 65);
   msgDecrypt[length] = '\0';
   printf("\nDecrypted Message: %s", msgDecrypt);
}

void main()
{
     encryptMsg();
     decryptMsg();
}

Result


Related Tutorials