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

Caesar Cipher Implementation

Uploaded by

ShivaPreethi
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

Caesar Cipher Implementation

Uploaded by

ShivaPreethi
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

Caesar Cipher Implementation

Introduction
The Caesar Cipher is one of the oldest and simplest encryption techniques, used by
Julius Caesar to protect sensitive military messages. It is a type of substitution cipher
where each letter in the plaintext is shifted by a certain number of positions down the
alphabet.
For example, with a shift of 3, 'A' becomes 'D', 'B' becomes 'E', and so on.
This document describes a Python program that implements the Caesar Cipher
algorithm for both encryption and decryption of text. The program allows users to
input a message and a shift value to encrypt or decrypt the message accordingly.

Program Overview
The Python program performs the following operations:
1. Accepts a message and a shift value from the user.
2. Encrypts the message using the Caesar Cipher.
3. Decrypts the message back to its original form using the same shift value.

Code Explanation
Encryption Function (encrypt)
The function takes the text and a shift value as input. It then iterates over each character
in the text:
 If the character is uppercase or lowercase, it shifts it by the specified shift value.
 Non-alphabetic characters are not encrypted and are added to the result as-is.
Decryption Function (decrypt)
This function simply calls the encrypt function with a negative shift value to reverse the
encryption process.
Code Implementation
# Function to encrypt text using Caesar Cipher
def encrypt(text, shift):
result = ""
# Loop through each character in the text
for char in text:
# Encrypt uppercase letters
if char.isupper():
result += chr((ord(char) + shift - 65) % 26 + 65)
# Encrypt lowercase letters
elif char.islower():
result += chr((ord(char) + shift - 97) % 26 + 97)
else:
# If the character is not a letter, add it as is
result += char
return result

# Function to decrypt text using Caesar Cipher


def decrypt(text, shift):
return encrypt(text, -shift)

# Get user input for message and shift


message = input("Enter your message: ")
shift_value = int(input("Enter shift value: "))
# Encrypt and decrypt the message
encrypted_message = encrypt(message, shift_value)
decrypted_message = decrypt(encrypted_message, shift_value)

# Display the results


print(f"Encrypted Message: {encrypted_message}")
print(f"Decrypted Message: {decrypted_message}")

Working of this Code


1. Encryption Function (encrypt):
 The function loops through each character of the input text.
 If the character is uppercase, it shifts the letter forward by the
given shift value and wraps it around the alphabet using modulo 26. It does
the same for lowercase letters.
 Non-alphabetic characters (such as spaces or punctuation) are added to the
result without any changes.
 The encrypted text is returned.
2. Decryption Function (decrypt):
 The decryption is simply the reverse of encryption. Since encryption shifts
letters forward by the shift value, decryption shifts them back by the same
value (i.e., -shift).
 It calls the encrypt function with a negative shift to decrypt the message.
3. User Interaction:
 The program prompts the user for a message and the shift value.
 It encrypts the message and then decrypts it using the same shift value.
 The encrypted and decrypted messages are displayed.
Sample Output

Final thoughts
This program allows users to encrypt and decrypt messages using the Caesar Cipher,
demonstrating both the encryption and decryption processes with a simple shift. It
handles both uppercase and lowercase letters, leaving non-alphabetic characters
untouched. The program can be expanded by adding features such as handling different
alphabets or adding a graphical user interface (GUI).

You might also like