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

  1. Write the code in any C programming editor or IDE.
  2. Compile the program using a C compiler (e.g., GCC). You can use the following command:
    gcc caesar_cipher.c -o caesar_cipher
  3. Run the compiled program:
    ./caesar_cipher
  4. Input the message and the shift value as prompted by the program. Then, choose whether you want to encrypt or decrypt the message.
© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)