Ceasar Python

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

CEASAR’S CIPHER LAB ONE REPORT

1. The Objective of Lab 1.


- The objective of the Python script is to implement the Caesar Cipher algorithm for
encrypting and decrypting text using a specified key (shift). The script aims to take
user input, perform encryption and decryption operations, and display the results.

2. Language used.
- The implementation is done using Python, a high-level programming language
known for its readability and simplicity. Python is widely used for various
applications, including scripting, web development, and data analysis.

3. Procedures followed.
- The script follows a modular structure with three functions: ‘encrypt’, ‘decrypt’,
and ‘main’.

Step 1: Encryption and Decryption Functions


The script defines two functions, `encrypt` and `decrypt`, responsible for the
encryption and decryption processes using the Caesar Cipher algorithm. These
functions iterate through each character in the input text, determine its case, and apply
the appropriate shift to perform encryption or decryption. Non-alphabetic characters
remain unchanged.

Step 2: Main Function for User Interaction


The `main` function orchestrates user interaction, taking input for the text and shift
value. It calls the `encrypt` and `decrypt` functions to perform the respective
operations. The results, i.e., the encrypted and decrypted text, are then displayed to
the user.

Step 3: User Input


The script prompts the user to enter the text and the shift value during the execution of
the `main` function. The user provides these inputs interactively, allowing flexibility
in testing the Caesar Cipher with different texts and shift values.

Step 4: Execution and Output


After receiving user input, the script executes the encryption and decryption processes
based on the provided text and shift value. The results are displayed to the user,
showing both the encrypted and decrypted text. This step allows users to observe the
impact of the Caesar Cipher algorithm on the input text.
4. Results:
- After running the script, the user is prompted to enter the text and the shift value.
The script then displays the encrypted and decrypted text.
Enter the text: Hello
Enter the shift value: 3

Original text: Hello


Encrypted text: Khoor
Decrypted text: Hello

5. Code organization (Functions used and theie functionality):


The program contains three functions which are:

1. encrypt Function:
This function takes a text and a shift value as input and applies the Caesar Cipher
encryption algorithm. It iterates through each character in the text, determining
whether it is uppercase or lowercase, and shifts it accordingly. Non-alphabetic
characters remain unchanged.

2. decrypt Function:
The decrypt function implements Caesar Cipher decryption, utilizing the encrypt
function with a negative shift. It takes an encrypted text and a shift value as input and
returns the decrypted text. The decryption process is essentially the reverse of
encryption.

3. main Function:
The main function serves as the entry point for the program, orchestrating user
interaction and execution. It prompts the user to input a text and a shift value, calls the
encrypt and decrypt functions, and prints the results. It encapsulates the overall flow
and logic of the program.

For the code organization , here is the full code snippet for the Ceasar Cipher using
Python langauge.
Code Snippet:
# Function to encrypt a text using the Caesar Cipher algorithm
def encrypt(text, shift):
result = "" # Initialize an empty string to store the encrypted text
for char in text:
if char.isalpha(): # Check if the character is an alphabet letter
# Determine whether the character is uppercase or lowercase
if char.isupper():
# Apply Caesar Cipher encryption to uppercase letters
result += chr((ord(char) + shift - ord('A')) % 26 + ord('A'))
else:
# Apply Caesar Cipher encryption to lowercase letters
result += chr((ord(char) + shift - ord('a')) % 26 + ord('a'))
else:
# If the character is not alphabetic, leave it unchanged
result += char
return result

# Function to decrypt a text using the Caesar Cipher algorithm


def decrypt(text, shift):
# Decryption is the same as encryption, but with a negative shift
return encrypt(text, -shift)

# Main function for user interaction and execution


def main():
text = input("Enter the text: ") # Prompt the user to enter the text
shift = int(input("Enter the shift value: ")) # Prompt the user to enter the shift
value

encrypted_text = encrypt(text, shift) # Encrypt the entered text


decrypted_text = decrypt(encrypted_text, shift) # Decrypt the encrypted text

# Display the encrypted and decrypted text to the user


print("Encrypted text:", encrypted_text)
print("Decrypted text:", decrypted_text)

# Check if the script is executed as the main program


if __name__ == "__main__":
main() # Call the main function to start the program

6. Algorithim Used
Two algorithims have been used in the python script to perfom the ceasar cipher
function.The algorithims are : a)Encryption algorithim b)Decryption algorithim
a)Encryption Algorithm:
- Iterates through each character, determines its case, and applies the Caesar
Cipher encryption formula.
- Non-alphabetic characters are unaffected.

b)Decryption Algorithm:
- Reuses the encryption function with a negative shift for decryption.

7. Conclusion:
- The Python script successfully implements the Caesar Cipher algorithm, providing a
user-friendly interface for encrypting and decrypting text. The objectives of the script
are achieved, and the script demonstrates the flexibility and simplicity of Python in
implementing cryptographic algorithms.

You might also like