-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCryptor.py
663 lines (604 loc) · 31.5 KB
/
Cryptor.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.filedialog as filedialog
import tkinter.messagebox as tkmessagebox
import logging
import threading
import time
import subprocess
import json
from threading import Thread
from multiprocessing import Process
from libs.Util import *
import libs.CFCrypto as CFCrypto
import libs.CFCryptoX as CFCryptoX
import libs.CFPlayer as CFPlayer
import ImgLook
import TextLook
logging.basicConfig(level=logging.ERROR)
class Window(ttk.Frame):
def __init__(self, master=None):
super().__init__(master, padding=2)
self.__init_variables()
self.__init_widgets()
self.__init_layout()
self.__init_bindings()
# 创建控件中需要用到的变量
def __init_variables(self):
self.cryptOption = tk.StringVar()
self.dataOption = tk.StringVar()
self.cryptModeOption = tk.StringVar()
self.nameOption = tk.StringVar()
self.process_var = tk.DoubleVar()
self.process_label_var = tk.StringVar()
# 用于记录文件处理或文件夹处理的进度值
self.task_now_length = 0
# 加密解密任务对象,用来改变加密解密的启停状态
self.crypto_task = None
# 创建控件
def __init_widgets(self):
self.cryptOptionCombobox = ttk.Combobox(self, width=10, textvariable=self.cryptOption)
self.dataOptionCombobox = ttk.Combobox(self, width=10, textvariable=self.dataOption)
# 选择使用哪种加密模式,ECB或CBC
self.cryptModeCombobox = ttk.Combobox(self, width=10, textvariable=self.cryptModeOption)
# 选择文件夹选项时是否加密解密文件名
self.nameCryptoOptionCombobox = ttk.Combobox(self, width=10, textvariable=self.nameOption)
self.populate_comboboxes()
self.passwordEntry = ttk.Entry(self, width=40, show="*")
self.passwordShowButton = ttk.Button(self, text="密码", width=5, command=self.password_show)
self.textFromEntry = ttk.Entry(self, width=40)
self.textToEntry = ttk.Entry(self, width=40)
self.fileFromChooseButton = ttk.Button(self, text="来源", state="disable", width=10,
command=self.file_from_choose)
self.fileToChooseButton = ttk.Button(self, text="目标", state="disable", width=10, command=self.file_to_choose)
self.run_button = ttk.Button(self, text="执行", command=self.run_task)
self.stop_button = ttk.Button(self, text="停止", command=self.stop_task)
# 进度条
self.progressBar = ttk.Progressbar(self, orient='horizontal', mode='determinate', value=0)
self.progressBar['variable'] = self.process_var
self.progressLabel = ttk.Label(self, textvariable=self.process_label_var)
# 文件浏览器
self.tree = ttk.Treeview(self, height=20)
self.ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
self.xsb = ttk.Scrollbar(self, orient='horizontal', command=self.tree.xview)
self.tree.configure(yscroll=self.ysb.set, xscroll=self.xsb.set)
self.tree.heading('#0', text="预览窗口", anchor='w')
self.tree.column("#0", anchor="w")
# 文件浏览器区右键菜单
self.tree_menu = tk.Menu(self, tearoff=0)
self.tree_menu.add_command(label="复制地址", command=self.on_copy_file_path)
self.tree_menu.add_separator()
self.tree_menu.add_command(label="打开文件", command=self.on_open_file)
self.tree_menu.add_separator()
self.tree_menu.add_command(label="打开文件夹", command=self.on_open_dir_path)
# 输入框右键菜单
self.entry_menu = tk.Menu(self, tearoff=0)
self.entry_menu.add_command(label="复制", command=self.on_entry_copy)
self.entry_menu.add_separator()
self.entry_menu.add_command(label="粘贴", command=self.on_entry_paste)
self.entry_menu.add_separator()
self.entry_menu.add_command(label="剪切", command=self.on_entry_cut)
# 将控件布局
def __init_layout(self):
pad_w_e = dict(sticky=(tk.W, tk.E), padx="0.5m", pady="0.5m")
self.passwordShowButton.grid(row=0, column=0, **pad_w_e)
self.passwordEntry.grid(row=0, column=1, columnspan=3, **pad_w_e)
self.fileFromChooseButton.grid(row=1, column=0, **pad_w_e)
self.textFromEntry.grid(row=1, column=1, columnspan=3, **pad_w_e)
self.fileToChooseButton.grid(row=2, column=0, **pad_w_e)
self.textToEntry.grid(row=2, column=1, columnspan=3, **pad_w_e)
self.cryptOptionCombobox.grid(row=3, column=0, **pad_w_e)
self.dataOptionCombobox.grid(row=3, column=1, **pad_w_e)
self.nameCryptoOptionCombobox.grid(row=3, column=2, **pad_w_e)
self.cryptModeCombobox.grid(row=3, column=3, **pad_w_e)
self.progressBar.grid(row=4, column=0, columnspan=2, **pad_w_e)
self.run_button.grid(row=4, column=2, **pad_w_e)
self.stop_button.grid(row=4, column=3, **pad_w_e)
self.progressLabel.grid(row=5, column=0, columnspan=4, **pad_w_e)
self.tree.grid(row=6, column=0, columnspan=4, sticky=(tk.N, tk.S, tk.E, tk.W))
self.ysb.grid(row=6, column=4, sticky=(tk.N, tk.S))
self.xsb.grid(row=7, column=0, columnspan=5, **pad_w_e)
self.grid(row=0, column=0, sticky=(tk.N, tk.S, tk.E, tk.W))
self.columnconfigure(2, weight=1)
self.rowconfigure(6, weight=1)
self.master.columnconfigure(0, weight=1)
self.master.rowconfigure(0, weight=1)
# 绑定事件
def __init_bindings(self):
self.dataOptionCombobox.bind("<<ComboboxSelected>>", self.data_choose_event)
self.cryptOptionCombobox.bind("<<ComboboxSelected>>", self.crypt_choose_event)
# 绑定自定义事件给主窗口
self.bind("<<DisableCrypto>>", self.disable_crypto_button)
self.bind("<<AllowCrypto>>", self.allow_crypto_button)
self.tree.bind("<Button-3>", self.pop_tree_menu)
self.tree.bind("<Double-Button-1>", self.on_open_file)
self.textFromEntry.bind("<FocusIn>", self.get_current_widget)
self.textToEntry.bind("<FocusIn>", self.get_current_widget)
self.textFromEntry.bind("<Button-3>", self.pop_entry_menu)
self.textToEntry.bind("<Button-3>", self.pop_entry_menu)
# 填充下拉列表选项
def populate_comboboxes(self):
self.cryptOptionCombobox.state(('readonly',))
self.dataOptionCombobox.state(('readonly',))
self.nameCryptoOptionCombobox.state(('readonly',))
self.cryptModeCombobox.state(('readonly',))
self.cryptOptionCombobox.config(values=["加密", "解密", "加密预览", "解密预览"])
self.dataOptionCombobox.config(values=["字符串", "文件", "文件夹", "文件夹名称"])
self.nameCryptoOptionCombobox.config(values=["修改文件名", "保持文件名"])
self.cryptModeCombobox.config(values=["ECB", "CBC"])
set_combobox_item(self.cryptOptionCombobox, "加密", True)
set_combobox_item(self.dataOptionCombobox, "字符串", True)
set_combobox_item(self.nameCryptoOptionCombobox, "修改文件名", True)
set_combobox_item(self.cryptModeCombobox, "ECB", True)
self.nameCryptoOptionCombobox["state"] = "disable"
# 获取当前事件的控件
def get_current_widget(self, event):
self.current_widget = event.widget
# 弹出TreeView菜单
def pop_tree_menu(self, event):
self.tree_menu.post(event.x_root, event.y_root)
# 弹出输入框菜单
def pop_entry_menu(self, event):
self.entry_menu.post(event.x_root, event.y_root)
# 复制选中的文件地址到剪贴板
def on_copy_file_path(self):
item_values = self.tree.item(self.tree.selection()[0])['values']
file_select_path = item_values[0] if item_values else ""
self.clipboard_clear()
self.clipboard_append(file_select_path)
# 右键菜单中,打开文件
def on_open_file(self, event=None):
if self.tree.selection():
item_values = self.tree.item(self.tree.selection()[0])['values']
file_select_path = item_values[0] if item_values else ""
file_select_name = os.path.basename(file_select_path)
if file_select_name:
crypto_option = self.cryptOption.get()
crypto_password = self.passwordEntry.get()
name_crypto_option = self.nameCryptoOptionCombobox.get()
if crypto_option in ["加密", "加密预览"]:
self.open_file(file_select_name, file_select_path)
elif crypto_option in ["解密", "解密预览"]:
if name_crypto_option == "保持文件名":
self.open_file(file_select_name, file_select_path)
elif name_crypto_option == "修改文件名":
crypto_algorithm = self.choose_crypt_mode()
file_decrypt_name = crypto_algorithm.StringCrypto(crypto_password).decrypt(file_select_name)
self.open_file(file_decrypt_name, file_select_path)
# 根据文件后缀名类型打开文件
def open_file(self, file_decrypt_name, file_select_path):
if is_img(file_decrypt_name.lower()):
self.on_open_img(file_select_path)
elif is_txt(file_decrypt_name.lower()):
self.on_open_txt(file_select_path)
elif is_video(file_decrypt_name.lower()):
self.on_open_video(file_select_path)
else:
tkmessagebox.showerror("错误", "暂时不支持打开此类文件!")
# 打开文本文件
def on_open_txt(self, file_select_path):
if self.cryptOption.get() in ["加密", "加密预览"]:
p = Process(target=TextLook.main_window, args=(file_select_path,))
p.start()
elif self.cryptOption.get() in ["解密", "解密预览"]:
password = self.passwordEntry.get()
p = Process(target=TextLook.main_window, args=(file_select_path, password, "解密文件"))
p.start()
# 打开图片文件
def on_open_img(self, file_select_path):
if self.cryptOption.get() in ["加密", "加密预览"]:
p = Process(target=ImgLook.main_window, args=(file_select_path,))
p.start()
elif self.cryptOption.get() in ["解密", "解密预览"]:
password = self.passwordEntry.get()
if self.nameCryptoOptionCombobox.get() == "修改文件名":
p = Process(target=ImgLook.main_window, args=(file_select_path, password, "解密文件", self.cryptModeOption.get()))
p.start()
elif self.nameCryptoOptionCombobox.get() == "保持文件名":
p = Process(target=ImgLook.main_window, args=(file_select_path, password, "解密保名", self.cryptModeOption.get()))
p.start()
# 打开视频文件
def on_open_video(self, file_select_path):
if self.cryptOption.get() in ["加密", "加密预览"]:
p = Process(target=CFPlayer.play, args=(file_select_path,))
p.start()
else:
pass
# 打开文件所在的文件夹
def on_open_dir_path(self):
item_values = self.tree.item(self.tree.selection()[0])['values']
file_select_path = item_values[0] if item_values else ""
if os.name == "nt":
subprocess.Popen('explorer.exe /select, "%s"' % file_select_path)
elif os.name == "posix":
# linux系统的功能待完成
pass
# 剪贴板上的字符串粘贴到Entry中
def on_entry_paste(self):
text = ""
try:
text = self.clipboard_get()
except tk.TclError:
pass
try:
self.current_widget.delete(0, 'end')
except tk.TclError:
pass
self.current_widget.insert(tk.INSERT, text)
# 复制Entry中的字符串到剪贴板
def on_entry_copy(self):
text = self.current_widget.get()
self.clipboard_clear()
self.clipboard_append(text)
# 剪切Entry中的字符串到剪贴板
def on_entry_cut(self):
self.on_entry_copy()
try:
self.current_widget.delete(0, 'end')
except tk.TclError:
pass
# 禁用加密解密按钮
def disable_crypto_button(self, event=None):
self.run_button["state"] = "disable"
self.run_button["text"] = "处理中"
# 启用加密解密按钮
def allow_crypto_button(self, event=None):
self.run_button["state"] = "normal"
self.run_button["text"] = "执行"
# 设置文件选择按钮是否可用
def data_choose_event(self, event=None):
if self.dataOption.get() == "字符串":
self.fileFromChooseButton["state"] = "disable"
self.fileToChooseButton["state"] = "disable"
self.textToEntry["state"] = "normal"
self.nameCryptoOptionCombobox["state"] = "disable"
elif self.dataOption.get() == "文件" or self.dataOption.get() == "文件夹":
self.fileFromChooseButton["state"] = "normal"
self.fileToChooseButton["state"] = "normal"
self.textToEntry["state"] = "normal"
self.nameCryptoOptionCombobox["state"] = "readonly"
elif self.dataOption.get() == "文件夹名称":
self.fileFromChooseButton["state"] = "normal"
self.fileToChooseButton["state"] = "disable"
self.textToEntry["state"] = "disable"
self.nameCryptoOptionCombobox["state"] = "disable"
# 设置路径输入是否可用
def crypt_choose_event(self, event=None):
if self.cryptOption.get() == "加密" or self.cryptOption.get() == "解密":
self.textToEntry["state"] = "normal"
elif self.cryptOption.get() == "加密预览" or self.cryptOption.get() == "解密预览":
self.textToEntry.delete(0, len(self.textToEntry.get()))
self.textToEntry["state"] = "disable"
# 显示或隐藏密码
def password_show(self):
if self.passwordEntry["show"] == "*":
self.passwordEntry["show"] = ""
else:
self.passwordEntry["show"] = "*"
# 文件/文件夹输入选择
def file_from_choose(self):
file_path = ""
if self.dataOption.get() == "文件":
file_path = filedialog.askopenfilename()
# 选择输入文件路径后,在文件浏览器中选中的文件
if file_path:
DirShowHandle(self, self.tree, file_path, lambda x: x).start()
elif self.dataOption.get() == "文件夹" or self.dataOption.get() == "文件夹名称":
file_path = filedialog.askdirectory()
# 选择输入文件夹路径后,在文件浏览器中显示路径下的内容
if file_path:
DirShowHandle(self, self.tree, file_path, lambda x: x).start()
self.textFromEntry.delete(0, len(self.textFromEntry.get()))
self.textFromEntry.insert(0, file_path)
# 文件夹输出选择
def file_to_choose(self):
file_path = filedialog.askdirectory()
self.textToEntry.delete(0, len(self.textToEntry.get()))
self.textToEntry.insert(0, file_path)
# 更新文件处理或文件夹处理的进度值
def update_task_now_length(self, obj, length):
self.task_now_length = 0
while True:
time.sleep(0.5)
crypto_status, self.task_now_length = obj.get_status()
if not crypto_status or self.task_now_length >= length:
logging.info("now_length:%d, length:%d" % (self.task_now_length, length))
break
logging.info("Done!")
# 更新进度条
def update_process_bar(self, max_length, max_position=100):
# 发送消息给主窗口,禁用按钮
self.event_generate("<<DisableCrypto>>", when="tail")
self.process_var.set(0.0)
self.process_label_var.set("")
old_length = 0
while True:
if self.task_now_length > old_length:
process_scale = self.task_now_length / max_length
self.process_var.set(process_scale * max_position)
self.process_label_var.set(str(int(process_scale * 100)) + " % 任务进行中...")
logging.info("Old ProcessBar position: %d New ProcessBar position: %d"
% (
old_length * max_position / max_length, self.task_now_length * max_position / max_length))
old_length = self.task_now_length
elif self.task_now_length == max_length:
self.process_var.set(max_position)
self.process_label_var.set("100% 任务已完成.")
break
elif self.crypto_task.if_stop():
self.task_now_length = 0
self.process_label_var.set("任务已被外部终止.")
break
time.sleep(0.5)
self.event_generate("<<AllowCrypto>>", when="tail")
# 选择加密解密使用的模式
def choose_crypt_mode(self):
if self.cryptModeOption.get() == "ECB":
return CFCrypto
elif self.cryptModeOption.get() == "CBC":
return CFCryptoX
# 使用多线程加密文件
def file_encrypt(self, file_path, output_dir_path, password, is_encrypt_name):
crypto_algorithm = self.choose_crypt_mode()
self.crypto_task = crypto_algorithm.FileCrypto(password)
input_file_name = os.path.split(file_path)[1]
max_length = os.path.getsize(file_path)
update_task_length_thread = Thread(target=self.update_task_now_length, args=(self.crypto_task, max_length,))
update_process_thread = Thread(target=self.update_process_bar, args=(max_length,))
# is_encrypt_name为False时,不加密文件名
if is_encrypt_name:
output_path = os.path.join(output_dir_path, crypto_algorithm.StringCrypto(password).encrypt(input_file_name))
else:
output_path = os.path.join(output_dir_path, input_file_name)
if os.path.exists(output_path):
tkmessagebox.showerror("错误", "加密后输出路径下存在同名文件!")
return
# 为了不阻塞窗口主程序,使用多线程加密文件
crypto_thread = Thread(target=self.crypto_task.encrypt, args=(file_path, output_path))
crypto_thread.start()
update_task_length_thread.start()
update_process_thread.start()
# 使用多线程解密文件
def file_decrypt(self, file_path, output_dir_path, password, is_decrypt_name):
crypto_algorithm = self.choose_crypt_mode()
self.crypto_task = crypto_algorithm.FileCrypto(password)
input_file_name = os.path.split(file_path)[1]
max_length = os.path.getsize(file_path)
update_task_length_thread = Thread(target=self.update_task_now_length, args=(self.crypto_task, max_length,))
update_process_thread = Thread(target=self.update_process_bar, args=(max_length,))
try:
# is_decrypt_name为False时,不解密文件名
if is_decrypt_name:
output_path = os.path.join(output_dir_path, crypto_algorithm.StringCrypto(password).decrypt(input_file_name))
else:
output_path = os.path.join(output_dir_path, input_file_name)
if os.path.exists(output_path):
tkmessagebox.showerror("错误", "解密后输出路径下存在同名文件!")
return
# 为了不阻塞窗口主程序,使用多线程解密文件
crypto_thread = Thread(target=self.crypto_task.decrypt, args=(file_path, output_path))
crypto_thread.start()
update_task_length_thread.start()
update_process_thread.start()
except Exception as e:
logging.warning("Convert error: ", e)
tkmessagebox.showerror("错误", "输入文件格式或者密码错误!")
return ""
# 使用多线程加密文件夹
def dir_encrypt(self, dir_path, output_dir_path, password, is_encrypt_name):
crypto_algorithm = self.choose_crypt_mode()
self.crypto_task = crypto_algorithm.DirFileCrypto(password)
max_length = count_files(dir_path)
update_task_length_thread = Thread(target=self.update_task_now_length, args=(self.crypto_task, max_length,))
update_process_thread = Thread(target=self.update_process_bar, args=(max_length,))
if is_encrypt_name:
output_path = os.path.join(output_dir_path, crypto_algorithm.StringCrypto(password).encrypt(os.path.split(dir_path)[1]))
else:
output_path = os.path.join(output_dir_path, os.path.split(dir_path)[1])
if os.path.exists(output_path):
tkmessagebox.showerror("错误", "加密后输出路径下存在同名文件夹!")
return
# is_encrypt_name为False时,不加密文件和文件夹名
if is_encrypt_name:
crypto_thread = Thread(target=self.crypto_task.encrypt, args=(dir_path, output_dir_path))
else:
crypto_thread = Thread(target=self.crypto_task.encrypt, args=(dir_path, output_dir_path, False))
crypto_thread.start()
update_task_length_thread.start()
update_process_thread.start()
# 使用多线程解密文件夹
def dir_decrypt(self, dir_path, output_dir_path, password, is_decrypt_name):
crypto_algorithm = self.choose_crypt_mode()
self.crypto_task = crypto_algorithm.DirFileCrypto(password)
max_length = count_files(dir_path)
update_task_length_thread = Thread(target=self.update_task_now_length, args=(self.crypto_task, max_length,))
update_process_thread = Thread(target=self.update_process_bar, args=(max_length,))
try:
if is_decrypt_name:
output_path = os.path.join(output_dir_path, crypto_algorithm.StringCrypto(password).decrypt(os.path.split(dir_path)[1]))
else:
output_path = os.path.join(output_dir_path, os.path.split(dir_path)[1])
if os.path.exists(output_path):
tkmessagebox.showerror("错误", "解密后输出路径下存在同名文件夹!")
return
# is_decrypt_name为False时,不解密文件和文件夹名
if is_decrypt_name:
crypto_thread = Thread(target=self.crypto_task.decrypt, args=(dir_path, output_dir_path))
else:
crypto_thread = Thread(target=self.crypto_task.decrypt, args=(dir_path, output_dir_path, False))
crypto_thread.start()
update_task_length_thread.start()
update_process_thread.start()
except Exception as e:
logging.warning("Convert error: ", e)
tkmessagebox.showerror("错误", "输入文件格式或者密码错误!")
return ""
# 文件夹名称加密
def dir_name_md5_encrypt(self, dir_path, password):
crypto_algorithm = self.choose_crypt_mode()
self.crypto_task = crypto_algorithm.DirNameCrypto(password)
max_length = count_files(dir_path)
try:
crypto_thread = Thread(target=self.crypto_task.encrypt, args=(dir_path,))
update_task_length_thread = Thread(target=self.update_task_now_length, args=(self.crypto_task, max_length,))
update_process_thread = Thread(target=self.update_process_bar, args=(max_length,))
crypto_thread.start()
update_task_length_thread.start()
update_process_thread.start()
except Exception as e:
logging.warning("Convert error: ", e)
tkmessagebox.showerror("错误", "输入文件格式或者密码错误!")
# 文件夹名称解密
def dir_name_md5_decrypt(self, dir_path, password):
crypto_algorithm = self.choose_crypt_mode()
self.crypto_task = crypto_algorithm.DirNameCrypto(password)
max_length = count_files(dir_path)
try:
crypto_thread = Thread(target=self.crypto_task.decrypt, args=(dir_path,))
update_task_length_thread = Thread(target=self.update_task_now_length, args=(self.crypto_task, max_length,))
update_process_thread = Thread(target=self.update_process_bar, args=(max_length,))
crypto_thread.start()
update_task_length_thread.start()
update_process_thread.start()
except Exception as e:
logging.warning("Convert error: ", e)
tkmessagebox.showerror("错误", "输入文件格式或者密码错误!")
# 强制停止任务
def stop_task(self):
if self.crypto_task:
self.crypto_task.stop_handle()
# 执行加密或者解密任务
def run_task(self):
crypto_algorithm = self.choose_crypt_mode()
input_text = self.textFromEntry.get()
output_text = self.textToEntry.get()
password = self.passwordEntry.get()
crypto_option = self.cryptOption.get()
data_option = self.dataOption.get()
is_handle_name = True if self.nameOption.get() == "修改文件名" else False
if not password:
tkmessagebox.showerror("错误", "密码不能为空!")
return
if not input_text:
tkmessagebox.showerror("错误", "来源不能为空!")
return
if crypto_option == "加密预览":
if data_option == "文件" or data_option == "文件夹" and is_handle_name:
DirShowHandle(self, self.tree, input_text, lambda x: crypto_algorithm.StringCrypto(password).encrypt(x)).start()
elif data_option == "文件夹名称":
DirShowHandle(self, self.tree, input_text, lambda x: crypto_algorithm.get_str_md5(crypto_algorithm.StringCrypto(password).encrypt(x))).start()
elif crypto_option == "解密预览":
if data_option == "文件" or data_option == "文件夹" and is_handle_name:
DirShowHandle(self, self.tree, input_text, lambda x: crypto_algorithm.StringCrypto(password).decrypt(x)).start()
elif data_option == "文件夹名称":
# 读取文件名MD5值字典
input_dir_name = os.path.basename(os.path.abspath(input_text))
encrypt_config_name = crypto_algorithm.get_str_md5(crypto_algorithm.StringCrypto(password).encrypt(input_dir_name)) + ".json"
config_file = os.path.join(os.path.dirname(os.path.abspath(input_text)), encrypt_config_name)
file_name_md5_dict = {}
with open(config_file, "r") as f:
file_name_md5_dict = json.load(f)
DirShowHandle(self, self.tree, input_text, lambda x: crypto_algorithm.StringCrypto(password).decrypt(file_name_md5_dict[x])).start()
elif data_option == "字符串":
if crypto_option == "加密":
output_text = crypto_algorithm.StringCrypto(password).encrypt(input_text)
elif crypto_option == "解密":
try:
output_text = crypto_algorithm.StringCrypto(password).decrypt(input_text)
except Exception as e:
logging.warning("Convert error: ", e)
output_text = "输入格式或者密码错误!"
else:
return
self.textToEntry.delete(0, len(self.textToEntry.get()))
self.textToEntry.insert(0, output_text)
elif data_option == "文件":
if not output_text:
tkmessagebox.showerror("错误", "目标不能为空!")
return
if not os.path.exists(input_text):
tkmessagebox.showerror("错误", "输入路径不存在!")
return
if not os.path.exists(output_text):
tkmessagebox.showerror("错误", "输出目录不存在!")
return
if crypto_option == "加密":
self.file_encrypt(input_text, output_text, password, is_handle_name)
elif crypto_option == "解密":
self.file_decrypt(input_text, output_text, password, is_handle_name)
elif data_option == "文件夹":
if not output_text:
tkmessagebox.showerror("错误", "目标不能为空!")
return
if not os.path.exists(input_text):
tkmessagebox.showerror("错误", "输入路径不存在!")
return
if not os.path.exists(output_text):
tkmessagebox.showerror("错误", "输出目录不存在!")
return
if is_sub_path(output_text, input_text):
tkmessagebox.showerror("错误", "输出路径不能是输入路径的子路径!")
return
if crypto_option == "加密":
self.dir_encrypt(input_text, output_text, password, is_handle_name)
elif crypto_option == "解密":
self.dir_decrypt(input_text, output_text, password, is_handle_name)
elif data_option == "文件夹名称":
if not os.path.exists(input_text):
tkmessagebox.showerror("错误", "输入路径不存在!")
return
if crypto_option == "加密":
self.dir_name_md5_encrypt(input_text, password)
elif crypto_option == "解密":
self.dir_name_md5_decrypt(input_text, password)
# 预览加密文件名称
class DirShowHandle(threading.Thread):
def __init__(self, main_window, tree, f_path, name_handle_func):
threading.Thread.__init__(self)
self.main_window = main_window
self.tree = tree
self.f_path = f_path
self.name_handle_func = name_handle_func
def run(self):
# 发送消息给主窗口,禁用按钮
self.main_window.event_generate("<<DisableCrypto>>", when="tail")
[self.tree.delete(item) for item in self.tree.get_children()]
abspath = os.path.abspath(self.f_path)
try:
root_node = self.tree.insert('', 'end', text=self.name_handle_func(os.path.split(abspath)[1]), open=True)
except Exception as e:
logging.warning("Convert error: ", e)
root_node = self.tree.insert('', 'end', text="输入格式或者密码错误!", open=True)
self.process_directory(root_node, abspath, self.name_handle_func)
self.main_window.event_generate("<<AllowCrypto>>", when="tail")
def process_directory(self, parent, f_path, name_handle_func):
if os.path.isdir(f_path):
# 遍历路径下的子目录
for p in os.listdir(f_path):
# 构建路径
abspath = os.path.join(f_path, p)
# 是否存在子目录
isdir = os.path.isdir(abspath)
try:
name = name_handle_func(p)
except Exception as e:
logging.warning("Convert error: ", e)
name = "输入格式或者密码错误!"
# 将文件地址加入tree的item的values中
oid = self.tree.insert(parent, 'end', text=name, values=[abspath], open=False)
if isdir:
self.process_directory(oid, abspath, name_handle_func)
else:
try:
name = name_handle_func(os.path.basename(f_path))
except Exception as e:
logging.warning("Convert error: ", e)
name = "输入格式或者密码错误!"
self.tree.insert(parent, 'end', text=name, values=[f_path], open=False)
if __name__ == '__main__':
app = Window()
# 设置窗口标题:
app.master.title("CF加密解密器")
app.master.minsize(400, 500)
# 主消息循环:
app.mainloop()