coding utf-8 from Tkinter import from tkFileDialog import import paszi

  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
# coding=utf-8
from Tkinter import *
from tkFileDialog import *
import paszi
import tkMessageBox
import os
class MainWin(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.master = master
self.master.title('RSA program')
self.master.geometry("500x450+450+100")
self.master.minsize(500, 450)
self.file_enter = Entry(font="Arial 16")
self.file_enter.pack(side='top')
self.file_input = StringVar()
self.file_enter["textvariable"] = self.file_input
self.a = []
self.b = []
self.file_button = Button(self.master, text=u'Загрузить файл', font="Arial 16")
self.file_button.pack(side='top')
self.file_button.bind('<Button-1>', lambda _: self.a.append(self.open_file(1)))
self.chose_label = Label(self.master, text=u'Что вы хотите сделать с файлом?', font="Arial 16")
self.chose_label.pack(side='top')
self.var = IntVar()
self.enc_button = Radiobutton(self.master, text='Зашифровать', variable=self.var, value=1, font="Arial 16")
self.dec_button = Radiobutton(self.master, text='Расшифровать', variable=self.var, value=2, font="Arial 16")
self.enc_button.pack(side='top')
self.dec_button.pack(side='top')
self.enc_button['command'] = self.encrypt_place
self.dec_button['command'] = self.decrypt_place
self.op_key_enter = Entry(font="Arial 16")
self.sec_key_enter = Entry(font="Arial 16")
self.key_label = Label(self.master, text=u"Введите ключ", font="Arial 16")
self.sec_key_btn = Button(self.master, text=u'Загрузить ключ', font="Arial 16")
self.sec_key_btn.bind('<Button-1>', lambda _: self.a.append(self.open_file(2)))
self.sec_key = StringVar()
self.sec_key_enter["textvariable"] = self.sec_key
self.op_key = StringVar()
self.op_key_enter["textvariable"] = self.op_key
self.save_d_enter = Entry(font="Arial 16")
self.save_d = StringVar()
self.save_d_enter["textvariable"] = self.save_d
self.save_d_btn = Button(self.master, text=u'Выберите куда сохранить секретный ключ', font="Arial 16")
self.save_d_btn.bind('<Button-1>', lambda _: self.b.append(self.save_file(1)))
self.save_file_enter = Entry(font="Arial 16")
self.save_file_ = StringVar()
self.save_file_enter["textvariable"] = self.save_file_
self.save_file_btn = Button(self.master, text=u'Выберите куда сохранить результат', font="Arial 16")
self.save_file_btn.bind('<Button-1>', lambda _: self.b.append(self.save_file(2)))
self.start = Button(self.master, text=u'Начать', font="Arial 16")
self.start.pack(side='bottom')
self.save_file_btn.pack(side='bottom')
self.save_file_enter.pack(side='bottom')
self.P = 127
self.Q = 109
self.F = paszi.gen_f(self.P, self.Q)
self.N = int(paszi.gen_n(self.P, self.Q))
self.master.mainloop()
def open_file(self, i):
filename = askopenfilename()
if filename == '':
tkMessageBox.showinfo("Oops", "Wrong file name", font="Arial 16")
if i == 1:
self.file_input.set(filename)
if i == 2:
self.sec_key.set(filename)
return filename
def save_file(self, i):
filename = asksaveasfilename()
if filename == '':
tkMessageBox.showinfo("Oops", "Wrong file name", font="Arial 16")
if i == 1:
d = paszi.multiple_inv(int(self.op_key.get()), self.N)
f = open(filename, "w")
f.write(str(d))
f.close()
self.save_d.set(filename)
if i == 2:
self.save_file_.set(filename)
return filename
def encrypt_place(self):
self.sec_key_btn.pack_forget()
self.sec_key_enter.pack_forget()
self.op_key_enter.pack(side='top')
self.key_label.pack(side='top')
self.save_d_enter.pack(side='top')
self.save_d_btn.pack(side='top')
self.start['command'] = self.encrypt_file
def decrypt_place(self):
self.op_key_enter.pack_forget()
self.key_label.pack_forget()
self.save_d_btn.pack_forget()
self.save_d_enter.pack_forget()
self.sec_key_enter.pack(side='top')
self.sec_key_btn.pack(side='top')
self.start['command'] = self.decrypt_file
def encrypt_file(self):
try:
if (int(self.a[0]) < self.F) & (int(self.F) % int(self.a[0]) != 0):
if os.path.exists(self.a[1]):
paszi.encrypt(self.a[1], self.file_input.get(), N)
tkMessageBox.showinfo("Encrypting", "Successful!")
else:
tkMessageBox.showinfo("ERROR", "Wrong file name")
else:
tkMessageBox.showinfo("ERROR", "Wrong open key")
except Exception:
print type(Exception)
tkMessageBox.showinfo("ERROR", "Something wrong")
def decrypt_file(self):
secret_key = open(self.a[2], 'r')
secret_key.read()
try:
if (int(self.file_input.get()) < self.F) & (int(self.F) % int(self.file_input.get()) != 0):
if os.path.exists(self.a[1]):
paszi.decrypt(self.a[1], secret_key, N)
tkMessageBox.showinfo("Decrypting", "Successful!")
else:
tkMessageBox.showinfo("ERROR", "Wrong file name")
else:
tkMessageBox.showinfo("ERROR", "Wrong open key")
except Exception:
tkMessageBox.showinfo("ERROR", "Something wrong")
#tkMessageBox.showinfo("Decrypt", "It's OK")
root = Tk()
MainWin(root)