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

CEASAR

The document summarizes a lab report on implementing a Caesar cipher program in C. The program allows a user to encrypt a plain text message and decrypt a cipher text message using a specified key. It includes functions for encryption and decryption and a main function that gets user input and calls the appropriate function. The encryption algorithm shifts each letter by the key value using modulo arithmetic and the decryption algorithm applies the reverse shift. The program achieves the objective of implementing the basic Caesar cipher for encryption and decryption of messages.

Uploaded by

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

CEASAR

The document summarizes a lab report on implementing a Caesar cipher program in C. The program allows a user to encrypt a plain text message and decrypt a cipher text message using a specified key. It includes functions for encryption and decryption and a main function that gets user input and calls the appropriate function. The encryption algorithm shifts each letter by the key value using modulo arithmetic and the decryption algorithm applies the reverse shift. The program achieves the objective of implementing the basic Caesar cipher for encryption and decryption of messages.

Uploaded by

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

CEASAR’S CIPHER LAB ONE REPORT

1. The Objective of Lab 1.


The implementaion of lab one is to implement of the Ceasar Cipher program used to
encrypty a plain text and decrypt the cipher text using a particular key used during
encryption.

2. Language used.
The implementation is done using the C language, C is a Object Oriented Programing
language that was developed in the early 1970s by Dennis Ritchie at Bell Labs. It is
one of the most widely used programming languages.

3. Procedures followed.
Step one: Including necessary header files ,These lines include the necessary header
files. <stdio.h> is included for standard input/output operations, and <string.h> is
included for string manipulation functions.

Step two: Implementing Caesar Cipher Encryption and Decryption Functions , These
functions define the logic for encrypting and decrypting a message using the Caesar
cipher algorithm. The encrypt function shifts each letter in the message by a specified
amount, and the decrypt function reverses the process.

Step three: Implementing the Main Function ,This part of the code takes user input
for the message, key value, and the choice of encryption or decryption. It then calls
the appropriate function based on the user's choice and displays the result.

Step four: Compiling and Running, Compiling the code using a C compiler (e.g.,
gcc) and run the executable to test the Caesar cipher encryption and decryption
functionality.
4. Results
After running the code the user is prompted to enter the text , key value and either
encrypt the plain text or decrypt the cipher text.
Enter a message: box
Enter the key value: 3
Choose an option:
1. Encrypt
2. Decrypt
1
Encrypted message: ERA

5. Code organization (Functions and their functionality)


There are three functions in the program
*Encrypt function (Used for palin text encryption)
*Decrypt function (Used for cipher text decryption)
*Main function (main(){})

Code Snippet:
#include <stdio.h>
#include <string.h>

// Function to encrypt a message using Caesar cipher


void encrypt(char message[], int shift) {
// Loop through each character in the message
for (int i = 0; i < strlen(message); i++) {
// Check if the character is an uppercase letter
if (message[i] >= 'A' && message[i] <= 'Z') {
// Apply Caesar cipher encryption to uppercase letters
message[i] = (message[i] - 'A' + shift) % 26 + 'A';
}
// Check if the character is a lowercase letter
else if (message[i] >= 'a' && message[i] <= 'z') {
// Apply Caesar cipher encryption to lowercase letters
message[i] = (message[i] - 'a' + shift) % 26 + 'a';
}
// Ignore non-alphabetic characters
}
}

// Function to decrypt a message using Caesar cipher


void decrypt(char message[], int shift) {
// Loop through each character in the message
for (int i = 0; i < strlen(message); i++) {
// Check if the character is an uppercase letter
if (message[i] >= 'A' && message[i] <= 'Z') {
// Apply Caesar cipher decryption to uppercase letters
message[i] = (message[i] - 'A' - shift + 26) % 26 + 'A';
}
// Check if the character is a lowercase letter
else if (message[i] >= 'a' && message[i] <= 'z') {
// Apply Caesar cipher decryption to lowercase letters
message[i] = (message[i] - 'a' - shift + 26) % 26 + 'a';
}
// Ignore non-alphabetic characters
}
}

int main() {
// Declare variables to store user input
char message[100];
int shift, choice;

// Prompt the user to enter a message


printf("Enter a message: ");
fgets(message, sizeof(message), stdin);

// Prompt the user to enter the key value for encryption/decryption


printf("Enter the key value: ");
scanf("%d", &shift);

// Prompt the user to choose between encryption and decryption


printf("Choose an option:\n");
printf("1. Encrypt\n");
printf("2. Decrypt\n");
scanf("%d", &choice);

// Perform the chosen operation and display the result


if (choice == 1) {
encrypt(message, shift);
printf("Encrypted message: %s\n", message);
} else if (choice == 2) {
decrypt(message, shift);
printf("Decrypted message: %s\n", message);
} else {
printf("Invalid option.\n");
}

return 0;
}
6. Algorithims Used.
The program works under two(2) alogorithims , which are:

a. Encryption Algorithm:

 For each character in the message, the program checks whether it is an


uppercase or lowercase letter.
 If it is an uppercase letter, it shifts it by adding the shift value (shift) and takes
the result modulo 26 to wrap around the alphabet. The character is then
converted back to uppercase.
 If it is a lowercase letter, the same process is applied, but the character is
converted back to lowercase.
 Non-alphabetic characters are left unchanged.

b. Decryption Algorithim:

 Similar to encryption, for each character in the message, the program checks
whether it is an uppercase or lowercase letter.
 If it is an uppercase letter, it shifts it back by subtracting the shift value (shift)
and adding 26 (the alphabet length) to handle wrapping around. The character
is then converted back to uppercase.
 If it is a lowercase letter, the same process is applied, but the character is
converted back to lowercase.
 Non-alphabetic characters are left unchanged.

7. Conclusion.
The objective of the provided C code was to implement a basic form of the Caesar
cipher, allowing users to encrypt and decrypt messages using a specified key value.
The program achieves this objective, and it provides a simple command-line interface
for users to interact with the encryption and decryption functionalities.

You might also like