-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCipher.py
More file actions
30 lines (25 loc) · 912 Bytes
/
Copy pathCipher.py
File metadata and controls
30 lines (25 loc) · 912 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
30
# Caesar Cipher Functions
def encrypt(text, shift):
result = ""
for char in text:
if char.isalpha():
base = ord('A') if char.isupper() else ord('a')
result += chr((ord(char) - base + shift) % 26 + base)
else:
result += char # Keep non-letters unchanged
return result
def decrypt(text, shift):
return encrypt(text, -shift) # Just reverse the shift
# --- Main Program ---
print("=== Caesar Cipher ===")
choice = input("Do you want to (E)ncrypt or (D)ecrypt? ").strip().upper()
message = input("Enter your message: ")
shift = int(input("Enter shift number (e.g., 3): "))
if choice == 'E':
encrypted = encrypt(message, shift)
print("Encrypted message:", encrypted)
elif choice == 'D':
decrypted = decrypt(message, shift)
print("Decrypted message:", decrypted)
else:
print("Invalid choice.")