-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar_cipher.py
More file actions
29 lines (23 loc) · 942 Bytes
/
Copy pathcaesar_cipher.py
File metadata and controls
29 lines (23 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def caesar_cipher(text, shift, mode):
result = ""
for char in text:
if char.isalpha(): # Process only letters
is_upper = char.isupper()
base = ord('A') if is_upper else ord('a')
if mode == "encrypt":
new_char = chr((ord(char) - base + shift) % 26 + base)
elif mode == "decrypt":
new_char = chr((ord(char) - base - shift) % 26 + base)
result += new_char
else:
result += char # Keep spaces and symbols unchanged
return result
# User input
message = input("Enter the message: ")
shift_value = int(input("Enter the shift value: "))
choice = input("Type 'encrypt' to encrypt or 'decrypt' to decrypt: ").lower()
if choice in ["encrypt", "decrypt"]:
output = caesar_cipher(message, shift_value, choice)
print(f"Result: {output}")
else:
print("Invalid choice! Please type 'encrypt' or 'decrypt'.")