parent
d357047662
commit
29dc7b5562
@ -0,0 +1,45 @@
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto.Random import get_random_bytes
|
||||
from Crypto.Util.Padding import pad, unpad
|
||||
|
||||
def encrypt_message(message, key):
|
||||
"""
|
||||
加密函数
|
||||
:param message: 要加密的明文消息
|
||||
:param key: 加密密钥
|
||||
:return: 密文
|
||||
"""
|
||||
# 创建 AES 对象
|
||||
cipher = AES.new(key, AES.MODE_CBC)
|
||||
|
||||
# 对明文进行填充,然后加密
|
||||
ct_bytes = cipher.encrypt(pad(message.encode(), AES.block_size))
|
||||
|
||||
# 将 IV 和密文返回
|
||||
return cipher.iv, ct_bytes
|
||||
|
||||
def decrypt_message(iv, ct, key):
|
||||
"""
|
||||
解密函数
|
||||
:param iv: 初始化向量
|
||||
:param ct: 密文
|
||||
:param key: 加密密钥
|
||||
:return: 解密后的明文
|
||||
"""
|
||||
# 创建一个新的 AES 对象
|
||||
cipher = AES.new(key, AES.MODE_CBC, iv)
|
||||
|
||||
# 解密并去除填充
|
||||
pt = unpad(cipher.decrypt(ct), AES.block_size).decode()
|
||||
|
||||
return pt
|
||||
|
||||
# 测试
|
||||
key = get_random_bytes(16) # 生成随机密钥
|
||||
message = "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"
|
||||
|
||||
iv, ct = encrypt_message(message, key)
|
||||
print("Encrypted:", ct)
|
||||
|
||||
pt = decrypt_message(iv, ct, key)
|
||||
print("Decrypted:", pt)
|
@ -1,19 +1,5 @@
|
||||
from openai import OpenAI
|
||||
import openai
|
||||
import os
|
||||
openai.api_base = "https://api.openai-proxy.com"
|
||||
openai.api_key = "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"
|
||||
# os.environ['http_proxy'] = 'http://127.0.0.1:10809'
|
||||
# os.environ['https_proxy'] = 'http://127.0.0.1:10809'
|
||||
client = OpenAI(
|
||||
api_key='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',
|
||||
base_url='https://api.openai-proxy.com'
|
||||
)
|
||||
|
||||
resp = client.chat.completions.create(
|
||||
model='gpt-3.5-turbo',
|
||||
messages=[
|
||||
{'role': 'user', 'content': '你好!'}
|
||||
]
|
||||
)
|
||||
print(resp.choices[0].message.content)
|
||||
import json
|
||||
with open('demo.json','r',encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
for i,j in enumerate(data.items()):
|
||||
print(i,j[-1])
|
@ -1,98 +1,134 @@
|
||||
import sys
|
||||
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, QLabel
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtGui import QPixmap, QPainter
|
||||
|
||||
class MyApp(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.initUI()
|
||||
|
||||
def initUI(self):
|
||||
# 加载背景图片
|
||||
self.background = QPixmap('hnu.jpg')
|
||||
|
||||
# 创建一个垂直布局
|
||||
mainLayout = QVBoxLayout()
|
||||
|
||||
# 创建三个文本框并设置占位符
|
||||
self.nameEdit = QLineEdit()
|
||||
self.pwdEdit = QLineEdit()
|
||||
self.urlEdit = QLineEdit()
|
||||
self.nameEdit.setPlaceholderText('Name')
|
||||
self.pwdEdit.setPlaceholderText('Password')
|
||||
self.urlEdit.setPlaceholderText('URL')
|
||||
|
||||
# 创建一个按钮并连接信号
|
||||
btn = QPushButton('提交')
|
||||
btn.clicked.connect(self.onSubmit)
|
||||
|
||||
# 对于每个输入框,创建一个水平布局以保持居中
|
||||
for label_text, edit_widget in [("用户名", self.nameEdit),
|
||||
("密码", self.pwdEdit),
|
||||
("实训网址", self.urlEdit)]:
|
||||
hbox = QHBoxLayout()
|
||||
hbox.addStretch()
|
||||
vbox_inner = QVBoxLayout()
|
||||
vbox_inner.addWidget(QLabel(label_text))
|
||||
vbox_inner.addWidget(edit_widget)
|
||||
hbox.addLayout(vbox_inner)
|
||||
hbox.addStretch()
|
||||
mainLayout.addLayout(hbox)
|
||||
|
||||
# 添加按钮到布局
|
||||
mainLayout.addWidget(btn, 0, Qt.AlignCenter)
|
||||
|
||||
# 设置窗口的布局
|
||||
self.setLayout(mainLayout)
|
||||
self.setWindowTitle('')
|
||||
self.setGeometry(300, 300, 400, 300)
|
||||
self.centerWindow()
|
||||
|
||||
# 应用样式
|
||||
self.applyStyles()
|
||||
|
||||
def paintEvent(self, event):
|
||||
painter = QPainter(self)
|
||||
|
||||
# 计算背景图片绘制的起始坐标,使其位于窗口中心
|
||||
bgWidth = self.background.width()
|
||||
bgHeight = self.background.height()
|
||||
startX = (self.width() - bgWidth) // 2
|
||||
startY = (self.height() - bgHeight) // 2
|
||||
|
||||
painter.drawPixmap(startX, startY, self.background)
|
||||
|
||||
def centerWindow(self):
|
||||
qr = self.frameGeometry()
|
||||
cp = QApplication.desktop().availableGeometry().center()
|
||||
qr.moveCenter(cp)
|
||||
self.move(qr.topLeft())
|
||||
|
||||
def applyStyles(self):
|
||||
self.setStyleSheet("""
|
||||
QLineEdit {
|
||||
border: 1px solid gray;
|
||||
border-radius: 10px;
|
||||
padding: 5px;
|
||||
background: transparent;
|
||||
}
|
||||
QLineEdit:focus {
|
||||
border: 1px solid LightSeaGreen;
|
||||
}
|
||||
""")
|
||||
|
||||
def onSubmit(self):
|
||||
name = self.nameEdit.text()
|
||||
pwd = self.pwdEdit.text()
|
||||
url = self.urlEdit.text()
|
||||
print(f"Name: {name}, Password: {pwd}, URL: {url}")
|
||||
|
||||
# 关闭窗口
|
||||
self.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = QApplication(sys.argv)
|
||||
ex = MyApp()
|
||||
ex.show()
|
||||
sys.exit(app.exec_())
|
||||
'''
|
||||
本模块用于创建登录界面并保存用户信息
|
||||
'''
|
||||
import sys
|
||||
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, QLabel
|
||||
from PyQt5.QtCore import Qt,QTimer
|
||||
from PyQt5.QtGui import QPixmap, QPainter
|
||||
import os
|
||||
import platform
|
||||
import json
|
||||
|
||||
class MyApp(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.initUI()
|
||||
|
||||
def initUI(self):
|
||||
# 加载背景图片
|
||||
self.background = QPixmap()
|
||||
# 创建一个垂直布局
|
||||
mainLayout = QVBoxLayout()
|
||||
|
||||
# 创建三个文本框并设置占位符
|
||||
self.nameEdit = QLineEdit()
|
||||
self.pwdEdit = QLineEdit()
|
||||
self.urlEdit = QLineEdit()
|
||||
|
||||
# 创建一个按钮并连接信号
|
||||
btn = QPushButton('提交')
|
||||
btn.clicked.connect(self.onSubmit)
|
||||
|
||||
# 创建一个用于显示错误信息的标签
|
||||
self.errorLabel = QLabel('', self)
|
||||
self.errorLabel.hide() # 初始时隐藏该标签
|
||||
|
||||
self.setWindowTitle('头歌助手登录')
|
||||
|
||||
# 对于每个输入框,创建一个水平布局以保持居中
|
||||
for label_text, edit_widget in [("用户名", self.nameEdit),
|
||||
("密 码", self.pwdEdit),
|
||||
("实训网址", self.urlEdit)]:
|
||||
hbox = QHBoxLayout()
|
||||
hbox.addStretch()
|
||||
vbox_inner = QVBoxLayout()
|
||||
vbox_inner.addWidget(QLabel(label_text))
|
||||
vbox_inner.addWidget(edit_widget)
|
||||
hbox.addLayout(vbox_inner)
|
||||
hbox.addStretch()
|
||||
mainLayout.addLayout(hbox)
|
||||
|
||||
# 添加按钮和错误信息标签到布局
|
||||
mainLayout.addWidget(btn, 0, Qt.AlignCenter)
|
||||
mainLayout.addWidget(self.errorLabel, 0, Qt.AlignCenter)
|
||||
|
||||
# 设置窗口的布局
|
||||
self.setLayout(mainLayout)
|
||||
self.setGeometry(300, 300, 650, 550)
|
||||
self.centerWindow()
|
||||
|
||||
# 应用样式
|
||||
self.applyStyles()
|
||||
|
||||
def paintEvent(self, event):
|
||||
painter = QPainter(self)
|
||||
painter.drawPixmap(self.rect(), self.background)
|
||||
|
||||
def centerWindow(self):
|
||||
qr = self.frameGeometry()
|
||||
cp = QApplication.desktop().availableGeometry().center()
|
||||
qr.moveCenter(cp)
|
||||
self.move(qr.topLeft())
|
||||
|
||||
def applyStyles(self):
|
||||
self.setStyleSheet("""
|
||||
QLineEdit {
|
||||
border: 1px solid gray;
|
||||
border-radius: 10px;
|
||||
padding: 5px;
|
||||
background: rgba(255, 255, 255, 100);
|
||||
color: black;
|
||||
width: 300px;
|
||||
height: 30px;
|
||||
}
|
||||
QLineEdit:focus {
|
||||
border: 3px solid black;
|
||||
}
|
||||
QLabel {
|
||||
color: black;
|
||||
font-size: 20pt;
|
||||
font-family: "Microsoft YaHei";
|
||||
}
|
||||
QPushButton {
|
||||
border: 1px solid gray;
|
||||
border-radius: 10px;
|
||||
background: rgba(255, 255, 255, 100);
|
||||
color: black;
|
||||
width: 100px;
|
||||
height: 27px;
|
||||
}
|
||||
/* 样式用于错误信息标签 */
|
||||
QLabel#errorLabel {
|
||||
color: red;
|
||||
font-size: 16pt;
|
||||
}
|
||||
""")
|
||||
|
||||
def onSubmit(self):
|
||||
name = self.nameEdit.text()
|
||||
pwd = self.pwdEdit.text()
|
||||
url = self.urlEdit.text()
|
||||
|
||||
if name == '' or pwd == '' or url == '':
|
||||
self.showError("非法输入!")
|
||||
else:
|
||||
with open('userinfo.json', 'w') as f:
|
||||
json.dump({'name': name, 'pwd': pwd, 'url': url}, f)
|
||||
self.close()
|
||||
|
||||
def showError(self, message):
|
||||
self.errorLabel.setText(message)
|
||||
self.errorLabel.show()
|
||||
QTimer.singleShot(2000, self.hideError)
|
||||
|
||||
def hideError(self):
|
||||
self.errorLabel.hide()
|
||||
|
||||
def main():
|
||||
app = QApplication(sys.argv)
|
||||
ex = MyApp()
|
||||
ex.show()
|
||||
sys.exit(app.exec_())
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Loading…
Reference in new issue