top of page

Caesar Cipher


What is a Caesar Cipher?

The Caesar Cipher is a simple encryption method used by the ancient Roman Emperor Julius Caesar. This method involves shifting each letter in the alphabet by a certain number of positions. For example, you can encrypt a letter by shifting it three positions to the right: A -> D, B -> E, C -> F, and so on. This type of encryption is used to increase information security and is a fundamental concept in the world of cryptography.


Project Objectives and Student Benefits

Through this project, students will reinforce their ability to develop simple and functional programs using basic Python knowledge. Additionally, they will gain experience in encryption and data processing.


Project Outputs

  • Create an encryption application using basic Python structures and control flows.

  • Collect data from users and manage encryption and decryption processes by processing this data.

  • Write more modular and readable code using functions.


Step-by-Step Tasks of the Project

  1. Collect input from the user for encryption or decryption.

  2. Collect the shift amount and text from the user.

  3. Perform the encryption or decryption process.

  4. Print the result.

  5. Make the code more modular using functions.


Solution Without Functions


Step 1: Collect Input from the User for Encryption or Decryption

In the first step, input will be collected from the user for encryption or decryption.

# Collect input from the user for encryption or decryption
operation = input("Choose encryption (e) or decryption (d): ").lower()
shift = int(input("Enter the shift amount: "))
text = input("Enter the text: ")

This code snippet collects input from the user for encryption or decryption.


Step 2: Perform the Encryption or Decryption Process

In the second step, the selected process will be performed.

# Perform the encryption or decryption process
result = ""
for char in text:
    if char.isalpha():
        ascii_offset = 65 if char.isupper() else 97
        if operation == 'e':
            new_char = chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset)
        elif operation == 'd':
            new_char = chr((ord(char) - ascii_offset - shift) % 26 + ascii_offset)
        result += new_char
    else:
        result += char

This code snippet performs the encryption or decryption process and creates the result.


Step 3: Print the Result

In the third step, the calculated result will be printed.

# Print the result
print("Result:", result)

This code snippet prints the calculated result.


Solution with Functions


Step 1: Function to Collect Input from the User for Encryption or Decryption

In the first step, input will be collected from the user for encryption or decryption.

def collect_input():
    """Collects input from the user for encryption or decryption."""
    operation = input("Choose encryption (e) or decryption (d): ").lower()
    shift = int(input("Enter the shift amount: "))
    text = input("Enter the text: ")
    return operation, shift, text

This function collects and returns input from the user for encryption or decryption.


Step 2: Function to Perform the Encryption or Decryption Process

In the second step, the selected process will be performed.

def caesar_cipher(operation, shift, text):
    """Performs the encryption or decryption process."""
    result = ""
    for char in text:
        if char.isalpha():
            ascii_offset = 65 if char.isupper() else 97
            if operation == 'e':
                new_char = chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset)
            elif operation == 'd':
                new_char = chr((ord(char) - ascii_offset - shift) % 26 + ascii_offset)
            result += new_char
        else:
            result += char
    return result

This function performs the encryption or decryption process and returns the result.


Step 3: Function to Print the Result

In the third step, the calculated result will be printed.

def print_result(result):
    """Prints the calculated result."""
    print("Result:", result)

This function prints the calculated result.


Step 4: Function to Start the Encryption Application

This function starts the Caesar Cipher application and manages its flow.

def start_encryption_application():
    """Starts the Caesar Cipher application."""
    operation, shift, text = collect_input()
    result = caesar_cipher(operation, shift, text)
    print_result(result)

# Start the application
start_encryption_application()

This function starts the Caesar Cipher application and manages its flow. It collects the necessary input from the user, performs the encryption or decryption process, and prints the result.


Concepts Used

  • Data Types: In this project, strings (user input, text) and integers (shift amount) are used.

  • Conditional Statements: if-else structures are used to determine the user-selected operation and perform the correct process.

  • Functions: Functions make the code modular and reusable.

  • Loops: A for loop is used to process each character in the text.

bottom of page