In: Computer Science
write a C program which performs encryption and decryption of a message
// C Program for encyption and decryption of
message
#include <stdio.h> // Header File
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
switch(x)
{
case 1:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] + 10; //the key for encryption is 10 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] - 10; //the key for encryption is 10 that is
subtracted to ASCII value
printf("\nDecrypted string: %s\n", str);
break;
default:
printf("\nError\n");
}
return 0;
}
Output Snapshot: