Implementing Caesar Cipher Encryption

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to implement the Caesar Cipher, a simple and widely known encryption technique. The Caesar Cipher uses a substitution method to replace English characters in a message with the character N positions later or earlier in the alphabet sequence.

👀 Preview

## Example 1
text = "python"; encryption_text = "ravjqp"
## Example 2
text = "pyThon"; encryption_text = "raVjqp"
## Example 3
text = "Python31"; encryption_text = "Ravjqp31"
## Example 4
text = None; encryption_text = None

ðŸŽŊ Tasks

In this project, you will learn:

  • How to set up the project environment and create the necessary files
  • How to implement the Caesar Encryption function to shift every English character in a string 2 positions to the right
  • How to handle different types of input, including English characters, non-English characters, and null values
  • How to test the Caesar Encryption function with various input examples

🏆 Achievements

After completing this project, you will be able to:

  • Understand the basic principles of the Caesar Cipher encryption technique
  • Implement a Caesar Encryption function in Python without using any standard or third-party libraries
  • Develop skills in string manipulation, character encoding, and conditional logic
  • Test your code to ensure it meets the project requirements

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/variables_data_types -.-> lab-302693{{"`Implementing Caesar Cipher Encryption`"}} python/strings -.-> lab-302693{{"`Implementing Caesar Cipher Encryption`"}} python/conditional_statements -.-> lab-302693{{"`Implementing Caesar Cipher Encryption`"}} python/for_loops -.-> lab-302693{{"`Implementing Caesar Cipher Encryption`"}} python/function_definition -.-> lab-302693{{"`Implementing Caesar Cipher Encryption`"}} python/build_in_functions -.-> lab-302693{{"`Implementing Caesar Cipher Encryption`"}} end

Implement the Caesar Encryption

In this step, you will implement the Caesar Encryption function.

  1. Open the caesar_cipher.py file.

  2. Modify the caesar_encryption(text) function to achieve the desired functionality:

    • Input a string and shift every English character in the string 2 positions to the right.
    • Only convert half-width English characters, leave other content unchanged.
    • The case of the characters should remain unchanged, for example, A will be converted to C, and b will be converted to d.
  3. Replace the """TODO""" comment with the following code:

def caesar_encryption(text: str) -> str:
    if text is None:
        return None
    encryption_text = ""
    for char in text:
        if char.isalpha() and char.isascii():
            if char.islower():
                encryption_text += chr((ord(char) - ord("a") + 2) % 26 + ord("a"))
            else:
                encryption_text += chr((ord(char) - ord("A") + 2) % 26 + ord("A"))
        else:
            encryption_text += char
    return encryption_text

This code implements the Caesar Encryption algorithm as described in the project requirements.

Test the Caesar Encryption

In this step, you will test the Caesar Encryption function with some example inputs.

  1. Add the following code at the end of the caesar_cipher.py file:
if __name__ == "__main__":
    test_input_text = input()
    print(caesar_encryption(test_input_text))
  1. Save the caesar_cipher.py file.
  2. Open a terminal and navigate to the /home/labex/project directory.
  3. Run the caesar_cipher.py script using the following command:
python3 caesar_cipher.py
  1. Enter some test input text and observe the encrypted output.

Verify the Requirements

In this step, you will verify that your implementation meets the project requirements.

  1. Review the project requirements and ensure that your caesar_encryption(text) function:

    • Inputs a string and shifts every English character in the string 2 positions to the right.
    • Only converts half-width English characters, leaving other content unchanged.
    • Preserves the case of the characters, for example, A is converted to C, and b is converted to d.
  2. Test your function with the provided reference examples:

## Example 1
text = "python"; encryption_text = "ravjqp"
## Example 2
text = "pyThon"; encryption_text = "raVjqp"
## Example 3
text = "Python31"; encryption_text = "Ravjqp31"
## Example 4
text = None; encryption_text = None
  1. Ensure that your function correctly handles all the test cases.

Congratulations! You have completed the Caesar Cipher project. If you have any questions or need further assistance, please don't hesitate to ask.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like