吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1939|回复: 6
收起左侧

[Python 原创] 自己用AES加密算法,写了一个加密解密工具,但还有很多不完善的,目前仅支持16字符

[复制链接]
Wasys 发表于 2023-5-16 09:44
[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import filedialog
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import os

def encrypt_file(input_file, output_file, key):
    cipher = AES.new(key, AES.MODE_CBC)
    with open(input_file, 'rb') as file_in:
        plaintext = file_in.read()
    ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
    with open(output_file, 'wb') as file_out:
        file_out.write(cipher.iv)
        file_out.write(ciphertext)


def decrypt_file(input_file, output_file, key):
    with open(input_file, 'rb') as file_in:
        iv = file_in.read(AES.block_size)
        ciphertext = file_in.read()
    cipher = AES.new(key, AES.MODE_CBC, iv)
    plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)
    with open(output_file, 'wb') as file_out:
        file_out.write(plaintext)


def browse_input_file():
    input_file = filedialog.askopenfilename()
    if input_file:
        input_entry.delete(0, tk.END)
        input_entry.insert(0, input_file)


def browse_output_file():
    output_file = filedialog.asksaveasfilename()
    if output_file:
        output_entry.delete(0, tk.END)
        output_entry.insert(0, output_file)

# 对文件进行16位字符加密操作
def encrypt():
    key = key_entry.get().encode()
    input_file = input_entry.get()
    output_folder = filedialog.askdirectory()
    if output_folder:
        encrypt_file(input_file, output_folder, key)
        result_label.config(text="Encryption completed.")


def encrypt_file(input_file, output_folder, key):
    cipher = AES.new(key, AES.MODE_CBC)
    with open(input_file, 'rb') as file_in:
        plaintext = file_in.read()
    ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
    input_filename = os.path.basename(input_file)
    output_filename = input_filename + ".encrypted"
    output_file_path = os.path.join(output_folder, output_filename)
    with open(output_file_path, 'wb') as file_out:
        file_out.write(cipher.iv)
        file_out.write(ciphertext)

# 对文件进行16位字符解密
def decrypt():
    key = key_entry.get().encode()
    input_file = input_entry.get()
    output_folder = os.path.dirname(input_file)
    decrypt_file(input_file, output_folder, key)
    result_label.config(text="Decryption completed.")


def encrypt_file(input_file, output_folder, key):
    cipher = AES.new(key, AES.MODE_CBC)
    with open(input_file, 'rb') as file_in:
        plaintext = file_in.read()
    ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
    output_filename = os.path.basename(input_file) + ".encrypted"
    output_file_path = os.path.join(output_folder, output_filename)
    with open(output_file_path, 'wb') as file_out:
        file_out.write(cipher.iv)
        file_out.write(ciphertext)


def decrypt_file(input_file, output_folder, key):
    with open(input_file, 'rb') as file_in:
        iv = file_in.read(16)  # 读取 IV(前16字节数据)
        ciphertext = file_in.read()

    cipher = AES.new(key, AES.MODE_CBC, iv)
    plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)

    output_filename = os.path.splitext(os.path.basename(input_file))[0]  # 去除 ".encrypted" 扩展名
    output_file_path = os.path.join(output_folder, output_filename)

    with open(output_file_path, 'wb') as file_out:
        file_out.write(plaintext)
# 创建主窗口
window = tk.Tk()
window.title("文件加密解密工具")

# 输入密钥
key_label = tk.Label(window, text="密钥:")
key_label.pack()
key_entry = tk.Entry(window, show="*")
key_entry.pack()

# 输入文件
input_frame = tk.Frame(window)
input_frame.pack()
input_label = tk.Label(input_frame, text="输入文件:")
input_label.pack(side=tk.LEFT)
input_entry = tk.Entry(input_frame, width=40)
input_entry.pack(side=tk.LEFT)
input_button = tk.Button(input_frame, text="浏览", command=browse_input_file)
input_button.pack(side=tk.LEFT)

# 输出文件
output_frame = tk.Frame(window)
output_frame.pack()
output_label = tk.Label(output_frame, text="输出文件:")
output_label.pack(side=tk.LEFT)
output_entry = tk.Entry(output_frame, width=40)
output_entry.pack(side=tk.LEFT)
output_button = tk.Button(output_frame, text="浏览", command=browse_output_file)
output_button.pack(side=tk.LEFT)

# 加密按钮
encrypt_button = tk.Button(window, text="加密", command=encrypt)
encrypt_button.pack()

# 解密按钮
decrypt_button = tk.Button(window, text="解密", command=decrypt)
decrypt_button.pack()

# 结果标签
result_label = tk.Label(window, text="")
result_label.pack()

# 运行主循环
window.mainloop()

免费评分

参与人数 1吾爱币 +3 热心值 +1 收起 理由
wushaominkk + 3 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

lsy832 发表于 2023-5-16 13:29
巧了这几天正在看密码学
Dlan 发表于 2023-5-16 14:05
hybpjx 发表于 2023-5-17 10:48
可以加个AES.MODE的可选性 支持别的模式 毕竟只支持一种加密模式还是有点局限。
 楼主| Wasys 发表于 2023-5-18 08:32
