0% found this document useful (0 votes)
17 views4 pages

Encryption and Decryption Program-1

The document outlines an encryption and decryption program that uses a key to modify characters in a string based on user input. It includes an algorithm and source code in C that handles both encryption and decryption for lowercase letters, uppercase letters, and digits. The program prompts the user for a key, choice of operation, and the string to be processed, then outputs the resulting encrypted or decrypted message.

Uploaded by

catsaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Encryption and Decryption Program-1

The document outlines an encryption and decryption program that uses a key to modify characters in a string based on user input. It includes an algorithm and source code in C that handles both encryption and decryption for lowercase letters, uppercase letters, and digits. The program prompts the user for a key, choice of operation, and the string to be processed, then outputs the resulting encrypted or decrypted message.

Uploaded by

catsaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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:

You might also like