import random
def caesar_cipher(text, shift, mode):
result = ""
for char in text:
if char.isalpha(): # Encrypts only letters
shift_amount = shift if mode == "encrypt" else -shift
new_char = chr(((ord(char.lower()) - 97 + shift_amount) % 26) + 97)
result += new_char.upper() if char.isupper() else new_char
else:
result += char # Keeps spaces and punctuation unchanged
return result
# Get user input
mode = input("Do you want to encrypt or decrypt? ").strip().lower()
if mode not in ["encrypt", "decrypt"]:
print("Invalid mode! Please enter 'encrypt' or 'decrypt'.")
exit()
message = input("Enter your message: ")
shift_input = input("Enter shift value (or type 'random'): ").strip().lower()
# Generate a random shift if "random" is entered
if shift_input == "random":
shift = random.randint(1, 25)
else:
try:
shift = int(shift_input)
if not (1 <= shift <= 25):
raise ValueError # Ensures shift is within valid range
except ValueError:
print("Invalid shift value! Enter a number between 1 and 25 or type 'random'.")
exit()
print(f"Using shift value: {shift}") # Display the shift used
output = caesar_cipher(message, shift, mode)
print(f"Result: {output}")
Using shift value: 1
Result: ojhhfs