-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecryptor.py
More file actions
104 lines (87 loc) · 2.63 KB
/
decryptor.py
File metadata and controls
104 lines (87 loc) · 2.63 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
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
global alph
alph = ["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"]
puct = [",", ".", ";", ":", "'", "\""]
wordsFile = open("words_alpha.txt", "r")
wordsContent = wordsFile.read()
wordsList = wordsContent.split("\n")
wordsFile.close()
print(len(wordsList))
def getUserInput(sel):
if sel == 0:
print("What is the shift: ")
shift = input()
print("enter cypher: ")
cypher = input()
message = decypher(shift, cypher, 0)
print(message)
elif sel == 1:
print("What is the shift: ")
shift = input()
print("enter cypher: ")
cypher = input()
message = decypher(shift, cypher, 1)
print(message)
elif sel == 2:
print("Message to be decyphered: ")
cypher = input()
autodecrypt(cypher)
def crossCheck(list1, element):
x = 0
for i in list1:
x += 1
if i == element:
return x
def decypher(sh, cy, mode):
newList = []
newStr = "\n"
if mode == 0:
op = "-"
elif mode == 1:
op = "+"
for i in cy:
lowerCaseLetter = i.lower()
if lowerCaseLetter not in alph:
newList.append(i)
else:
initialnum = crossCheck(alph, lowerCaseLetter)
newNum = eval(str(initialnum) + op + sh)
if int(newNum) > 26:
wrappedNum = newNum - 26
newLetter = alph[wrappedNum - 1]
newList.append(newLetter)
else:
newLetter = alph[newNum - 1]
newList.append(newLetter)
for i in newList:
newStr += str(i)
return newStr
def autodecrypt(cy):
matchList = []
for i in range(0, 26):
listStr = []
finStr = "\n"
initDecypher = decypher(str(i), cy, 0)
for i in initDecypher:
lowerCaseLetter = i.lower()
if lowerCaseLetter in puct:
continue
else:
listStr.append(lowerCaseLetter)
for i in listStr:
finStr += str(i)
words = finStr.split()
x = 0
for i in words:
if i in wordsList:
x += 1
else:
continue
matchList.append(x)
shiftNum = max(matchList)
shift = matchList.index(shiftNum)
print("\nShift: " + str(shift))
message = decypher(str(shift), cy, 0)
print(message)
print("What would you like to do: \ndecrypt: 0\nEncrypt: 1\nauto-decrypt: 2")
userInput = input()
getUserInput(int(userInput))