概述
背景:
项目中一些客户端软件 需要本地认证才可以使用
通常会获取电脑唯一标志 在对唯一标志做加密 之后做rsa认证成功之后可以使用
代码编写:
# -*- coding: utf-8 -*-
# @Time : 2022/10/21 9:17
# @Author : hllyzms
# -*- coding: utf-8 -*-
import wmi
import base64
from pyDes import *
class Register:
def __init__(self):
self.Des_key = "abcdefgh" # Key,需八位
self.Des_IV = "x112x2a31x272 " # 自定IV向量
# 获取硬件信息,输出macode
# 1、CPU序列号(ID) 2、本地连接 无线局域网 以太网的MAC 3.硬盘序列号(唯一) 4.主板序列号(唯一)
global s
s = wmi.WMI()
# cpu序列号
def get_CPU_info(self):
cpu = []
cp = s.Win32_Processor()
for u in cp:
cpu.append(
{
"Name": u.Name,
"Serial Number": u.ProcessorId,
"CoreNum": u.NumberOfCores
}
)
return cpu
# 硬盘序列号
def get_disk_info(self):
disk = []
for pd in s.Win32_DiskDrive():
disk.append(
{
"Serial": s.Win32_PhysicalMedia()[0].SerialNumber.lstrip().rstrip(), # 获取硬盘序列号,调用另外一个win32 API
"ID": pd.deviceid,
"Caption": pd.Caption,
"size": str(int(float(pd.Size) / 1024 / 1024 / 1024))
}
)
return disk
# mac地址(包括虚拟机的)
def get_network_info(self):
network = []
for nw in s.Win32_NetworkAdapterConfiguration():
if nw.MacAddress != None:
network.append(
{
"MAC": nw.MacAddress,
"ip": nw.IPAddress
}
)
return network
# 主板序列号
def get_mainboard_info(self):
mainboard = []
for board_id in s.Win32_BaseBoard():
mainboard.append(board_id.SerialNumber.strip().strip('.'))
return mainboard
# 由于机器码矿太长,故选取机器码字符串部分字符
def getCombinNumber(self):
a = self.get_network_info()
b = self.get_CPU_info()
c = self.get_disk_info()
d = self.get_mainboard_info()
machinecode_str = ""
machinecode_str = machinecode_str + a[0]['MAC'] + b[0]['Serial Number'] + c[0]['Serial'] + d[0]
selectIndex = [8, 10, 15, 16, 17, 30, 32, 38, 43, 46]
macode = ""
for i in selectIndex:
macode = macode + machinecode_str[i]
return macode
# DES+base64加密
def Encryted(self, tr):
k = des(self.Des_key, CBC, self.Des_IV, pad=None, padmode=PAD_PKCS5)
EncryptStr = k.encrypt(tr)
return base64.b32encode(EncryptStr) # 转base64编码返回
# 获取注册码,验证成功后生成注册文件
def regist(self):
key = input('please input your register code:')
# 由于输入类似“12”这种不符合base64规则的字符串会引起异常,所以需要增加输入判断
if key:
ontent = self.getCombinNumber()
tent = bytes(ontent, encoding='utf-8')
# 得到加密后机器码
content = self.Encryted(tent)
key_decrypted = bytes(key, encoding='utf-8')
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.")
# 读写文件要加判断
with open('register.txt', 'w') as f:
f.write(key)
f.close()
return True
else:
return False
else:
return False
else:
self.regist()
return False
# 打开程序先调用注册文件,比较注册文件中注册码与此时的硬件信息编码后是否一致
def checkAuthored(self):
ontent = self.getCombinNumber()
tent = bytes(ontent, encoding='utf-8')
# 获得加密后的机器码
content = self.Encryted(tent)
# 读写文件要加判断
try:
f = open('register.txt', 'r')
if f:
key = f.read()
if key:
key_decryted = bytes(key, encoding='utf-8')
if key_decryted:
if key_decryted == content:
print("register succeed.")
else:
print('请重新输入注册码,或发送', ontent, '到13900000000获取注册码')
self.regist()
else:
self.regist()
else:
self.regist()
else:
self.regist()
except:
print('请发送', ontent, '到13900000000获取注册码')
self.regist()
if __name__ == '__main__':
register = Register()
print("***",register.get_disk_info())
print("***",register.get_network_info())
print("***",register.get_mainboard_info())
print("***",register.getCombinNumber())
print(register.Encryted(register.getCombinNumber()))
# register.regist()
# register.checkAuthored()
# *** [{'Serial': '0025_38B3_1102_3060.', 'ID': '\\.\PHYSICALDRIVE0', 'Caption': 'NVMe SAMSUNG MZVL2512HCJQ-00BH1', 'size': '476'}, {'Serial': '0025_38B3_1102_3060.', 'ID': '\\.\PHYSICALDRIVE1', 'Caption': 'NVMe KINGSTON SA2000M8500G', 'size': '465'}]
# *** [{'MAC': '4C:79:6E:8B:07:6D', 'ip': ('192.168.137.118', 'fe80::85e1:6113:3849:833e')}, {'MAC': 'B0:22:7A:F1:CD:B2', 'ip': None}, {'MAC': '4C:79:6E:8B:07:6E', 'ip': None}, {'MAC': 'B2:62:20:52:41:53', 'ip': None}, {'MAC': 'BA:6C:20:52:41:53', 'ip': None}, {'MAC': 'BE:D4:20:52:41:53', 'ip': None}, {'MAC': '4E:79:6E:8B:07:6D', 'ip': None}, {'MAC': '00:50:56:C0:00:01', 'ip': ('169.254.130.55', 'fe80::9c83:b877:1140:8237')}, {'MAC': '00:50:56:C0:00:08', 'ip': ('169.254.139.222', 'fe80::f826:a2b7:f8d7:8bde')}, {'MAC': '00:15:5D:9B:FD:E1', 'ip': ('172.23.144.1', 'fe80::19b7:15d:3b9b:24b5')}, {'MAC': '4C:79:6E:8B:07:71', 'ip': None}]
# *** ['PLSWK018JF74C']
# *** :B6DB6112
# b'UKMYMZ7N7CIYQCYPQ54FDSWVPY======'
最后
以上就是怕黑乐曲为你收集整理的python 获取计算机唯一标志的全部内容,希望文章能够帮你解决python 获取计算机唯一标志所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复