Skip to content

Commit 45575bc

Browse files
committed
Initial commit
0 parents  commit 45575bc

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

caesar_cipher.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#Caeser Cipher Algorithm for encryption and decryption
2+
3+
def caesar_encryption(plaintext, key):
4+
encrypted_str = ""
5+
for i in plaintext:
6+
if i.isupper():
7+
uni_value = 65 + ((ord(i) - 65 + key) % 26)
8+
encrypted_str = encrypted_str + chr(uni_value)
9+
elif i.islower():
10+
uni_value = 97 + ((ord(i) - 97 + key) % 26)
11+
encrypted_str = encrypted_str + chr(uni_value)
12+
else:
13+
encrypted_str = encrypted_str + i
14+
15+
print("The encrypted text is:", encrypted_str)
16+
17+
plaintext = input("Enter the text: ")
18+
key = int(input("Enter the key: "))
19+
caesar_encryption(plaintext, key)
20+
21+
def caesar_decryption(ciphertext, key):
22+
decrypted_str = ""
23+
for i in ciphertext:
24+
if i.isupper():
25+
if (ord(i) - 65 - key) < 0:
26+
uni_value = 65 + ((ord(i) - 65 - key) + 26) % 26
27+
decrypted_str = decrypted_str + chr(uni_value)
28+
else:
29+
uni_value = 65 + (ord(i) - 65 - key) % 26
30+
decrypted_str = decrypted_str + chr(uni_value)
31+
elif i.islower():
32+
if (ord(i) - 97 - key) < 0:
33+
uni_value = 97 + ((ord(i) - 97 - key) + 26) % 26
34+
decrypted_str = decrypted_str + chr(uni_value)
35+
else:
36+
uni_value = 97 + (ord(i) - 97 - key) % 26
37+
decrypted_str = decrypted_str + chr(uni_value)
38+
else:
39+
decrypted_str = decrypted_str + i
40+
41+
print("The decrypted text is:", decrypted_str)
42+
43+
ciphertext = input("Enter the encrypted text: ")
44+
key = int(input("Enter the key:"))
45+
caesar_decryption(ciphertext, key)

0 commit comments

Comments
 (0)