forked from lightski/caesar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar_decrypt.py
38 lines (35 loc) · 1.08 KB
/
caesar_decrypt.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
'''
caesar_decrypt.py
given key and caesar ciphertext translate into plaintext
'''
import string, sys
def decrypt(key, ciphertext):
'''
params: char key - offset of A
string ciphertext - encoded message to be read
desc: decrypt message by reversing offset of each letter by key
'''
shift = ord(key.upper()) - 65
plaintext = ""
for c in ciphertext.upper():
if ord(c) != ord(" "):
nchar = chr(ord(c) - shift)
if ord(nchar) < 65:
nchar = chr(ord(nchar) + 26)
elif ord(nchar) > 90:
nchar = chr(ord(nchar) - 26)
plaintext += nchar
else:
plaintext += " "
return plaintext
if __name__ == "__main__":
try:
# automatic mode: get key and ciphertext from commandline options
key = sys.argv[1]
ciphertext = sys.argv[2]
except:
# interactive mode: prompt user
key = input("Key (char); 'A'= ")
ciphertext = inpt("Ciphertext: ")
plaintext = decrypt(key, ciphertext)
print(plaintext)