通过RSA私钥签名,公钥验签的原理进行简单实现一机一码授权。
RSA实例2.1实现一机一码客服端.py
[Python] 纯文本查看 复制代码 import wmi # wmi依赖pywin32,python3.5以上版本安装pywin32需要手动安装
import hashlib
import os
import rsa
class c_一机一码验证:
def __init__(self):
pass
def m_获取处理器信息(self):
v_处理器信息列表 = []
for u in wmi.WMI().Win32_Processor():
v_处理器信息列表.append(
{
"Name": u.Name,
"Serial Number": u.ProcessorId,
"CoreNum": u.NumberOfCores
}
)
return v_处理器信息列表
def m_生成机器码(self):
v_处理器信息 = self.m_获取处理器信息()
v_处理器序列号 = v_处理器信息[0]['Serial Number']
v_处理器核心数量 = v_处理器信息[0]['CoreNum']
# 计算得到数字码
v_处理器数字码 = int(bytes(v_处理器序列号, 'UTF-8').hex(), 16) ** v_处理器核心数量
v_数字码哈希值 = hashlib.md5(str(v_处理器数字码).encode(encoding='UTF-8')).hexdigest()
v_机器码 = f'{v_数字码哈希值[0:5]}-{v_数字码哈希值[5:10]}-{v_数字码哈希值[10:15]}-{v_数字码哈希值[15:20]}'.upper()
return v_机器码
def m_公钥(self):
v公钥b64 = '''
-----BEGIN RSA PUBLIC KEY-----
MIICCgKCAgEAlRr4y/mxct7pYMcbhuW3kW4uN4EoYRLNEPoOJhuQhvgOp/9mJyXC
sRq/k0WZIa56Zs2ulOUIGYuOptQ6dlCHVKx1OG+dMBufRPlJJht9pw0ZU4sdcEBd
Lssrw1nzwNeMOfqGWmiIifB3ERGMQvq8M3wrB6XSOmI6HWShHKiXggHaWNKiZ8WS
AiK0cBx8Uu8GuOTVYZLdwtPBdqwj4z0Trxi1+fvXJxL4mPBPN3gLxUyVAdV8ancO
sAQdnxxgkt5WX5bi6JVVMQaTtlCGag1THP3y3Poy+sNDn39DnvDaZBOOF2ZMZSlk
clzQ9WjcBvXz/Ou48MwtxceTaQuIcbFebafHjeARXQ6Lp3P+oBBuAfjdHPdNsho1
qd/1g+Fa1972HHmcpgzo3d6xc1DoPYnwINeNTDr9iGRR95tXYud+waZp1qMcL46O
xprlJ7qtXH+XYBpUkrUavkYPNFJyKhq32tijDAnm9T2eAZ1HRn4nwOmBqT+CiRUy
48fb97JxUuNV2hN7Z37rliIzEBT3ergBRWIihxpeqvYC/6Aoi8o1gdfh9fiNBVJj
p98HeiNSUNay4foY+G0prbozzGaVHw5x/ZpszySh3JBMM/yGIcZPA6odgQe4+9iN
Q22Nt7E6ppaT5WIDVBpebTNfgKapISdMkrcLkYUKCQuLAFU3OpZfEakCAwEAAQ==
-----END RSA PUBLIC KEY-----
'''
v_公钥 = rsa.PublicKey.load_pkcs1(v公钥b64.encode())
return v_公钥
def m_验证激活(self):
v_机器码 = self.m_生成机器码()
if os.path.isfile('激活码'):
with open(file='激活码', mode='rb') as f:
v_激活码 = f.read()
try:
xx = rsa.verify(v_机器码.encode(), v_激活码, self.m_公钥())
if xx == 'SHA-1':
# print('激活成功')
return True
except BaseException as e:
print(f'激活失败:{v_机器码}')
else:
print(f'未激活:{v_机器码}')
if c_一机一码验证().m_验证激活():
print('验证通过,')
RSA实例2.2实现一机一码服务端.py
[Python] 纯文本查看 复制代码 import rsa
v_私钥b64 = '''
-----BEGIN RSA PRIVATE KEY-----
MIIJNgIBAAKCAgEAlRr4y/mxct7pYMcbhuW3kW4uN4EoYRLNEPoOJhuQhvgOp/9m
JyXCsRq/k0WZIa56Zs2ulOUIGYuOptQ6dlCHVKx1OG+dMBufRPlJJht9pw0ZU4sd
cEBdLssrw1nzwNeMOfqGWmiIifB3ERGMQvq8M3wrB6XSOmI6HWShHKiXggHaWNKi
Z8WSAiK0cBx8Uu8GuOTVYZLdwtPBdqwj4z0Trxi1+fvXJxL4mPBPN3gLxUyVAdV8
ancOsAQdnxxgkt5WX5bi6JVVMQaTtlCGag1THP3y3Poy+sNDn39DnvDaZBOOF2ZM
ZSlkclzQ9WjcBvXz/Ou48MwtxceTaQuIcbFebafHjeARXQ6Lp3P+oBBuAfjdHPdN
sho1qd/1g+Fa1972HHmcpgzo3d6xc1DoP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QVRISOAu52Qvc
bOy+cfQJ/NxvsutSwl6gUEEXJ5sRjmnY1zcs+N1v/LkQ9Yg24blswA+jaGHAVAwQ
joOxSAgZh1VNEvjqGnlLROsxQyGzQRqzwFMmvTvpBcTelkpEGMhX1vJlAVaPhesA
rMcOc6CRgu79ZPTHjOKGy8WjMLed2hiKwK4R7hQnsaR00DNab3bjKQ5hoX1vGbbD
ma/Cqh2oEY4hyWQuBBUWX6SjnIBl5wKCARA9phDIkRPWz/gMkAvAg43pYJvSegK2
cINUKb80vRu5T9dFKhAJjEH6CKbJ3TZFt5DASbGe5DYKLXLTnYdqzKZS5TBOvgeh
6A+2I8AdVIzgIIycSG0pbjv5Vs4jGSbpySF/TTWfvpDnrBrcuoExgsfV+5ONqJCz
BChQqwJYJGPYmBQfgONKrwcygpxBHdE3R+47SQKo0JO6gtN30i3iroaBmnp0x5ER
BG3o6YoAWFYvvSExxZIo6To8Kgys9K8i8SLbcTz4sd/bEMEDGJI7lk+ZkfrVAQ1U
1KiwnlMjQsye4qsF0gLLjkjevXyGUpDKPsPcRLxj8f826UtH9Yig+EtZyJc+T+8U
/FENCT5S7PrZYg==
-----END RSA PRIVATE KEY-----
'''
v_私钥 = rsa.PrivateKey.load_pkcs1(v_私钥b64.encode())
v_需要签名的信息 = input('输入机器码:')
v_签名数据 = rsa.sign(v_需要签名的信息.encode(), v_私钥, 'SHA-1')
with open(file='激活码', mode='wb') as f:
f.write(v_签名数据)
你可以把客户端的代码添加到你自己写的代码前,启动的时候验证一下,验证成功就执行。失败则退出。
只要保管好服务端的签名秘钥就行了。把激活码文件发个客服端就可以了。
客服端:获取CPU信息,简单加密,生产一串机器码。
服务端:签名机器码,生产激活码文件。发给客服端
如果需要更改公钥和私钥,可以参考我的上一条:python之初学RAS库实现:加密,解密,签名,验签,RSA实例1.1生产秘钥.py 代码
|