Introduction
The Caesar Cipher is a simple encryption technique used in classical cryptography. It is a substitution cipher where each letter in the plaintext is shifted by a certain number of positions down or up the alphabet. This type of encryption is easy to implement but not very secure by modern standards.
Objective
The objective of this program is to implement the Caesar Cipher algorithm in C programming language to perform both encryption and decryption. The program allows the user to input a message and a shift value and provides an encrypted message for secure communication. It also supports decrypting a message using the same shift value.
Code Implementation
#include #include // Function to encrypt the message void encrypt(char* message, int shift) { for(int i = 0; message[i] != '\0'; i++) { char ch = message[i]; // Encrypt uppercase letters if(ch >= 'A' && ch <= 'Z') { message[i] = ((ch - 'A' + shift) % 26) + 'A'; } // Encrypt lowercase letters else if(ch >= 'a' && ch <= 'z') { message[i] = ((ch - 'a' + shift) % 26) + 'a'; } } } // Function to decrypt the message void decrypt(char* message, int shift) { for(int i = 0; message[i] != '\0'; i++) { char ch = message[i]; // Decrypt uppercase letters if(ch >= 'A' && ch <= 'Z') { message[i] = ((ch - 'A' - shift + 26) % 26) + 'A'; } // Decrypt lowercase letters else if(ch >= 'a' && ch <= 'z') { message[i] = ((ch - 'a' - shift + 26) % 26) + 'a'; } } } int main() { char message[100]; int shift; int choice; printf("Enter a message: "); fgets(message, sizeof(message), stdin); message[strcspn(message, "\n")] = '\0'; // Remove newline character printf("Enter shift value: "); scanf("%d", &shift); printf("\nCaesar Cipher Menu:\n"); printf("1. Encrypt Message\n"); printf("2. Decrypt Message\n"); printf("Enter your choice: "); scanf("%d", &choice); switch(choice) { case 1: encrypt(message, shift); printf("\nEncrypted Message: %s\n", message); break; case 2: decrypt(message, shift); printf("\nDecrypted Message: %s\n", message); break; default: printf("\nInvalid choice.\n"); break; } return 0; }
Explanation of the Program Structure
The program is structured as follows:
- encrypt function: This function takes the input message and a shift value, and encrypts the message by shifting each character according to the Caesar Cipher logic. It handles both uppercase and lowercase letters.
- decrypt function: This function works similarly to the encrypt function, but it shifts characters in the opposite direction to decrypt the message.
- main function: This is the entry point of the program. It accepts the user’s message and shift value, and then provides a menu for the user to choose whether they want to encrypt or decrypt the message. The appropriate function is called based on the user’s choice.
How to Run the Program
- Write the code in any C programming editor or IDE.
- Compile the program using a C compiler (e.g., GCC). You can use the following command:
gcc caesar_cipher.c -o caesar_cipher
- Run the compiled program:
./caesar_cipher
- Input the message and the shift value as prompted by the program. Then, choose whether you want to encrypt or decrypt the message.