This script implements the Caesar Cipher encryption and decryption algorithm. It provides functionality to encrypt and decrypt text using a specified shift value, as well as a user-friendly interface for interactive use.
- Encrypt text using a specified shift value
- Decrypt text using a specified shift value
- Interactive command-line interface for ease of use
The script can be run from the command line with the following syntax:
python caesar_cipher.py
Once the script is running, you will be presented with a menu:
- Choose 'e' for encryption or 'd' for decryption
- Enter the text to be processed
- Enter the shift value (an integer)
- View the result
- Choose whether to continue or exit
-
Encrypt a string:
Do you want to encrypt or decrypt? (Enter 'e' for encrypt, 'd' for decrypt, 'q' to quit): e Enter the text: Hello, World! Enter the shift value: 3 Encrypted text: Khoor, Zruog!
-
Decrypt a string:
Do you want to encrypt or decrypt? (Enter 'e' for encrypt, 'd' for decrypt, 'q' to quit): d Enter the text: Khoor, Zruog! Enter the shift value: 3 Decrypted text: Hello, World!
encrypt(text: str, shift: int) -> str
: Function that performs the Caesar Cipher encryption on the input text.decrypt(text: str, shift: int) -> str
: Function that performs the Caesar Cipher decryption on the input text.get_integer_input(prompt: str) -> int
: Utility function to ensure valid integer input for the shift value.main() -> None
: Entry point of the script, handles the interactive mode and user input.
The encrypt()
function implements the Caesar Cipher algorithm as follows:
- Iterate through each character in the input text.
- If the character is a letter: a. Determine the base ASCII value ('A' for uppercase, 'a' for lowercase). b. Apply the shift: (ASCII value - base + shift) % 26 + base c. Convert the resulting ASCII value back to a character.
- If the character is not a letter, leave it unchanged.
- Return the resulting encrypted text.
The decrypt()
function simply calls encrypt()
with a negative shift value.
- The script processes each character individually, which is efficient for the Caesar Cipher's simplicity.
- The modulo operation ensures that the shift wraps around the alphabet correctly.
- The current implementation only works with the 26 letters of the English alphabet.
- It does not handle different character sets or languages.
- The script could be extended to accept command-line arguments for non-interactive use.
- File input/output functionality could be added to process larger texts.
- The Caesar Cipher is a very simple encryption method and is not secure for sensitive information.
- It is vulnerable to frequency analysis and brute-force attacks.
- This implementation is for educational purposes only and should not be used for securing sensitive data.
The script uses only Python standard library modules:
typing
: For type hinting (Python 3.5+)
No additional installations are required beyond a standard Python environment.