Blowfish 算法加密解密工具 - Python 实现

该工具使用 Python 实现 Blowfish 算法的加密解密功能,并提供文件加密解密、密钥生成和密钥复制等功能。

代码实现

import os
import random
import string
import tkinter as tk
from tkinter import filedialog, messagebox
from Crypto.Cipher import Blowfish
from Crypto.Util.Padding import pad, unpad
import pyperclip

def Blowfish_encrypt_decrypt(key, data, mode):
    bs = Blowfish.block_size
    iv = os.urandom(bs)
    cipher = Blowfish.new(key, mode, iv)
    data = pad(data, bs)
    encrypted_data = iv + cipher.encrypt(data)
    return encrypted_data

def Blowfish_encrypt(key, data):
    return Blowfish_encrypt_decrypt(key, data, Blowfish.MODE_CBC)

def Blowfish_decrypt(key, data):
    bs = Blowfish.block_size
    if len(data) % bs != 0:
        raise ValueError('Invalid data length. Data length must be a multiple of {} bytes.'.format(bs))
    iv = data[:bs]
    encrypted_data = data[bs:]
    cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv)
    decrypted_data = cipher.decrypt(encrypted_data)
    unpadded_data = unpad(decrypted_data, bs)
    return unpadded_data

def validate_key(key):
    key_length = len(key.encode())
    if key_length < 8 or key_length > 56 or key_length % 8 != 0:
        raise ValueError('Invalid key length. Key must be between 8 and 56 bytes, and a multiple of 8 bytes.')
    return key.encode()

def create_new_window3():
    def encrypt_file(input_file_path, output_file_path, key):
        key = validate_key(key)
        with open(input_file_path, 'rb') as f:
            data = f.read()
            encrypted_data = Blowfish_encrypt(key, data)
        with open(output_file_path, 'wb') as f:
            f.write(encrypted_data)
        return True
    def decrypt_file(input_file_path, output_file_path, key):
        key = validate_key(key)
        with open(input_file_path, 'rb') as f:
            encrypted_data = f.read()
            decrypted_data = Blowfish_decrypt(key, encrypted_data)
        with open(output_file_path, 'wb') as f:
            f.write(decrypted_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
        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
        if decrypt_file(input_file_path, output_file_path, key):
            messagebox.showinfo('Success', '文件解密成功!')
    def generate_random_key():
        key = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
        return key
    def on_generate_key():
        key = generate_random_key()
        e_key.delete(0, tk.END)
        e_key.insert(0, key)
    def on_copy_key():
        key = e_key.get()
        if key:
            pyperclip.copy(key)
            messagebox.showinfo('Copied', '密钥已复制到剪贴板!')
    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():
        file_path = filedialog.asksaveasfilename(defaultextension='.docx')
        if file_path:
            e_output.delete(0, tk.END)
            e_output.insert(0, file_path)
    new_window = tk.Toplevel(root_new)
    new_window.title('Blowfish 算法加密/解密窗口')
    new_window.geometry('2000x1000')
    lbl_input = tk.Label(new_window, text='输入文件路径:')
    lbl_input.pack()
    e_input = tk.Entry(new_window, width=50)
    e_input.pack()
    btn_select_input = tk.Button(new_window, text='选择文件', command=on_select_input)
    btn_select_input.pack()
    lbl_output = tk.Label(new_window, text='输出文件路径:')
    lbl_output.pack()
    e_output = tk.Entry(new_window, width=50)
    e_output.pack()
    btn_select_output = tk.Button(new_window, text='选择路径', command=on_select_output)
    btn_select_output.pack()
    lbl_key = tk.Label(new_window, text='密钥:')
    lbl_key.pack()
    e_key = tk.Entry(new_window)
    e_key.pack()
    generate_key_btn = tk.Button(new_window, text='生成密钥', command=on_generate_key)
    generate_key_btn.pack()
    copy_key_btn = tk.Button(new_window, text='复制密钥', command=on_copy_key)
    copy_key_btn.pack()
    btn_encrypt = tk.Button(new_window, text='加密', command=on_encrypt)
    btn_encrypt.pack()
    btn_decrypt = tk.Button(new_window, text='解密', command=on_decrypt)
    btn_decrypt.pack()

使用方法

  1. 安装必要的库:
pip install pycryptodome pyperclip tkinter
  1. 运行代码,将会弹出一个窗口,包含以下功能:
    • 选择输入文件路径:选择要加密或解密的文件。
    • 选择输出文件路径:选择加密或解密后的文件保存路径。
    • 输入密钥:可以手动输入密钥,也可以点击“生成密钥”按钮自动生成随机密钥。
    • 复制密钥:将生成的密钥复制到剪贴板。
    • 加密:点击加密按钮,将输入文件加密并保存到输出文件路径。
    • 解密:点击解密按钮,将输入文件解密并保存到输出文件路径。

注意

  • 密钥长度必须在 8 到 56 字节之间,且必须是 8 的倍数。
  • 解密时,需要使用相同的密钥才能成功解密。
  • 该工具仅用于学习和测试,请勿用于任何非法用途。
Blowfish 算法加密解密工具 - Python 实现

原文地址: http://www.cveoy.top/t/topic/oK0Q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录