-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
121 lines (113 loc) · 4.88 KB
/
Main.py
File metadata and controls
121 lines (113 loc) · 4.88 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def cypher(sentence, offset):
while offset > 26:
offset -= 26
alphabetsoup = ["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", "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"]
sentence = sentence.casefold()
sentence = list(sentence)
for p in range(len(sentence)):
if sentence[p] == ' ':
continue
else:
sentence[p] = alphabetsoup[alphabetsoup.index(sentence[p]) + offset]
str1 = ""
return str1.join(sentence)
def decryptknown(sentence, offset):
while offset > 26:
offset -= 26
alphabetsoup = ["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", "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"]
realwords = ""
sentence = sentence.casefold()
sentence = list(sentence)
for p in range(len(sentence)):
if sentence[p - 1] == ' ':
continue
else:
sentence[p - 1] = alphabetsoup[alphabetsoup.index(sentence[p - 1]) - offset]
for y in range(len(sentence)):
realwords += sentence[y - 1]
realwords = list(realwords)
letter = realwords.pop(0)
realwords.append(letter)
str1 = ""
return str1.join(realwords)
def decrypt(sentence):
alphabetsoup = ["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", "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"]
sentence = sentence.casefold()
for x in range(26):
changing_sentence = sentence
changing_sentence = list(changing_sentence)
offset = x
for p in range(len(changing_sentence)):
if changing_sentence[p] == ' ':
continue
else:
changing_sentence[p] = alphabetsoup[alphabetsoup.index(changing_sentence[p]) + offset]
str1 = ""
print("offset = {}, {}".format(x + 1, str1.join(changing_sentence)))
def decryptadvanced(sentence):
alphabetsoup = ["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"]
goodwords = ['the', 'be', 'to', 'of', 'and', 'in', 'that', 'have', 'it', 'for', 'not', 'on', 'with', 'he',
'as', 'you', 'do']
listofsplitsentences = []
listofnonsplitsentences = []
str1 = ""
printed = False
sentence = sentence.casefold()
for offset in range(26):
changing_sentence = sentence
changing_sentence = list(changing_sentence)
for q in range(len(changing_sentence)):
changed_amount = 0
if changing_sentence[q] != ' ':
while alphabetsoup.index(changing_sentence[q]) + offset >= 26:
offset -= 26
changed_amount += 1
changing_sentence[q] = alphabetsoup[alphabetsoup.index(changing_sentence[q]) + offset]
for u in range(changed_amount):
offset += 26
print(sentence)
str1 = ""
listofnonsplitsentences.append("offset = {}, {}".format(offset, str1.join(changing_sentence)))
var2 = str1.join(changing_sentence)
var2 = var2.split()
listofsplitsentences.append(var2)
for r in range(len(listofsplitsentences)):
for h in range(len(listofsplitsentences[r])):
if listofsplitsentences[r][h] in goodwords:
listofsplitsentences[r] = ['{} '.format(elem) for elem in listofsplitsentences[r]]
print(str1.join(listofsplitsentences[r]))
printed = True
if not printed:
for repeat in range(len(listofnonsplitsentences)):
print(listofnonsplitsentences[repeat])
while True:
encode = input("Do you want to encode or decode? ")
if encode == "encode":
text = input("input sentence: ")
off = int(input("input offset: "))
print(cypher(text, off))
elif encode == "decode":
texts = input("input sentence: ")
known = input("Do you know the offset? ")
if known == "yes":
off = int(input("input offset: "))
words = decryptknown(texts, off)
print(words)
else:
yes = input("do you want to use advanced search? ")
if yes == 'yes':
decryptadvanced(texts)
else:
decrypt(texts)
else:
print("Unrecognised input")