-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesar_Cipher.py
39 lines (37 loc) · 1.22 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
38
39
def encrypt_caesar(plaintext, shift):
encrypted_text = ""
for char in plaintext:
if char.isalpha():
shifted = ord(char) + shift
if char.islower():
if shifted > ord('z'):
shifted -= 26
elif char.isupper():
if shifted > ord('Z'):
shifted -= 26
encrypted_text += chr(shifted)
else:
encrypted_text += char
return encrypted_text
def decrypt_caesar(ciphertext, shift):
decrypted_text = ""
for char in ciphertext:
if char.isalpha():
shifted = ord(char) - shift
if char.islower():
if shifted < ord('a'):
shifted += 26
elif char.isupper():
if shifted < ord('A'):
shifted += 26
decrypted_text += chr(shifted)
else:
decrypted_text += char
return decrypted_text
# Example usage:
plaintext = input("Enter your text ... ")
shift = int(input("Enter no of shifts ... "))
encrypted_text = encrypt_caesar(plaintext, shift)
print("Encrypted:", encrypted_text)
decrypted_text = decrypt_caesar(encrypted_text, shift)
print("Decrypted:", decrypted_text)