-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.Caesar_Cipher.py
More file actions
25 lines (23 loc) · 866 Bytes
/
Copy path10.Caesar_Cipher.py
File metadata and controls
25 lines (23 loc) · 866 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
def encrypt(message,key):
result = ""
for char in message:
if char.isalpha():
base = ord('A') if char.isupper() else ord('a')
shifted = (ord(char)-base+key) % 26 + base
result += chr(shifted)
else:
result += char
return result
def decrypt(message,key):
return encrypt(message,-key)
choice = input("Do you want to encrypt or decrypt a message?").strip().lower()
if choice == 'e':
msg = input("Enter the message to encrypt: ")
k = int(input("Enter the key (shift value): "))
print("Encrypted message:", encrypt(msg, k))
elif choice == 'd':
msg = input("Enter the message to decrypt: ")
k = int(input("Enter the key (shift value): "))
print("Decrypted message:", decrypt(msg, k))
else:
print("Invalid choice. Please enter 'e' for encrypt or 'd' for decrypt.")