谢谢各位大神指点
gzsklsskszngc 发表于 2023-5-19 23:27
Dlan 发表于 2023-5-16 14:05
没看到UI,差评

[Python] 纯文本查看 复制代码
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QGridLayout, QVBoxLayout, QHBoxLayout, QPushButton, QFileDialog, QMessageBox
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import os


class FileEncryptor(QWidget):
    def __init__(self):
        super().__init__()

        self.key = ''

        self.initUI()

    def initUI(self):
        # 设置窗口标题和图标
        self.setWindowTitle('文件加密解密器')
        self.setWindowIcon(QIcon('icon.png'))

        # 创建控件
        self.keyLabel = QLabel('请输入密码:')
        self.keyLineEdit = QLineEdit()
        self.inputFileLabel = QLabel('请选择待加密/解密的文件:')
        self.inputFileLineEdit = QLineEdit()
        self.inputFileButton = QPushButton('浏览')
        self.outputFileLabel = QLabel('请选择输出文件夹:')
        self.outputFileLineEdit = QLineEdit()
        self.outputFileButton = QPushButton('浏览')
        self.encryptButton = QPushButton('加密')
        self.decryptButton = QPushButton('解密')

        # 绑定事件
        self.keyLineEdit.textChanged.connect(self.updateKey)
        self.inputFileLineEdit.textChanged.connect(self.checkInputFile)
        self.inputFileButton.clicked.connect(self.browseInputFile)
        self.outputFileLineEdit.textChanged.connect(self.checkOutputFolder)
        self.outputFileButton.clicked.connect(self.browseOutputFolder)
        self.encryptButton.clicked.connect(self.encryptFile)
        self.decryptButton.clicked.connect(self.decryptFile)

        # 设置布局
        grid = QGridLayout()
        grid.addWidget(self.keyLabel, 0, 0)
        grid.addWidget(self.keyLineEdit, 0, 1)
        grid.addWidget(self.inputFileLabel, 1, 0)
        grid.addWidget(self.inputFileLineEdit, 1, 1)
        grid.addWidget(self.inputFileButton, 1, 2)
        grid.addWidget(self.outputFileLabel, 2, 0)
        grid.addWidget(self.outputFileLineEdit, 2, 1)
        grid.addWidget(self.outputFileButton, 2, 2)

        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(self.encryptButton)
        hbox.addWidget(self.decryptButton)

        vbox = QVBoxLayout()
        vbox.addLayout(grid)
        vbox.addStretch(1)
        vbox.addLayout(hbox)

        self.setLayout(vbox)

        # 禁止调整窗口大小
        self.setFixedSize(self.width(), self.height())

    def updateKey(self, text):
        self.key = text.encode()

    def checkInputFile(self, text):
        if os.path.isfile(text):
            self.inputFileLineEdit.setStyleSheet('')
        else:
            self.inputFileLineEdit.setStyleSheet('color: red')

    def browseInputFile(self):
        filename, _ = QFileDialog.getOpenFileName(self, '选择文件', '', 'All Files (*)')
        if filename:
            self.inputFileLineEdit.setText(filename)

    def checkOutputFolder(self, text):
        if os.path.isdir(text):
            self.outputFileLineEdit.setStyleSheet('')
        else:
            self.outputFileLineEdit.setStyleSheet('color: red')

    def browseOutputFolder(self):
        folder = QFileDialog.getExistingDirectory(self, '选择文件夹')
        if folder:
            self.outputFileLineEdit.setText(folder)

    def encryptFile(self):
        input_file = self.inputFileLineEdit.text()
        output_folder = self.outputFileLineEdit.text()
        if not (self.key and os.path.isfile(input_file) and os.path.isdir(output_folder)):
            return

        cipher = AES.new(self.key, AES.MODE_CBC)
        with open(input_file, 'rb') as file_in:
            plaintext = file_in.read()
        ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
        input_filename = os.path.basename(input_file)
        output_filename = input_filename + ".encrypted"
        output_file_path = os.path.join(output_folder, output_filename)
        with open(output_file_path, 'wb') as file_out:
            file_out.write(cipher.iv)
            file_out.write(ciphertext)

        QMessageBox.information(self, '提示', '加密成功!')

    def decryptFile(self):
        input_file = self.inputFileLineEdit.text()
        output_folder = self.outputFileLineEdit.text()
        if not (self.key and os.path.isfile(input_file) and os.path.isdir(output_folder)):
            return

        with open(input_file, 'rb') as file_in:
            iv = file_in.read(16)
            ciphertext = file_in.read()

        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)

        output_filename = os.path.splitext(os.path.basename(input_file))[0]
        output_file_path = os.path.join(output_folder, output_filename)

        with open(output_file_path, 'wb') as file_out:
            file_out.write(plaintext)

        QMessageBox.information(self, '提示', '解密成功!')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    fileEncryptor = FileEncryptor()
    fileEncryptor.show()
    sys.exit(app.exec_())

免费评分

参与人数 1吾爱币 +2 收起 理由
Dlan + 2 我很赞同!

查看全部评分

corona522 发表于 2023-7-30 18:57
学习了,继续
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 20:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表