-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCeaser_Cipher.py
More file actions
39 lines (33 loc) · 1.09 KB
/
Ceaser_Cipher.py
File metadata and controls
39 lines (33 loc) · 1.09 KB
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
31
32
33
34
35
36
37
38
39
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
return result
def decrypt(text, shift):
return encrypt(text, -shift)
def main():
print("=== Caesar Cipher Tool ===")
choice = input("Do you want to (E)ncrypt or (D)ecrypt? ").strip().upper()
if choice not in ['E', 'D']:
print("Invalid choice. Please select E or D.")
return
message = input("Enter your message: ")
try:
shift = int(input("Enter shift value (0-25): "))
if not (0 <= shift <= 25):
raise ValueError
except ValueError:
print("Invalid shift value. Please enter an integer between 0 and 25.")
return
if choice == 'E':
encrypted = encrypt(message, shift)
print("Encrypted message:", encrypted)
else:
decrypted = decrypt(message, shift)
print("Decrypted message:", decrypted)
if __name__ == "__main__":
main()