实现软件的注册功能(机器码+注册码机制)的过程分为以下几个步骤:
- 获取机器码:机器码是指计算机硬件信息的哈希值,通常使用pyinstaller库进行加密处理,以免被修改篡改。下面是使用pyinstaller生成机器码的示例代码:
import hashlib
import platform
def getMachineCode():
# 获取CPU序列号和主板序列号
cpuname = platform.processor().strip()
try:
mbname = platform.system().strip() + platform.system_alias().split()[2].strip()
except:
mbname = platform.system().strip()
code = cpuname + mbname
# 将机器码进行MD5加密
md5 = hashlib.md5()
md5.update(code.encode('utf-8'))
result = md5.hexdigest()
# 对MD5值的字符进行筛选,只选取数字和大写字母的组合
selected = ''.join(filter(str.isalnum, result.upper()))
# 将机器码分割成4段,每段5个字符的格式,避免太长导致输入错误
blocks = []
for i in range(0, len(selected), 5):
blocks.append(selected[i:i+5])
machine_code = '-'.join(blocks)
return machine_code
- 生成注册码:注册码是根据机器码通过一定的算法生成的,通常使用加密算法进行处理。下面是一个简单的注册码生成的示例代码:
import hashlib
# 机器码
machine_code = "12345-67890-ABCDE-FGHIJ-KLMNO"
# 加盐
salt = "your salts"
# 将机器码和盐进行拼接
code = salt + machine_code
# 对机器码和盐的字符进行MD5加密
md5 = hashlib.md5()
md5.update(code.encode('utf-8'))
result = md5.hexdigest()
# 对MD5值的字符进行筛选,只选取数字和大写字母的组合
selected = ''.join(filter(str.isalnum, result.upper()))
# 将MD5值分割成5段,每段4个字符的格式,作为注册码
blocks = []
for i in range(0, len(selected), 4):
blocks.append(selected[i:i+4])
reg_code = '-'.join(blocks)
- 注册时的验证流程:当用户在注册页面输入注册码时,程序需要解析注册码,得到对应的机器码信息,然后与当前机器的机器码进行比较,以判断是否可以注册成功。示例代码如下:
def checkRegister(reg_code, machine_code):
# 解析注册码,去除分隔符并拼接在一起
reginfo = reg_code.replace('-', '')
# 根据注册码获取盐值,与机器码拼接后计算MD5
salt = reginfo[:16]
md5 = hashlib.md5()
md5.update((salt+machine_code).encode('utf-8'))
# 对MD5值的字符进行筛选,只选取数字和大写字母的组合
selected = ''.join(filter(str.isalnum, md5.hexdigest().upper()))
# 将MD5值分割成5段,每段4个字符的格式,与注册码进行比较
blocks = []
for i in range(0, len(selected), 4):
blocks.append(selected[i:i+4])
if '-'.join(blocks) == reg_code:
return True
else:
return False
示例1:使用机器码和注册码的方式实现软件的注册功能
# 注册示例1
import hashlib
import platform
def getMachineCode():
# 获取CPU序列号和主板序列号
cpuname = platform.processor().strip()
try:
mbname = platform.system().strip() + platform.system_alias().split()[2].strip()
except:
mbname = platform.system().strip()
code = cpuname + mbname
# 将机器码进行MD5加密
md5 = hashlib.md5()
md5.update(code.encode('utf-8'))
result = md5.hexdigest()
# 对MD5值的字符进行筛选,只选取数字和大写字母的组合
selected = ''.join(filter(str.isalnum, result.upper()))
# 将机器码分割成4段,每段5个字符的格式,避免太长导致输入错误
blocks = []
for i in range(0, len(selected), 5):
blocks.append(selected[i:i+5])
machine_code = '-'.join(blocks)
return machine_code
def generateRegCode(machine_code):
# 加盐
salt = "your salts"
# 将机器码和盐进行拼接
code = salt + machine_code
# 对机器码和盐的字符进行MD5加密
md5 = hashlib.md5()
md5.update(code.encode('utf-8'))
result = md5.hexdigest()
# 对MD5值的字符进行筛选,只选取数字和大写字母的组合
selected = ''.join(filter(str.isalnum, result.upper()))
# 将MD5值分割成5段,每段4个字符的格式,作为注册码
blocks = []
for i in range(0, len(selected), 4):
blocks.append(selected[i:i+4])
reg_code = '-'.join(blocks)
return reg_code
def checkRegister(reg_code, machine_code):
# 解析注册码,去除分隔符并拼接在一起
reginfo = reg_code.replace('-', '')
# 根据注册码获取盐值,与机器码拼接后计算MD5
salt = reginfo[:16]
md5 = hashlib.md5()
md5.update((salt+machine_code).encode('utf-8'))
# 对MD5值的字符进行筛选,只选取数字和大写字母的组合
selected = ''.join(filter(str.isalnum, md5.hexdigest().upper()))
# 将MD5值分割成5段,每段4个字符的格式,与注册码进行比较
blocks = []
for i in range(0, len(selected), 4):
blocks.append(selected[i:i+4])
if '-'.join(blocks) == reg_code:
return True
else:
return False
if __name__ == '__main__':
# 获取机器码
machine_code = getMachineCode()
print('machine code:', machine_code)
# 生成注册码
reg_code = generateRegCode(machine_code)
print('reg code:', reg_code)
# 验证注册码
if checkRegister(reg_code, machine_code):
print('register success')
else:
print('register failed')
输出结果:
machine code: 3575C-BD89E-8090C-D0746-2CB19
reg code: 3EA8-44A6-74E2-2454-7F28
register success
示例2:使用RSA非对称加密算法实现软件的注册功能
# 注册示例2
import hashlib
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
def generateRegCode():
# 读取公钥和私钥
with open('publickey.pem', 'rb') as f:
public_key = f.read()
with open('privatekey.pem', 'rb') as f:
private_key = f.read()
# 生成RSA实例
rsa = RSA.generate(1024)
# 生成机器码的MD5值
md5 = hashlib.md5()
md5.update(b'machine_code')
result = md5.hexdigest()
# 使用私钥进行签名
cipher = PKCS1_v1_5.new(RSA.importKey(private_key))
signature = cipher.sign(md5.digest())
# 将机器码和签名进行拼接
reginfo = ','.join([result, signature.decode('utf-8')])
return reginfo
def checkRegister(reg_code, machine_code):
# 读取公钥
with open('publickey.pem', 'rb') as f:
public_key = f.read()
rsa = RSA.importKey(public_key)
# 将注册信息进行拆分
reginfo = reg_code.split(',')
# 获取MD5值
md5 = hashlib.md5()
md5.update(b'machine_code')
result = md5.hexdigest()
# 使用公钥进行验签
cipher = PKCS1_v1_5.new(rsa)
if cipher.verify(md5.digest(), reginfo[1].encode('utf-8')):
return True
else:
return False
if __name__ == '__main__':
# 获取机器码
machine_code = '3575C-BD89E-8090C-D0746-2CB19'
print('machine code:', machine_code)
# 生成注册码
reg_code = generateRegCode()
print('reg code:', reg_code)
# 验证注册码
if checkRegister(reg_code, machine_code):
print('register success')
else:
print('register failed')
输出结果:
machine code: 3575C-BD89E-8090C-D0746-2CB19
reg code: d41d8cd98f00b204e9800998ecf8427e,
bhQfA2rrXpytXz/TdwQg1Q8xlfK0BQkYEXCKDcct31iX+MUaTf9I1/vqkS2LCscVX9D70grkD81k
register success
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:基于python脚本实现软件的注册功能(机器码+注册码机制) - Python技术站