一直有想过一个问题,如何做一个类似于授权的程序,于是通过网上搜索学习,有了接下来的程序。
如何利用机器码进行注册加密验证:1:选取c盘序列号,一般每台电脑的c盘序列号都不一样 2:将序列号进行des加密并用base64编码 3:解密验证
import base64
import win32api
from pyDes import *
import binascii
class register:
def __init__(self):
self.Des_Key = b"BHC#@*UM" # Key(可换)
self.Des_IV = b"\x22\x33\x35\x81\xBC\x38\x5A\xE7" # 自定义IV向量(可换)
self.str = self.getCVolumeSerialNumber()
# 获取C盘卷序列号
def getCVolumeSerialNumber(self):
CVolumeSerialNumber = win32api.GetVolumeInformation("C:\\")[1]
#print(CVolumeSerialNumber)
if CVolumeSerialNumber:
return str(CVolumeSerialNumber)
else:
return 0
# 使用DES加base64的形式加密
def DesEncrypt(self):
k = des(self.Des_Key, CBC, self.Des_IV, pad=None, padmode=PAD_PKCS5)
EncryptStr = k.encrypt(self.str)
#print(EncryptStr)
#self.str1=EncryptStr
#print(base64.b64encode(EncryptStr))
return base64.b64encode(EncryptStr) # 转base64编码返回
# 获取注册码,验证
def regist(self):
key = input('please input your register code: ')
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:
print("register succeed.")
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)
#print(DecryptStr.decode('utf-8'))
return DecryptStr.decode('utf-8')
if __name__ == '__main__':
reg = register()
reg.regist()
引用:参考