-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcesar.py
executable file
·32 lines (31 loc) · 982 Bytes
/
cesar.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
rot = int(input('Enter the key: '))
action = input('Would do you [e]ncrypting or [d]ecrypting?: ')
data = input('Get text: ')
if action == 'e' or action == 'encrypting':
text = ''
for char in data:
char_ord = ord(char)
if 32 <= char_ord <= 126: # create alphabet
char_ord -= 32
char_ord += rot # rotate into the right == encrypting
char_ord %= 94
char_ord += 32
text += chr(char_ord)
else:
text += char
print("Code: '{}'".format(text))
elif action == 'd' or action == 'decrypting':
text = ''
for char in data:
char_ord = ord(char)
if 32 <= char_ord <= 126:
char_ord -= 32
char_ord -= rot # rotate into the left == decrypting
char_ord %= 94
char_ord += 32
text += chr(char_ord)
else:
text += char
print("Text: '{}'".format(text))
else:
print('Error. Try again.')