It can encrypt and decrypt the message
PYTHON CODE:
def caesar_encrypt(plaintext, shift): """Encrypts the plaintext using Caesar Cipher with a given shift.""" encrypted_text = "" for char in plaintext: if char.isalpha(): # Process only alphabetic characters # Determine ASCII base (97 for lowercase, 65 for uppercase) ascii_base = 65 if char.isupper() else 97 # Shift the character and wrap around the alphabet (mod 26) encrypted_char = chr((ord(char) - ascii_base + shift) % 26 + ascii_base) encrypted_text += encrypted_char else: # Keep non-alphabetic characters unchanged encrypted_text += char return encrypted_text
def caesar_decrypt(ciphertext, shift): """Decrypts the ciphertext using Caesar Cipher with a given shift.""" # Decryption is just encryption with the negative shift return caesar_encrypt(ciphertext, -shift)
def main(): print("Caesar Cipher Encryption/Decryption Tool") while True: choice = input("\nChoose an option (1: Encrypt, 2: Decrypt, 3: Exit): ")
if choice == '3':
print("Exiting program.")
break
if choice not in ['1', '2']:
print("Invalid choice! Please select 1, 2, or 3.")
continue
# Get user input
message = input("Enter the message: ")
try:
shift = int(input("Enter the shift value (1-25): "))
if shift < 1 or shift > 25:
print("Shift value must be between 1 and 25.")
continue
except ValueError:
print("Invalid input! Shift must be a number.")
continue
# Perform encryption or decryption
if choice == '1':
result = caesar_encrypt(message, shift)
print(f"Encrypted message: {result}")
else:
result = caesar_decrypt(message, shift)
print(f"Decrypted message: {result}")
if name == "main": main()
OUTPUT:
PS C:\Users\user.vscode> & C:/Users/user/.vscode/.venv/Scripts/python.exe c:/Users/user/.vscode/.vscode/caesar.py Caesar Cipher Encryption/Decryption Tool
Choose an option (1: Encrypt, 2: Decrypt, 3: Exit): 1 Enter the message: how are you Enter the shift value (1-25): 4 Encrypted message: lsa evi csy
Choose an option (1: Encrypt, 2: Decrypt, 3: Exit): 3 Exiting program.
OUTPUT:(2)
PS C:\Users\user.vscode> & C:/Users/user/.vscode/.venv/Scripts/python.exe c:/Users/user/.vscode/.vscode/caesar.py Caesar Cipher Encryption/Decryption Tool
Choose an option (1: Encrypt, 2: Decrypt, 3: Exit): 2 Enter the message: lsa evi csy Enter the shift value (1-25): 4 Decrypted message: how are you
Choose an option (1: Encrypt, 2: Decrypt, 3: Exit): 3 Exiting program.