-
Notifications
You must be signed in to change notification settings - Fork 0
/
vernam_GUI.py
169 lines (151 loc) · 5.21 KB
/
vernam_GUI.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
import tkinter as tk
from vernam_def import *
from tkinter import filedialog as fd
#-----------------------------------------------------------------------
def decrypting(listCrypt, key):
msgNum = [a ^ b for a,b in zip(listCrypt, key)]
msgString = numList2msg(msgNum)
return msgString
#-----------------------------------------------------------------------
def keyGenBut():
ent_key_size.insert(tk.END,"\r")
try:
file_name = fd.asksaveasfilename(
filetypes=(("VSK files", "*.VSK"),
("All files", "*.*"))
)
f = open(file_name, 'w')
sizeKeyStr = ent_key_size.get()
ent_key_size.delete(0, tk.END)
sizeKey = "".join(c for c in sizeKeyStr if c.isdecimal())
sizeKey = int(sizeKey)
strText = list2str(keyGen(0,sizeKey))
f.write(strText)
f.close()
except TypeError:
return 1
except FileNotFoundError:
return 2
except ValueError:
return 3
def inputMsg():
try:
file_name = fd.askopenfilename(
filetypes=(("VSM files", "*.VSM"),
("TXT files", "*.TXT"),
("All files", "*.*"))
)
f = open(file_name)
s = f.read()
txt_msg.delete(1.0, tk.END)
txt_msg.insert(1.0, s)
f.close()
except FileNotFoundError:
return 1
except TypeError :
return 2
def outputMsg():
try:
file_name = fd.asksaveasfilename(
filetypes=(("VSM files", "*.VSM"),
("All files", "*.*"))
)
f = open(file_name, 'w')
Msg = txt_msg.get("1.0", tk.END)
f.write(Msg)
f.close()
except FileNotFoundError:
return 1
except TypeError :
return 2
def inputVer():
file_name = fd.askopenfilename(
filetypes=(("VSV files", "*.VSV"),
("All files", "*.*"))
)
f = open(file_name)
s = f.read()
ent_ver.delete(0, tk.END)
ent_ver.insert(0, s)
f.close()
def outputVer():
file_name = fd.asksaveasfilename(
filetypes=(("VSV files", "*.VSV"),
("All files", "*.*"))
)
f = open(file_name, 'w')
Msg = ent_ver.get()
f.write(Msg)
f.close()
def inputKey():
file_name = fd.askopenfilename(
filetypes=(("VSK files", "*.VSK"),
("All files", "*.*"))
)
f = open(file_name)
s = f.read()
ent_key.delete(0, tk.END)
ent_key.insert(0, s)
f.close()
def outputKey():
file_name = fd.asksaveasfilename(
filetypes=(("VSK files", "*.VSK"),
("All files", "*.*"))
)
f = open(file_name, 'w')
Msg = ent_key.get()
f.write(Msg)
f.close()
def crypting():
ent_ver.delete(0, tk.END)
MsgNum = msg2ver(txt_msg.get(1.0, tk.END))
keyNum = str2list(ent_key.get())
if len(keyNum) == 0:
keyNum = keyGen("", (len(MsgNum)-1))
keyNum.pop(0)
ent_key.delete(0, tk.END)
ent_key.insert(0, list2str(keyNum))
listCrypt = [a ^ b for a,b in zip(MsgNum, keyNum)]
strCrypt = list2str(listCrypt)
ent_ver.insert(0, strCrypt)
def decrypting():
txt_msg.delete(1.0, tk.END)
verNum = str2list(ent_ver.get())
keyNum = str2list(ent_key.get())
if len(keyNum) < len(verNum):
print("ключ не подходит!")
listDecrypt = [b ^ a for a,b in zip(verNum, keyNum)]
strDecrypt = ver2msg(listDecrypt)
txt_msg.insert(1.0, strDecrypt)
window = tk.Tk()
window.title("Vernam Encrypt")
window.geometry('400x600')
window.minsize(420,500)
window.maxsize(420,500)
lbl_msg = tk.Label(window, text="Message", font=(20)).grid(row=0, column=0,stick="w", padx=5,pady=5)
btn_in_msg = tk.Button(window, text="import", command=inputMsg).grid(row=0, column=1, padx=5,pady=5)
btn_out_msg = tk.Button(window, text="output",command=outputMsg).grid(row=0, column=2, padx=5,pady=5)
txt_msg = tk.Text(window, width=50, height=7)
txt_msg.grid(row=1, column=0, columnspan=3, stick="we", padx=5)
lbl_spaser = tk.Label(window, text=" ").grid(row=2, column=0, columnspan=3, stick="we", padx=5, pady=5)
lbl_ver = tk.Label(window, text="Chiphrogram", font=(20)).grid(row=3, column=0,stick="w", padx=5)
btn_in_ver = tk.Button(window, text="import", command=inputVer).grid(row=3, column=1)
btn_out_ver = tk.Button(window, text="output",command=outputVer).grid(row=3, column=2)
ent_ver = tk.Entry(window)
ent_ver.grid(row=4, column=0, columnspan=3, stick="we", padx=5, pady=5)
lbl_spaser = tk.Label(window, text=" ").grid(row=5, column=0, columnspan=3, stick="we", padx=5, pady=5)
lbl_key = tk.Label(window, text="Key", font=(20)).grid(row=6, column=0, stick="w", padx=5)
btn_in_key = tk.Button(window, text="import", command=inputKey).grid(row=6, column=1)
btn_out_key = tk.Button(window, text="output", command=outputKey).grid(row=6, column=2)
ent_key = tk.Entry(window)
ent_key.grid(row=7, column=0, columnspan=3, stick="we", padx=5, pady=5)
btn_crypt = tk.Button(window, text="Crypt", relief=tk.RAISED, borderwidth=5,width=10, height=1, command=crypting).grid(row=8, column=1, padx=10, pady=5)
btn_decrypt = tk.Button(window, text="Decrypt", relief=tk.RAISED, borderwidth=5,width=10, height=1, command=decrypting).grid(row=8, column=2, padx=10, pady=5)
lbl_spaser = tk.Label(window, text=" ", font=(20)).grid(row=9, column=0, columnspan=3, stick="we")
lbl_key = tk.Label(window, text="Key size", font=(5)).grid(row=10, column=0, stick="we", padx=5)
ent_key_size = tk.Entry(window,width=15)
ent_key_size.grid(row=10, column=1, padx=5)
btn_keygen = tk.Button(window, text="Key Gen", command=keyGenBut).grid(row=10, column=2,padx=5)
#-----------------------------------------------------------------------
print(str2list("123.123.44.xer.r34.56"))
window.mainloop()