-
Notifications
You must be signed in to change notification settings - Fork 0
/
CS01.py
50 lines (43 loc) · 1.66 KB
/
CS01.py
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
40
41
42
43
44
45
46
47
48
49
50
#implmenting caesar cipher algorithm
def caesar_cipher_encrypt(text, shift):
"""
Encrypts the given text using Caesar Cipher with the specified shift.
"""
encrypted_text = ""
for char in text:
if char.isalpha():
shift_base = 65 if char.isupper() else 97
encrypted_text += chr((ord(char) - shift_base + shift) % 26 + shift_base)
else:
encrypted_text += char
return encrypted_text
def caesar_cipher_decrypt(text, shift):
"""
Decrypts the given text encrypted with Caesar Cipher by reversing the shift.
"""
return caesar_cipher_encrypt(text, -shift)
def main():
print("Welcome to the Caesar Cipher Program!")
while True:
print("\nMenu:")
print("1. Encrypt a message")
print("2. Decrypt a message")
print("3. Exit")
choice = input("Enter your choice (1/2/3): ")
if choice == '1':
message = input("Enter the message to encrypt: ")
shift = int(input("Enter the shift value: "))
encrypted_message = caesar_cipher_encrypt(message, shift)
print(f"Encrypted Message: {encrypted_message}")
elif choice == '2':
message = input("Enter the message to decrypt: ")
shift = int(input("Enter the shift value: "))
decrypted_message = caesar_cipher_decrypt(message, shift)
print(f"Decrypted Message: {decrypted_message}")
elif choice == '3':
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice! Please try again.")
if __name__ == "__main__":
main()