Saturday, 22 October 2022

Encryption and Decryption Algorithm

 #include <stdio.h>

int main()

{

   int i, x;

   char str[100];


   printf("\nPlease enter a string:\t");

   gets(str);


   printf("\nPlease choose following options:\n");

   printf("1 = Encrypt the string.\n");

   printf("2 = Decrypt the string.\n");

   scanf("%d", &x);


   //using switch case statements

   switch(x)

   {

   case 1:

      for(i = 0; (i < 100 && str[i] != '\0'); i++)

        str[i] = str[i] + 3; //the key for encryption is 3 that is added to ASCII value


      printf("\nEncrypted string: %s\n", str);

      break;


   case 2:

      for(i = 0; (i < 100 && str[i] != '\0'); i++)

        str[i] = str[i] - 3; //the key for encryption is 3 that is subtracted to ASCII value


      printf("\nDecrypted string: %s\n", str);

      break;


   default:

      printf("\nError\n");

   }

   return 0;

}

No comments:

Post a Comment