Encryption and Decryption Program
Algorithm:
START
1. Input key
2. Input choice (1 for encryption, 2 for decryption)
3. Input string ch
4. If choice == 1 (Encryption)
For each character ch[i] in ch:
If ch[i] is a lowercase letter ('a' to 'z'):
ch[i] = (ch[i] - 'a' + key) % 26 + 'a'
Else if ch[i] is an uppercase letter ('A' to 'Z'):
ch[i] = (ch[i] - 'A' + key) % 26 + 'A'
Else if ch[i] is a digit ('0' to '9'):
ch[i] = (ch[i] - '0' + key) % 10 + '0'
Print "The encrypted message is: " ch
5. Else if choice == 2 (Decryption)
For each character ch[i] in ch:
If ch[i] is a lowercase letter ('a' to 'z'):
ch[i] = (ch[i] - 'a' - key + 26) % 26 + 'a'
Else if ch[i] is an uppercase letter ('A' to 'Z'):
ch[i] = (ch[i] - 'A' - key + 26) % 26 + 'A'
Else if ch[i] is a digit ('0' to '9'):
ch[i] = (ch[i] - '0' - key + 10) % 10 + '0'
Print "The decrypted message is: " ch
END
Flowchart:
Program Source Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
int i, j, key, choice;
char a[26], ch[26];
printf("enter the key for encryption or decryption: \n");
scanf("%d", &key);
printf("enter choice:\n");
printf("1.Encryption\n");
printf("2.Decryption\n");
scanf("%d", &choice);
printf("enter the string: \n");
scanf("%s", ch);
switch (choice)
{
case 1:
for (i = 0; ch[i] != '\0'; i++)
{
if (ch[i]>='a'&&ch[i]<='z')
{ch[i] = (ch[i] - 'a' + key) % 26 + 'a';}
if (ch[i]>='A'&&ch[i]<='Z')
{ch[i] = (ch[i] - 'A' + key) % 26 + 'A';}
if (ch[i]>='0'&&ch[i]<='9')
{ch[i] = (ch[i] - '0' + key) % 10 + '0';}
}
printf("The encrypted message is: %s\n", ch);
break;
case 2:
for (i = 0; ch[i] != '\0'; i++)
{
if (ch[i]>='a'&&ch[i]<='z')
{ch[i] = (ch[i] - 'a' - key+26) % 26 + 'a';}
if (ch[i]>='A'&&ch[i]<='Z')
{ch[i] = (ch[i] - 'A' - key+26) % 26 + 'A';}
if (ch[i]>='0'&&ch[i]<='9')
{ch[i] = (ch[i] - '0' - key+10) % 10 + '0';}
}
printf("The decrypted message is: %s\n", ch);
break;
}
return 0;
}
Sample Output: