-
Notifications
You must be signed in to change notification settings - Fork 0
/
Implement Caesar Cipher
42 lines (41 loc) · 1.45 KB
/
Implement Caesar Cipher
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
# Function to encrypt text using Caesar Cipher
def encrypt(text, shift):
result = ""
# Traverse each character in the text
for char in text:
# Encrypt uppercase characters
if char.isupper():
result += chr((ord(char) + shift - 65) % 26 + 65)
# Encrypt lowercase characters
elif char.islower():
result += chr((ord(char) + shift - 97) % 26 + 97)
else:
# Keep non-alphabet characters as they are
result += char
return result
# Function to decrypt text using Caesar Cipher
def decrypt(text, shift):
result = ""
# Traverse each character in the text
for char in text:
# Decrypt uppercase characters
if char.isupper():
result += chr((ord(char) - shift - 65) % 26 + 65)
# Decrypt lowercase characters
elif char.islower():
result += chr((ord(char) - shift - 97) % 26 + 97)
else:
# Keep non-alphabet characters as they are
result += char
return result
# Main function to get user input
def caesar_cipher():
# Get user input
text = input("Enter the message: ")
shift = int(input("Enter the shift value: "))
# Perform encryption and decryption
encrypted_message = encrypt(text, shift)
decrypted_message = decrypt(encrypted_message, shift)
print(f"\nEncrypted Message: {encrypted_message}")
print(f"Decrypted Message: {decrypted_message}")
caesar_cipher()