文本加密解密工具,可自定义秘钥,附带源码
下面附源码
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_() 学一学吧 谢谢楼主分享 程序写得很好,谢谢楼主分享。。。 应用程序在哪里? 没有成品的分享链接吗? 见过一个跑字典破wifi的 思路有点相似 谢谢了很有帮助! 感谢大佬分享,十分有用 麻烦分享一下成品链接吗谢谢