-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
213 lines (171 loc) · 6.25 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import math
import tkinter as ui
import plistlib
from _ctypes import Array
from tkinter.filedialog import askopenfilename, asksaveasfilename
import keyword
# There are variables of key and message.
global cryptKey
global message
encrypted = True
# processedMessage = [] # Unused in final list
# Resulting message variable
returningMessage = ""
listOfChars = [chr(ch) for ch in (range(ord('a'), ord('z') + 1))]
def open_file():
# Open file
filepath = askopenfilename(
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
)
if not filepath:
return
print("File: ")
print(filepath)
# Putting a message from file to global variable
global message
with open(filepath, "r") as input_file:
text = input_file.read()
message = text
print(text)
def open_key():
# Open file
filepath = askopenfilename(
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
)
if not filepath:
return
print("Key: ")
print(filepath)
# Putting a key from file to global variable
global cryptKey
with open(filepath, "r") as input_file:
text = input_file.read()
cryptKey = text
print(text)
def cut_by_chars(text):
for char in text:
return char
def cut_by_blocks(key, text):
pass
def select_vernam_char(key, text, count):
# Set current char to variable
processedChar = text[count].lower()
# Linking to global variable
global returningMessage
# Checking char, is it alpha?
if (text[count]).isalpha():
if ord('z') >= ord(text[count].lower()) >= ord('a'):
# Encrypted char = (basic char + char of key) % size of [a..z] list
processedChar = chr((((ord(processedChar) - 97) + (ord(key[count % len(key)]) - 97)) % 26) + 97)
returningMessage = returningMessage + processedChar
print(
text[count].lower(), ord(text[count].lower()) - 97, " ",
key[count % len(key)], ord(key[count % len(key)]) - 97, " ",
processedChar, ord(processedChar) - 97, " ",
count
)
else:
returningMessage = returningMessage + processedChar
return
else:
returningMessage = returningMessage + processedChar
return
def select_vernam_char_inv(key, text, count):
# Set current char to variable
processedChar = text[count].lower()
# Linking to global variable
global returningMessage
# Checking char, is it alpha?
if (text[count]).isalpha():
# Is alpha contained in [a..z] list? checking by ASCII codes
if ord('z') >= ord(text[count].lower()) >= ord('a'):
print((ord(key[count % len(key)]) - 97) % 26)
# Decrypted char = (encrypted char - char of key) % size of [a..z] list
processedChar = chr((((ord(processedChar) - 97) - (ord(key[count % len(key)]) - 97)) % 26) + 97)
returningMessage = returningMessage + processedChar
print(
text[count].lower(), ord(text[count].lower()) - 97, " ",
key[count % len(key)], ord(key[count % len(key)]) - 97, " ",
processedChar, ord(processedChar) - 97, " ",
count
)
else:
returningMessage = returningMessage + processedChar
return
else:
returningMessage = returningMessage + processedChar
return
def operate_and_save():
global message
global cryptKey
# Now check, what should program do
if (encrypted == False):
# Here we're encrypting message
for i in range(len(message)):
select_vernam_char(cryptKey, message, i)
else:
# Here we're performing inversion operations
for i in range(len(message)):
select_vernam_char_inv(cryptKey, message, i)
# Create new file
filepath = asksaveasfilename(
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
)
if not filepath:
return
print("File: ")
print(filepath)
# Put our performed message in a file
with open(filepath, "w") as output_file:
global returningMessage
output_file.write(returningMessage)
print(returningMessage)
# Now we should clear to not create duplication messages
returningMessage = ""
def showClickedBTWD1():
# It sets variable to False
global encrypted
encrypted = False
print("We're going to encrypt file.")
return
def showClickedBTWD2():
# It sets variable to True
global encrypted
encrypted = True
print("We're going to decrypt file.")
return
class mainWindow:
# Low skills in Python don't let me use this vars.
# It's unused block now.
isFileSelected = False
isKeySelected = False
isFileCrypted = False
# This block creates the window by methods of classes in Tkinter library.
window = ui.Tk()
window.columnconfigure(5, weight=1, minsize=25, pad=10)
window.rowconfigure([0, 5], weight=1, minsize=10, pad=10)
lblKeyInfo = ui.Label(text="Put key here:")
lblKeyInfo.grid(row=0, column=3)
lblFileInfo = ui.Label(text="Put file here:")
lblFileInfo.grid(row=1, column=3)
btnKeySelect = ui.Button(text="Select Key", command=open_key)
btnKeySelect.grid(row=0, column=4)
btnFileSelect = ui.Button(text="Select File", command=open_file)
btnFileSelect.grid(row=1, column=4)
# Here we are select our method of performing message.
# showClickedBTWDX are methods to operate on a global variable "encrypted".
btnWtWeDo1 = ui.Radiobutton(text="Encrypt", variable=isFileCrypted, value=True, command=showClickedBTWD1)
btnWtWeDo1.grid(row=0, column=2)
btnWtWeDo2 = ui.Radiobutton(text="Decrypt", variable=isFileCrypted, value=False, command=showClickedBTWD2)
btnWtWeDo2.grid(row=1, column=2)
# Final button calculates our message by operate_and_save method, which is declared over this class.
btnDo = ui.Button(text="Complete", command=operate_and_save)
btnDo.grid(row=2, column=3)
# lblKeyInfo.pack()
# lblFileInfo.pack()
window.mainloop()
if __name__ == '__main__':
# Create an object vernamWindow by class mainWindow
# which is declared upper lines.
vernamWindow = mainWindow()
print(cryptKey, message)