You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DCS/model/ClientModel/authentication.py

153 lines
4.5 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import base64
import win32api
from pyDes import *
import binascii
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog, QWidget, QLineEdit, QPushButton, QHBoxLayout, QVBoxLayout, QMessageBox, QLabel
from PyQt5 import QtCore, QtGui, QtWidgets
import os
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
# self.setupUi()
def setupUi(self):
layout = QVBoxLayout()
# 第一行
label1 = QLabel('Label 1')
#第二行
input1 = QLineEdit()
# 第三行
label2 = QLabel('Label 2')
# 第四行
input2 = QLineEdit()
# 第五行
button = QPushButton('Button')
layout.addWidget(button, alignment=Qt.AlignCenter)
self.show()
def initUI(self):
# 创建两个 QLineEdit 控件
self.line_edit1 = QLineEdit()
self.line_edit2 = QLineEdit()
self.label1 = QLabel('输入盘符号:')
self.label2 = QLabel('激活码:')
self.reg = register()
self.button2 = QPushButton('生成激活码')
self.button2.clicked.connect(self.check_license)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
vbox = QVBoxLayout()
vbox.addItem(spacerItem2)
vbox.addWidget(self.label1)
vbox.addWidget(self.line_edit1)
vbox.addWidget(self.label2)
vbox.addWidget(self.line_edit2)
vbox.addItem(spacerItem2)
vbox1 = QHBoxLayout()
vbox1.addItem(spacerItem)
vbox1.addWidget(self.button2)
vbox1.addItem(spacerItem)
vbox2 = QVBoxLayout()
vbox2.addLayout(vbox)
vbox2.addLayout(vbox1)
vbox2.addItem(spacerItem2)
# 设置窗口布局,并设置窗口的标题和大小
self.setLayout(vbox2)
self.setWindowTitle('产品许可激活')
self.resize(300, 300)
# 显示窗口
self.show()
def check_license(self):
if self.line_edit1.text():
licese = self.line_edit1.text()
code = self.reg.DesEncrypt(licese).decode('utf-8')
self.line_edit2.setText(code)
else:
reply = QMessageBox.question(self.parent(),
'提示',
"请输入盘符号",
QMessageBox.Yes)
return
class register:
def __init__(self):
self.Des_Key = b"BHC#@*AW" # Key可换
self.Des_IV = b"\x22\x33\x35\x81\xBC\x38\x5A\xE7" # 自定义IV向量可换
# 获取C盘卷序列号
def getCVolumeSerialNumber(self):
CVolumeSerialNumber = win32api.GetVolumeInformation("C:\\")[1]
# print(CVolumeSerialNumber)
if CVolumeSerialNumber:
return str(CVolumeSerialNumber)
else:
return 0
# 使用DES加base64的形式加密
def DesEncrypt(self, str):
# str = str
k = des(self.Des_Key, CBC, self.Des_IV, pad=None, padmode=PAD_PKCS5)
EncryptStr = k.encrypt(str)
print(type(EncryptStr))
return base64.b64encode(EncryptStr) # 转base64编码返回
# 获取注册码,验证
def regist(self, key):
key = key
self.str1 = base64.b64decode(key)
# 由于输入不符合base64规则的字符串会引起异常所以需要增加输入判断
if key:
content = self.getCVolumeSerialNumber()
key_decrypted = str(self.DesDecrypt())
if content != 0 and key_decrypted != 0:
if content != key_decrypted:
print("wrong register code, please check and input your register code again:")
self.regist()
elif content == key_decrypted:
return True
else:
return False
else:
return False
else:
self.regist()
return False
# des解码
def DesDecrypt(self):
k = des(self.Des_Key, CBC, self.Des_IV, pad=None, padmode=PAD_PKCS5)
DecryptStr = k.decrypt(self.str1)
return DecryptStr.decode('utf-8')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())