转轮机密码窗口:加密解密工具
转轮机密码窗口:加密解密工具
该工具使用转轮机密码算法实现文件加密解密,支持多种文件类型,包括.docx,.xlsx等,提供可视化界面,方便用户操作。
代码示例
def create_new_window1():
def encrypt_file(input_file_path, output_file_path, key):
# 根据输入文件的后缀选择合适的加密函数
if input_file_path.endswith('.docx'):
# 如果是.docx文件,使用Python库docx对文本进行加密
document = Document(input_file_path)
for paragraph in document.paragraphs:
text = paragraph.text
encrypted_text = encrypt_text(text, key) #调用encrypt_text函数进行加密操作
paragraph.text = encrypted_text
document.save(output_file_path)#保存加密后的文件
elif input_file_path.endswith('.xlsx'):
#如果是.xlsx文件,使用Python库openpyxl对单元格数据进行加密
workbook = load_workbook(input_file_path)
for sheet in workbook.sheetnames:
worksheet = workbook[sheet]
for row in worksheet.iter_rows():
for cell in row:
if cell.value is None:
continue
if isinstance(cell.value, str):
encrypted_value = encrypt_text(cell.value, key)#调用encrypt_text函数进行加密操作
cell.value = encrypted_value
elif isinstance(cell.value, int) or isinstance(cell.value, float):
encrypted_value = encrypt_number(cell.value, key)
cell.value = encrypted_value
workbook.save(output_file_path)#保存加密后的文件
else:
#如果是其他类型的文件,则按照转子密码进行加密
#定义三个转子,用于进行加密操作
rotor1 = list(string.ascii_lowercase)
rotor2 = list(string.ascii_lowercase)
rotor3 = list(string.ascii_lowercase)
#根据提供的密钥对转子进行初始化
for i in range(key[0]):
rotor1.append(rotor1.pop(0))
for i in range(key[1]):
rotor2.append(rotor2.pop(0))
for i in range(key[2]):
rotor3.append(rotor3.pop(0))
#存储加密后的结果数据
result_data = []
#打开输入文件进行读取数据
with open(input_file_path, 'r') as file:
data = file.read()
#遍历输入文件的每个字符
for character in data:
if character.isalpha():#如果字符是字母
index = rotor1.index(character.lower())#在第一个转子中查找字母在转子中的位置
index = rotor2[index]#将结果作为索引在第二个转子中查找
index = rotor3[index]#将结果作为索引在第三个转子中查找
result_data.append(rotor1[index])#将最终的结果字母添加到结果数据中
else:#如果字符不是字母,则直接添加到结果数据中
result_data.append(character)
#将加密后的结果数据写入输出文件
with open(output_file_path, 'w') as file:
file.write(''.join(result_data))
return True
def decrypt_file(input_file_path, output_file_path, key):
# 根据输入文件的后缀选择合适的解密函数
if input_file_path.endswith('.docx'):
# 如果是.docx文件,使用Python库docx对文本进行解密
document = Document(input_file_path)
for paragraph in document.paragraphs:
text = paragraph.text
decrypted_text = decrypt_text(text, key) #调用decrypt_text函数进行解密操作
paragraph.text = decrypted_text
document.save(output_file_path)#保存解密后的文件
elif input_file_path.endswith('.xlsx'):
#如果是.xlsx文件,使用Python库openpyxl对单元格数据进行解密
workbook = load_workbook(input_file_path)
for sheet in workbook.sheetnames:
worksheet = workbook[sheet]
for row in worksheet.iter_rows():
for cell in row:
if cell.value is None:
continue
if isinstance(cell.value, str):
decrypted_value = decrypt_text(cell.value, key)#调用decrypt_text函数进行解密操作
cell.value = decrypted_value
elif isinstance(cell.value, int) or isinstance(cell.value, float):
decrypted_value = decrypt_number(cell.value, key)
cell.value = decrypted_value
workbook.save(output_file_path)#保存解密后的文件
else:
#如果是其他类型的文件,则按照转子密码进行解密
#定义三个转子,用于进行解密操作
rotor1 = list(string.ascii_lowercase)
rotor2 = list(string.ascii_lowercase)
rotor3 = list(string.ascii_lowercase)
#根据提供的密钥对转子进行初始化
for i in range(key[0]):
rotor1.append(rotor1.pop(0))
for i in range(key[1]):
rotor2.append(rotor2.pop(0))
for i in range(key[2]):
rotor3.append(rotor3.pop(0))
#存储解密后的结果数据
result_data = []
#打开输入文件进行读取数据
with open(input_file_path, 'r') as file:
data = file.read()
#遍历输入文件的每个字符
for character in data:
if character.isalpha():#如果字符是字母
index = rotor1.index(character.lower())#在第一个转子中查找字母在转子中的位置
index = rotor2.index(index)#将结果作为索引在第二个转子中查找
index = rotor3.index(index)#将结果作为索引在第三个转子中查找
result_data.append(rotor1[index])#将最终的结果字母添加到结果数据中
else:#如果字符不是字母,则直接添加到结果数据中
result_data.append(character)
#将解密后的结果数据写入输出文件
with open(output_file_path, 'w') as file:
file.write(''.join(result_data))
return True
#获取输入的文件路径、输出的文件路径和密钥
def on_encrypt():
input_file_path = e_input.get()
output_file_path = e_output.get()
key = e_key.get()
#检查是否填写完整的输入、输出路径和密钥
if not input_file_path or not output_file_path or not key:
messagebox.showwarning('Warning', '请填写完整的输入、输出路径和密钥!')
return
#调用encrypt_file函数进行文件加密操作
if encrypt_file(input_file_path, output_file_path, key):
messagebox.showinfo('Success', '文件加密成功!')
def on_decrypt():
input_file_path = e_input.get()
output_file_path = e_output.get()
key = e_key.get()
#检查是否填写完整的输入、输出路径和密钥
if not input_file_path or not output_file_path or not key:
messagebox.showwarning('Warning', '请填写完整的输入、输出路径和密钥!')
return
#调用decrypt_file函数进行文件解密操作
if decrypt_file(input_file_path, output_file_path, key):
messagebox.showinfo('Success', '文件解密成功!')
def on_select_input():
#弹出选择文件对话框并获取选择的文件路径
file_path = filedialog.askopenfilename()
if file_path:
#将选择的文件路径设置到输入路径文本框中
e_input.delete(0, tk.END)
e_input.insert(0, file_path)
def on_select_output():
#获取输入的文件路径
input_file_path = e_input.get()
if not input_file_path:
return
#根据输入路径的文件扩展名生成默认的输出文件路径,并弹出保存文件对话框获取输出文件路径
file_extension = os.path.splitext(input_file_path)[1]
file_path = filedialog.asksaveasfilename(defaultextension=file_extension)
if file_path:
#将选择的文件路径设置到输出路径文本框中
e_output.delete(0, tk.END)
e_output.insert(0, file_path)
def encrypt_number(number, key):
# 将数字转换为字符串,并在前面加上'N'标识数字类型
number_str = 'N' + str(number)
# 调用encrypt_text函数进行加密操作
encrypted_text = encrypt_text(number_str, key)
return encrypted_text
def decrypt_number(text, key):
# 调用decrypt_text函数进行解密操作
decrypted_text = decrypt_text(text, key)
# 提取出数字部分并转换为数字类型
number = float(decrypted_text[1:])
return number
功能说明
- 加密功能:
- 选择需要加密的文件
- 设置输出文件路径
- 输入密钥
- 点击“加密”按钮进行加密
- 解密功能:
- 选择需要解密的文件
- 设置输出文件路径
- 输入密钥
- 点击“解密”按钮进行解密
使用说明
- 运行代码
- 点击“选择输入文件”按钮选择需要加密或解密的文件
- 点击“选择输出文件”按钮选择输出文件路径
- 在“密钥”文本框中输入密钥,密钥为一个三位数的列表,例如[1, 2, 3]
- 点击“加密”或“解密”按钮进行操作
注意
- 密钥必须与加密或解密时使用的密钥一致
- 该工具仅供学习使用,请勿用于任何非法目的
联系方式
如果您有任何问题,请随时与我联系。
原文地址: https://www.cveoy.top/t/topic/oRSr 著作权归作者所有。请勿转载和采集!