coding utf-8 from Tkinter import from tkMessageBox import from tkFileD

  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
#coding: utf-8
from Tkinter import *
from tkMessageBox import *
from tkFileDialog import *
import paszi
import os
class MainWindow(Tk):
def __init__(self):
Tk.__init__(self)
Frame = self
Frame.title('RSA program')
Frame.geometry("300x300+450+100")
Frame.minsize(300, 300)
self.message_file_name = StringVar()
self.secret_key_file_name = StringVar()
self.open_key = StringVar()
self.result_file_name = StringVar()
self.action_type = IntVar(value=0)
self.crypt_operation = self.encrypt_file
self.place_elements()
def place_elements(self):
# Enter message key
self.top_frame = Frame(self)
self.top_frame.pack(padx=3)
self.entry_message_file_name = Entry(self.top_frame)
self.entry_message_file_name['textvariable'] = self.message_file_name
self.entry_message_file_name.pack(side='left')
self.button_message_file_name = Button(self.top_frame, text=u'Открыть')
self.button_message_file_name['command'] = lambda: self.message_file_name.set(askopenfilename())
self.button_message_file_name.pack(padx=5)
# Choose action type
self.middle_frame = Frame(self)
self.middle_frame.pack()
self.radiobutton_encrypt = Radiobutton(self.middle_frame, variable=self.action_type, value=0, text=u'Зашифровать')
self.radiobutton_encrypt.pack(side='left')
self.radiobutton_encrypt['command'] = lambda: (self.decrypt_frame.pack_forget(), self.encrypt_frame.pack())
self.radiobutton_decrypt = Radiobutton(self.middle_frame, variable=self.action_type, value=1, text=u'Расшифровать')
self.radiobutton_decrypt.pack()
self.radiobutton_decrypt['command'] = lambda: (self.encrypt_frame.pack_forget(), self.decrypt_frame.pack())
# Main frame
self.main_frame = Frame(self)
self.main_frame.pack()
self.encrypt_frame_create()
self.decrypt_frame_create()
self.encrypt_frame.pack()
# Enter result file name
self.bottom_frame = Frame(self)
self.bottom_frame.pack(pady=3)
self.entry_result_file_name = Entry(self.bottom_frame)
self.entry_result_file_name['textvariable'] = self.result_file_name
self.entry_result_file_name.pack(side='left')
self.button_result_file_name = Button(self.bottom_frame, text=u'Открыть')
self.button_result_file_name['command'] = lambda: self.result_file_name.set(asksaveasfilename())
self.button_result_file_name.pack(padx=5)
# Start button
self.start_frame = Frame(self)
self.start_frame.pack(pady=3)
self.button_start = Button(self.start_frame, text=u'Начать')
self.button_start['command'] = self.crypt_operation
self.button_start.pack()
def encrypt_frame_create(self):
self.encrypt_frame = Frame(self.main_frame)
self.entry_open_key = Entry(self.encrypt_frame)
self.entry_open_key['textvariable'] = self.open_key
self.entry_open_key.pack(anchor=W)
self.sub_frame = Frame(self.encrypt_frame)
self.sub_frame.pack(pady=3)
self.entry_save_secret_key_file = Entry(self.sub_frame)
self.entry_save_secret_key_file['textvariable'] = self.secret_key_file_name
self.entry_save_secret_key_file.pack(side='left')
self.button_save_secret_key_file = Button(self.sub_frame, text=u'Открыть')
self.button_save_secret_key_file['command'] = lambda: self.secret_key_file_name.set(asksaveasfilename())
self.button_save_secret_key_file.pack(padx=5)
def decrypt_frame_create(self):
self.decrypt_frame = Frame(self.main_frame)
self.entry_load_secret_key_file = Entry(self.decrypt_frame)
self.entry_load_secret_key_file['textvariable'] = self.secret_key_file_name
self.entry_load_secret_key_file.pack(side='left')
self.button_load_secret_key_file = Button(self.decrypt_frame, text=u'Открыть')
self.button_load_secret_key_file['command'] = lambda: self.secret_key_file_name.set(askopenfilename())
self.button_load_secret_key_file.pack(padx=5, pady=3)
def encrypt_file(self):
try:
open_key = int(self.open_key.get())
self.save_secret_key()
if (open_key < paszi.F) & (paszi.gcd(paszi.F, open_key) == 1):
if os.path.exists(self.message_file_name.get()):
paszi.encrypt(self.message_file_name.get(), self.open_key.get(), paszi.N)
showinfo("Encrypting", "Successful!")
else:
showinfo("Encrypting", "Wrong file name")
else:
showinfo("Encrypting", "Wrong open key")
except ValueError:
showinfo("Encrypting", "Something wrong")
def decrypt_file(self):
try:
file_s = open(self.secret_key_file_name.get(), 'r')
secret_key = int(file_s.read())
if os.path.exists(self.secret_key_file_name.get()) & os.path.exists(self.message_file_name.get()):
#print self.a[0], self.b[0], secret_key, self.N
paszi.decrypt(self.message_file_name.get(), self.secret_key_file_name.get(), secret_key, paszi.N)
showinfo("Decrypting", "Successful!")
else:
showinfo("Decrypting", "Wrong file name")
file_s.close()
except ValueError:
showinfo("Decrypting", "Something wrong")
def save_secret_key(self):
try:
d = int(paszi.multiple_inv(int(self.open_key.get()), paszi.N))
print d
f = open(self.secret_key_file_name.get(), "w")
f.write(str(d))
f.close()
showinfo("It's OK", "The private key is successfully loaded")
except Exception:
showinfo("ERROR", "Lapsus :(")
def crypt_operation(self):
if self.action_type == 0:
self.decrypt_file()
elif self.action_type == 1:
self.encrypt_file()
MainWindow().mainloop()