文本加密解密工具,可自定义秘钥,附带源码-python学习交流论坛-编程网安-山海云端论坛

文本加密解密工具,可自定义秘钥,附带源码

代码

[md]from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QTextEdit, QPushButton, QVBoxLayout, QHBoxLayout, QMessageBox
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64
 
 
class TextEncrypter(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.key = b'1234567890123456'
        self.iv = b'abcdefghijklmnop'
 
    def initUI(self):
        self.resize(400, 300)
        self.setWindowTitle('文本加密or解密工具')
 
        self.input_label = QLabel('请输入要加密or解密的文本:')
        self.input_text = QTextEdit()
        self.key_label = QLabel('请输入秘钥:')
        self.key_input = QLineEdit()
        self.encrypt_button = QPushButton('加密')
        self.decrypt_button = QPushButton('解密')
        self.output_label = QLabel('加密or解密后的文本:')
        self.output_text = QTextEdit()
        self.output_text.setReadOnly(True)
 
        input_layout = QVBoxLayout()
        input_layout.addWidget(self.input_label)
        input_layout.addWidget(self.input_text)
 
        key_layout = QHBoxLayout()
        key_layout.addWidget(self.key_label)
        key_layout.addWidget(self.key_input)
 
        button_layout = QHBoxLayout()
        button_layout.addWidget(self.encrypt_button)
        button_layout.addWidget(self.decrypt_button)
 
        output_layout = QVBoxLayout()
        output_layout.addWidget(self.output_label)
        output_layout.addWidget(self.output_text)
 
        main_layout = QVBoxLayout()
        main_layout.addLayout(input_layout)
        main_layout.addLayout(key_layout)
        main_layout.addLayout(button_layout)
        main_layout.addLayout(output_layout)
        self.setLayout(main_layout)
 
        self.encrypt_button.clicked.connect(self.encrypt_text)
        self.decrypt_button.clicked.connect(self.decrypt_text)
 
    def encrypt_text(self):
        text = self.input_text.toPlainText()
        key_text = self.key_input.text()
        self.key = self.handle_key(key_text)
        cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
        ciphertext = cipher.encrypt(pad(text.encode('utf-8'), AES.block_size))
        encrypted_text = base64.b64encode(ciphertext).decode('utf-8')
        self.output_text.setPlainText(encrypted_text)
 
    def decrypt_text(self):
        text = self.input_text.toPlainText()
        key_text = self.key_input.text()
        self.key = self.handle_key(key_text)
        try:
            ciphertext = base64.b64decode(text.encode('utf-8'))
            cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
            decrypted_text = unpad(cipher.decrypt(ciphertext), AES.block_size).decode('utf-8')
            self.output_text.setPlainText(decrypted_text)
        except ValueError:
            QMessageBox.warning(self, '错误', '解密失败,请检查输入的文本及秘钥是否正确!')
 
    def handle_key(self, key_text):
        key = key_text.encode('utf-8')
        if len(key) > 16:
            key = key[:16]
        elif len(key) < 16:
            key = key.ljust(16, b'0')
        return key
 
 
if __name__ == '__main__':
    app = QApplication([])
    window = TextEncrypter()
    window.show()
    app.exec_()[/md]
if name == 'main':

app = QApplication([])

window = TextEncrypter()

window.show()

app.exec_()

084407zox3233lcq8k2ic2

原文地址

文本加密解密工具,可自定义秘钥,附带源码 – 『编程语言区』 – 吾爱破解 – LCG – LSG |安卓破解|病毒分析|www.52pojie.cn

请登录后发表评论

    没有回复内容