-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.py
More file actions
47 lines (44 loc) · 1.41 KB
/
caesar.py
File metadata and controls
47 lines (44 loc) · 1.41 KB
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
41
42
43
44
45
46
47
##############################################################################
# The program can encrypt or decrypt chars from the keyboard
##############################################################################
rot = int(input("Enter rotation: "))
action = input("Do you want [e]ncrypt or [d]ecrypt data?: ")
data = input("Enter text: ")
start, end = 32, 126 # chars in ascii for caesar cipher
divide = end - start
if action == "e" or action == "encrypt":
text = ""
for char in data:
char_c = ord(char)
if start <= char_c <= end:
char_c -= start
char_c += rot # encrypt
char_c %= divide
char_c += start
text += chr(char_c)
else:
text += char
# second solution
# symbol = ord(char) + rot
# if symbol > 122:
# symbol = symbol - 122
# symbol = symbol + 64
#
# symbol = chr(symbol)
# text += symbol
print("Text '{}' => '{}'".format(data, text))
elif action == "d" or action == "decrypt":
text = ""
for char in data:
char_c = ord(char)
if start <= char_c <= end:
char_c -= start
char_c -= rot # decrypt
char_c %= divide
char_c += start
text += chr(char_c)
else:
text += char
print("Text '{}' => '{}'".format(data, text))
else:
print("Error with input")