Skip to content

Commit

Permalink
增加文件夹名称加密功能
Browse files Browse the repository at this point in the history
  • Loading branch information
cforth committed Dec 9, 2019
1 parent 4c1d156 commit 398d43b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
20 changes: 18 additions & 2 deletions Cryptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def populate_comboboxes(self):
self.dataOptionCombobox.state(('readonly',))
self.nameCryptoOptionCombobox.state(('readonly',))
self.cryptOptionCombobox.config(values=["加密", "解密", "加密预览", "解密预览"])
self.dataOptionCombobox.config(values=["字符串", "文件", "文件夹"])
self.dataOptionCombobox.config(values=["字符串", "文件", "文件夹", "文件夹名称"])
self.nameCryptoOptionCombobox.config(values=["修改文件名", "保持文件名"])
set_combobox_item(self.cryptOptionCombobox, "加密", True)
set_combobox_item(self.dataOptionCombobox, "字符串", True)
Expand Down Expand Up @@ -253,11 +253,18 @@ 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):
Expand All @@ -282,7 +289,7 @@ def file_from_choose(self):
# 选择输入文件路径后,在文件浏览器中选中的文件
if file_path:
DirShowHandle(self, self.tree, file_path, lambda x: x).start()
elif self.dataOption.get() == "文件夹":
elif self.dataOption.get() == "文件夹" or self.dataOption.get() == "文件夹名称":
file_path = filedialog.askdirectory()
# 选择输入文件夹路径后,在文件浏览器中显示路径下的内容
if file_path:
Expand Down Expand Up @@ -513,6 +520,15 @@ def run_task(self):
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 == "加密":
DirNameCrypto(password).encrypt(input_text)
elif crypto_option == "解密":
DirNameCrypto(password).decrypt(input_text)


# 预览加密文件名称
class DirShowHandle(threading.Thread):
Expand Down
67 changes: 67 additions & 0 deletions libs/CFCrypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Util.Padding import pad, unpad
import logging
import json


# '\0'填充密码
Expand Down Expand Up @@ -374,6 +375,71 @@ def decrypt(self, file_path, output_file_path):
f.write(data)


# 文件假名称加密解密类
class DirNameCrypto(object):
def __init__(self, password, config_file=None):
self.string_crypto = StringCrypto(password)
# 用来保存文件名MD5值的字典的配置文件
self.config_file = config_file
# 用来保存文件名MD5值的字典
self.file_name_md5_dict = {}

# 将文件名替换成MD5值,并保存至字典中
def file_name_encrypt(self, file_name):
file_name_encrypt_str = self.string_crypto.encrypt(file_name)
file_name_md5 = get_str_md5(file_name_encrypt_str)
self.file_name_md5_dict[file_name_md5] = file_name_encrypt_str
return file_name_md5

# 读取MD5值对应的文件名
def file_name_decrypt(self, file_name_md5):
file_name_encrypt_str = self.file_name_md5_dict[file_name_md5]
file_name = self.string_crypto.decrypt(file_name_encrypt_str)
return file_name

# 文件夹处理方法
@staticmethod
def dir_handle(input_dir, name_handle_func):
real_input_dir = os.path.abspath(input_dir).replace('\\', '/')
if not os.path.exists(real_input_dir):
raise ValueError('Input Dir not exists: %s', real_input_dir)
for path, subdir, files in os.walk(input_dir, topdown=False):
for d in subdir:
original_dir = os.path.join(os.path.abspath(path), d)
try:
rename_dir = os.path.join(os.path.abspath(path), name_handle_func(d))
os.rename(original_dir, rename_dir)
except Exception as e:
logging.exception(e)
for f in files:
original_file = os.path.join(os.path.abspath(path), f)
try:
rename_file = os.path.join(os.path.abspath(path), name_handle_func(f))
os.rename(original_file, rename_file)
except Exception as e:
logging.exception(e)

def encrypt(self, input_dir):
DirNameCrypto.dir_handle(input_dir, self.file_name_encrypt)
# 保存文件名MD5值字典
if not self.config_file:
input_dir_name = os.path.basename(os.path.abspath(input_dir))
encrypt_config_name = self.file_name_encrypt(input_dir_name) + ".json"
self.config_file = os.path.join(os.path.dirname(os.path.abspath(input_dir)), encrypt_config_name)
with open(self.config_file, "w") as f:
json.dump(self.file_name_md5_dict, f)

def decrypt(self, input_dir):
# 读取文件名MD5值字典
if not self.config_file:
input_dir_name = os.path.basename(os.path.abspath(input_dir))
encrypt_config_name = self.file_name_encrypt(input_dir_name) + ".json"
self.config_file = os.path.join(os.path.dirname(os.path.abspath(input_dir)), encrypt_config_name)
with open(self.config_file, "r") as f:
self.file_name_md5_dict = json.load(f)
DirNameCrypto.dir_handle(input_dir, self.file_name_decrypt)


# 文件MD5值生成
def get_file_md5(filename):
if not os.path.isfile(filename):
Expand All @@ -395,3 +461,4 @@ def get_str_md5(string):
my_hash = hashlib.md5()
my_hash.update(string.encode('utf-8'))
return my_hash.hexdigest()

0 comments on commit 398d43b

Please sign in to comment.