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

C examples for Data Structure:Encrypt Decrypt

Description

Implement a cryptographic system using the Affine cipher method.

Demo Code

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

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

void encryptMsg()
{
   int i;//from w ww  . j  a  v a2 s .c o m
   length = strlen(msgOriginal);
   for (i = 0; i < length; i++)
      msgEncrypt[i] = ((((a * msgOriginal[i]) + b) % 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 chars): \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] - b)) % 26)) + 65); 
      msgDecrypt[length] = '\0';
   printf("\nDecrypted Message: %s", msgDecrypt);
}

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

Result


Related Tutorials