-
Notifications
You must be signed in to change notification settings - Fork 17
/
Caesar_cipher.py
40 lines (29 loc) · 971 Bytes
/
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
40
#Caesar cipher: Encryption and Decryption
def encrypt(text,s):
result = ""
for i in range(len(text)):
char = text[i]
if char.isupper():
result += chr((ord(char) + s-65) % 26 + 65)
elif char.islower():
result += chr((ord(char) + s - 97) % 26 + 97)
else:
result += char
return result
def decrypt(text,s):
result = ""
for i in range(len(text)):
char = text[i]
if char.isupper():
result += chr((ord(char) - s-65 + 26) % 26 + 65)
elif char.islower():
result += chr((ord(char) - s - 97 + 26) % 26 + 97)
else:
result += char
return result
if __name__ == "__main__":
text = input("\nEnter the plaintext: ")
key = int(input("\nEnter the key: "))
ciphertest = encrypt(text,key)
print ("\nCipher: " + ciphertest)
print ("\nDecrypted: " + decrypt(ciphertest,key))