-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar.py
130 lines (103 loc) · 2.43 KB
/
caesar.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from __future__ import print_function
import re
import random
# Initial Basic Caesar Cipher Cryptography Program
# Alphabet
alphabet = [
"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y",
"Z",
]
letters = {
"A": 0, "B": 1, "C": 2, "D": 3,
"E": 4, "F": 5, "G": 6, "H": 7,
"I": 8, "J": 9, "K": 10, "L": 11,
"M": 12, "N": 13, "O": 14, "P": 15,
"Q": 16, "R": 17, "S": 18, "T": 19,
"U": 20, "V": 21, "W": 22, "X": 23,
"Y": 24, "Z": 25,
}
def encrypt_value(value):
user_input = value.upper()
decrypted = None
# Check for numbers in the sequence to remove
#NOTE: Need to remove any special, non-alphabetic characters from user input
if user_input.isalpha():
decrypted = user_input
else:
decrypted = re.sub(r'[^A-Za-z]', "", user_input)
print(decrypted)
# Perform the encryption
#NOTE: Modify to be function that can support encryption and error handling
encrypt_list = []
# randomize the shifter once for entire string
shifter = random.randint(1,9)
# coin flip a negative or positive shifter operation
direction = random.randint(0,1)
if direction:
for el in decrypted:
index = letters[el] + shifter
try:
el = alphabet[index]
except IndexError:
if index > 26:
index - 26
else:
index + 26
encrypt_list.append(el)
else:
for el in decrypted:
index = letters[el] - shifter
try:
el = alphabet[index]
except IndexError:
if index > 26:
index - 26
else:
index + 26
encrypt_list.append(el)
encrypted = "".join(encrypt_list)
print("ENCRYPTION: ", encrypted)
# decrypt_value(
# value=encrypted,
# shifter=shifter,
# direction=direction
# )
return encrypted
def decrypt_value(value, shifter, direction):
user_input = value.upper()
encrypted = None
if user_input.isalpha():
encrypted = user_input
else:
encrypted = re.sub(r'\d', "", user_input)
print(encrypted)
decrypt_list = []
if direction:
for el in encrypted:
index = letters[el] - shifter
try:
el = alphabet[index]
except IndexError:
if index > 26:
index - 26
else:
index + 26
decrypt_list.append(el)
else:
for el in encrypted:
index = letters[el] + shifter
try:
el = alphabet[index]
except IndexError:
if index > 26:
index - 26
else:
index + 26
decrypt_list.append(el)
decrypted = "".join(decrypt_list)
print("DECRYPTION: ", decrypted)
return decrypted