C program for Encryption & Decryption of a String
/*
Date: 23rd August 2024
Purpose: Encryption & Decryption Operation on String
Author: Manas R. Das
*/
#include <stdio.h>
#include <string.h>
#include <windows.h> // Including Windows API for MessageBeep function
int c, e; // Declaring integer variables 'c' for choice and 'e' for encryption level
char str[1000], tmp[1000]; // Declaring character arrays 'str' and 'tmp' to hold the message
// Defining the encryption function
void encr(char str[]){
// Iterating over each character in the string and applying the encryption level
for (int i = 0; i < strlen(str); i++){
str[i] = str[i] + e; // Shifting each character by the encryption level
}
puts(str); // Printing the encrypted string
}
// Defining the decryption function
void decr(char str[]){
// Iterating over each character in the string and reversing the encryption
for (int i = 0; i < strlen(str); i++){
str[i] = str[i] - e; // Reversing the shift applied during encryption
}
puts(str); // Printing the decrypted string
}
int main(){
printf("\nEnter a Message: "); // Prompting the user to enter a message
fgets(str, sizeof(str), stdin); // Reading the input message and storing it in 'str'
str[strcspn(str, "\n")] = 0; // Removing the newline character from the end of the input
strcpy(tmp, str); // Copying the original message to 'tmp' for later comparison
do{
// Displaying the available operations to the user
printf("\nOperations:\n\t1. Encryption\n\t2. Decryption\n\t3. Exit\n");
printf("\nChoose the Operation: "); // Prompting the user to choose an operation
scanf("%d", &c); // Reading the user's choice and storing it in 'c'
getchar(); // Clearing the newline character left by scanf
// Switching based on the user's choice
switch (c){
case 1:
lable:
printf("\nEnter the Level of Encryption (Between 0 to 10): "); // Asking for the encryption level
scanf("%d", &e); // Reading the encryption level and storing it in 'e'
// Checking if the encryption level is within the valid range
if (e >= 0 && e <= 10){
encr(str); // Calling the encryption function if the choice is 1
}else{
MessageBeep(MB_ICONERROR); // Playing an error sound
printf("\n(i) Error 404: Unbounded Encryption Level!\n"); // Displaying an error message if the encryption level is invalid
goto lable;
}
break;
case 2:
// Checking if the string has been encrypted by comparing with the original
if (strcmp(str, tmp) != 0){
decr(str); // Calling the decryption function if the string was encrypted
}
else{
MessageBeep(MB_ICONERROR); // Playing an error sound
printf("\n(i) Error 404: Message not Encrypted!\n The Message: ");
puts(tmp); // Displaying the original message if not encrypted
}
break;
case 3:
return 0; // Exiting the program if the choice is 3
default:
MessageBeep(MB_ICONERROR); // Playing an error sound
printf("\nChoice Invalid!"); // Displaying an error message for invalid choice
}
} while (c != 3); // Repeating until the user chooses to exit
return 0; // Ending the main function
}
/*
OUTPUT:
Enter a Message: Hello
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 7
Choice Invalid!
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 2
(i) Error 404: Message not Encrypted!
The Message: Hello
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 1
Enter the Level of Encryption (Between 0 to 10): 0
Hello
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 2
(i) Error 404: Message not Encrypted!
The Message: Hello
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 1
Enter the Level of Encryption (Between 0 to 10): 1
Ifmmp
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 2
Hello
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 1
Enter the Level of Encryption (Between 0 to 10): 2
Jgnnq
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 2
Hello
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 1
Enter the Level of Encryption (Between 0 to 10): 3
Khoor
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 2
Hello
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 1
Enter the Level of Encryption (Between 0 to 10): 4
Lipps
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 2
Hello
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 1
Enter the Level of Encryption (Between 0 to 10): 5
Mjqqt
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 2
Hello
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 1
Enter the Level of Encryption (Between 0 to 10): 10
Rovvy
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 2
Hello
Operations:
1. Encryption
2. Decryption
3. Exit
Choose the Operation: 3
*/


Comments