-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesar Cipher.py
37 lines (33 loc) · 1.33 KB
/
Caesar Cipher.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
def caesar_cipher_encrypt(text, 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):
decrypted_text = ""
for char in text:
if char.isalpha():
shift_base = 65 if char.isupper() else 97
decrypted_text += chr((ord(char) - shift_base - shift) % 26 + shift_base)
else:
decrypted_text += char
return decrypted_text
def main():
print("Welcome to the Caesar Cipher program!")
choice = input("Do you want to (e)ncrypt or (d)ecrypt a message? ")
message = input("Enter your message: ")
shift = int(input("Enter the shift value: "))
if choice.lower() == 'e':
encrypted_message = caesar_cipher_encrypt(message, shift)
print(f"Encrypted message: {encrypted_message}")
elif choice.lower() == 'd':
decrypted_message = caesar_cipher_decrypt(message, shift)
print(f"Decrypted message: {decrypted_message}")
else:
print("Invalid choice. Please enter 'e' to encrypt or 'd' to decrypt.")
if __name__ == "__main__":
main()