pyqt密码加密解密
时间: 2024-01-26 15:14:39 浏览: 206
以下是一个使用PyQt5编写的密码加密解密小程序的示例:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox
# 凯撒加密函数
def caesar_cipher(text, shift):
result = ""
for char in text:
if char.isalpha():
ascii_offset = ord('a') if char.islower() else ord('A')
shifted_char = chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset)
result += shifted_char
else:
result += char
return result
# 维吉尼亚加密函数
def vigenere_cipher(text, key):
result = ""
key_index = 0
for char in text:
if char.isalpha():
ascii_offset = ord('a') if char.islower() else ord('A')
key_shift = ord(key[key_index % len(key)]) - ascii_offset
shifted_char = chr((ord(char) - ascii_offset + key_shift) % 26 + ascii_offset)
result += shifted_char
key_index += 1
else:
result += char
return result
# 仿射加密函数
def affine_cipher(text, a, b):
result = ""
for char in text:
if char.isalpha():
ascii_offset = ord('a') if char.islower() else ord('A')
shifted_char = chr(((ord(char) - ascii_offset) * a + b) % 26 + ascii_offset)
result += shifted_char
else:
result += char
return result
# 培根加密函数
def bacon_cipher(text):
bacon_dict = {
'A': 'AAAAA', 'B': 'AAAAB', 'C': 'AAABA', 'D': 'AAABB', 'E': 'AABAA',
'F': 'AABAB', 'G': 'AABBA', 'H': 'AABBB', 'I': 'ABAAA', 'J': 'ABAAB',
'K': 'ABABA', 'L': 'ABABB', 'M': 'ABBAA', 'N': 'ABBAB', 'O': 'ABBBA',
'P': 'ABBBB', 'Q': 'BAAAA', 'R': 'BAAAB', 'S': 'BAABA', 'T': 'BAABB',
'U': 'BABAA', 'V': 'BABAB', 'W': 'BABBA', 'X': 'BABBB', 'Y': 'BBAAA',
'Z': 'BBAAB'
}
result = ""
for char in text:
if char.isalpha():
bacon_char = bacon_dict[char.upper()]
result += bacon_char
else:
result += char
return result
class PasswordEncryptionApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("密码加密解密")
self.layout = QVBoxLayout()
self.label_text = QLabel("请输入要加密/解密的文本:")
self.layout.addWidget(self.label_text)
self.text_input = QLineEdit()
self.layout.addWidget(self.text_input)
self.label_key = QLabel("请输入密钥:")
self.layout.addWidget(self.label_key)
self.key_input = QLineEdit()
self.layout.addWidget(self.key_input)
self.button_caesar = QPushButton("凯撒加密")
self.button_caesar.clicked.connect(self.caesar_encrypt)
self.layout.addWidget(self.button_caesar)
self.button_vigenere = QPushButton("维吉尼亚加密")
self.button_vigenere.clicked.connect(self.vigenere_encrypt)
self.layout.addWidget(self.button_vigenere)
self.button_affine = QPushButton("仿射加密")
self.button_affine.clicked.connect(self.affine_encrypt)
self.layout.addWidget(self.button_affine)
self.button_bacon = QPushButton("培根加密")
self.button_bacon.clicked.connect(self.bacon_encrypt)
self.layout.addWidget(self.button_bacon)
self.setLayout(self.layout)
self.show()
def caesar_encrypt(self):
text = self.text_input.text()
shift = int(self.key_input.text())
encrypted_text = caesar_cipher(text, shift)
QMessageBox.information(self, "凯撒加密结果", encrypted_text)
def vigenere_encrypt(self):
text = self.text_input.text()
key = self.key_input.text()
encrypted_text = vigenere_cipher(text, key)
QMessageBox.information(self, "维吉尼亚加密结果", encrypted_text)
def affine_encrypt(self):
text = self.text_input.text()
a = int(self.key_input.text().split(',')[0])
b = int(self.key_input.text().split(',')[1])
encrypted_text = affine_cipher(text, a, b)
QMessageBox.information(self, "仿射加密结果", encrypted_text)
def bacon_encrypt(self):
text = self.text_input.text()
encrypted_text = bacon_cipher(text)
QMessageBox.information(self, "培根加密结果", encrypted_text)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = PasswordEncryptionApp()
sys.exit(app.exec_())
```
这个程序使用了PyQt5库来创建一个带有用户界面的密码加密解密小程序。它包括凯撒加密、维吉尼亚加密、仿射加密和培根加密四种加密算法。用户可以输入要加密/解密的文本和密钥,然后选择相应的加密算法进行加密操作。加密结果将以消息框的形式显示出来。
阅读全文