-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.py
More file actions
25 lines (24 loc) · 767 Bytes
/
caesar.py
File metadata and controls
25 lines (24 loc) · 767 Bytes
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
def caesarEncrypt(plaintext, key):
ciphertext = ""
for char in plaintext:
if char.islower():
shift = (ord(char) - 97 + key) % 26 + 97
ciphertext += chr(shift)
elif char.isupper():
shift = (ord(char) - 65 + key) % 26 + 65
ciphertext += chr(shift)
else:
ciphertext += char
return ciphertext
def caesarDecrypt(ciphertext, key):
plaintext = ""
for char in ciphertext:
if char.islower():
shift = (ord(char) - 97 - key) % 26 + 97
plaintext += chr(shift)
elif char.isupper():
shift = (ord(char) - 65 - key) % 26 + 65
plaintext += char
else:
plaintext += char
return plaintext