软件授权码方案(附Python示例代码)

思路:

软件安装后,运行软件时,通过电脑机器码的唯一性实现授权码的唯一性。(机器码:由cpu序列号、硬盘序列号、mac地址、主板序列号组成的字符串截取部分字符)

方案:

1、运行软件,检测注册文件,若未检测到注册文件或者注册文件中的注册码与机器码进行DES+base64加密后的编码不一致,则提醒用户输入注册码或重新获取注册码。重新获取注册码会将程序运行后显示的机器组合码 发送给指定管理员,管理员经过编码生成注册码给回用户,用户输入,生成注册文件,进入软件。

2、注册文件中注册码与机器码进行DES+base64加密后的编码一致,则通过认证 进入软件。

软件授权码方案(附Python示例代码)_第1张图片

代码:

# -*- coding: utf-8 -*-

import wmi
import base64
from pyDes import *

class Register:
    def __init__(self):
        self.Des_key = "abcdefgh" #Key,需八位
        self.Des_IV = "\x11\2\x2a\3\1\x27\2\0"  # 自定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()

参考地址:python读取电脑识别码_三更雪的博客-CSDN博客_python获取机器码

你可能感兴趣的:(Python,知识产权,python,开发语言,后端)