-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceasar_cipher.py
35 lines (27 loc) · 852 Bytes
/
ceasar_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
plain_text = input("Enter text to encrypt:")
shift_key = int(input("Enter shift value:"))
def encrypt(plain_text, shift_key):
result = ""
for i in range(len(plain_text)):
ch = plain_text[i]
#Encrypt uppercase
if ch.isupper():
result += chr(( ord(ch) + shift_key - 65) % 26 + 65)
#Encrypt lowercase
else:
result += chr( (ord(ch) + shift_key - 97) % 26 + 97)
return result
print(encrypt(plain_text, shift_key))
cipher_text = input("Enter cipher text to decrypt:")
shift_key = int(input("Enter shift key:"))
def decrypt(cipher_text, shift_key):
plain_text = ""
for i in range(len(cipher_text)):
ch = cipher_text[i]
if ch.isalpha():
if ch.isupper():
plain_text += chr((ord(ch)-shift_key-65)%26+65)
else:
plain_text += chr((ord(ch)-shift_key-97)%26+97)
return plain_text
print(decrypt(cipher_text,shift_key))