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
Implement the Caesar Encryption
In this step, you will implement the Caesar Encryption function.
Open the
caesar_cipher.pyfile.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.
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.
- Add the following code at the end of the
caesar_cipher.pyfile:
if __name__ == "__main__":
test_input_text = input()
print(caesar_encryption(test_input_text))
- Save the
caesar_cipher.pyfile. - Open a terminal and navigate to the
/home/labex/projectdirectory. - Run the
caesar_cipher.pyscript using the following command:
python3 caesar_cipher.py
- 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.
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.
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
- 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.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



