ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python 的licence授权机制

2022-06-16 09:33:10  阅读:244  来源: 互联网

标签:file license python self content mac licence time 授权


import re
import datetime
from Crypto.Cipher import AES  #安装报错参考 https://blog.csdn.net/pittpakk/article/details/120021977
from binascii import a2b_hex
from binascii import b2a_hex
import sys
import os
import json
import uuid

class get_license_file():
    #生成license
    def __init__(self, mac, deadline_use_time, pwd):
        self.mac = mac
        self.deadline_use_time = deadline_use_time
        self.pwd = pwd

    def encrypt(self, content):
        # content length must be a multiple of 16.
        while len(content) % 16:
            content += ' '
        content = content.encode('utf-8')
        # Encrypt content.
        aes = AES.new(self.pwd.encode(), AES.MODE_CBC, self.pwd.encode())
        encrypted_content = aes.encrypt(content)
        return (b2a_hex(encrypted_content))

    def gen_license_file(self):
        license_file = 'License.dat'
        with open(license_file, 'w') as LF:
            LF.write('MAC : %s\n' % (self.mac))
            LF.write('Deadline_use_time : %s\n' % (self.deadline_use_time))
            sign = self.encrypt('%s#%s' % (self.mac, self.deadline_use_time))
            print('Sign : ' + str(sign.decode('utf-8')) + '\n')
            LF.write('Sign : ' + str(sign.decode('utf-8')) + '\n')

        #生成登陆时间信息
        time_file = 'time.txt'
        current_date = datetime.datetime.now().strftime('%Y%m%d')
        with open(time_file,'w')as f:
            time_sign = self.encrypt('%s'%(current_date))
            data = {"last_use_time": current_date,
                    "deadline_use_time": self.deadline_use_time,
                    "GPU-ID": self.mac,
                    "time_sign":str(time_sign.decode('utf-8'))
                    }
            f.write(json.dumps(data))


class check_license_file():
    #检测license
    def license_check(self):
        current_date = datetime.datetime.now().strftime('%Y%m%d')
        time_dic = self.parse_time_file()
        last_use_time = time_dic['last_use_time'].strip()
        time_sign = self.decrypt(time_dic['time_sign']).strip()
        # 防止用户篡改最后登陆时间进行校验
        if last_use_time != time_sign:
            print('*Error*: Time file is modified')
            sys.exit(1)
        # 防止用户篡改系统时间进行校验
        if last_use_time > current_date:
            print('*Error*: The system time has been changed')
            sys.exit(1)

        license_dic = self.parse_license_file()
        sign = self.decrypt(license_dic['Sign'])
        sign_list = sign.split('#')
        mac = sign_list[0].strip()
        date = sign_list[1].strip()

        # 防止用户篡改过期时间进行校验
        if (mac != license_dic['MAC']) or (date != license_dic['Deadline_use_time']):
            print('*Error*: License file is modified!')
            sys.exit(1)
        # Check MAC and effective date invalid or not.
        if len(sign_list) == 2:
            mac = self.get_mac()
            if sign_list[0] != mac:
                print('*Error*: Invalid host!')
                sys.exit(1)
            # Current time must be before effective date.
            if sign_list[1] < current_date:
                print('*Error*: License is expired!')
                sys.exit(1)
        else:
            print('*Error*: Wrong Sign setting on license file.')
            sys.exit(1)
        self.save_time_file(current_date,date,mac)   #更新Json字符串
        print('Succeed')


    def encrypt(self, content):
        # content length must be a multiple of 16.
        while len(content) % 16:
            content += ' '
        content = content.encode('utf-8')
        # Encrypt content.
        aes = AES.new('1122334455667788'.encode(), AES.MODE_CBC, '1122334455667788'.encode())
        encrypted_content = aes.encrypt(content)
        return (b2a_hex(encrypted_content))

    def save_time_file(self,current_date,deadline_use_time,mac):
        #生成time文件
        time_file = 'time.txt'
        with open(time_file, 'w') as f:
            time_sign = self.encrypt('%s' % (current_date))
            data = {"last_use_time": current_date,
                    "deadline_use_time": deadline_use_time,
                    "GPU-ID": mac,
                    "time_sign": str(time_sign.decode('utf-8'))
                    }
            f.write(json.dumps(data))

    def parse_license_file(self):
        #打开文本解析数据
        license_dic = {}
        license_file = 'License.dat'
        with open(license_file, 'r') as LF:
            for line in LF.readlines():
                if re.match('^\s*(\S+)\s*:\s*(\S+)\s*$', line):
                    my_match = re.match('^\s*(\S+)\s*:\s*(\S+)\s*$', line)
                    license_dic[my_match.group(1)] = my_match.group(2)
        return (license_dic)

    def parse_time_file(self):
        time_file = 'time.txt'
        with open(time_file,'r')as f:
            time_sign = json.loads(f.read())
        return time_sign


    def decrypt(self, content):
        #AES解密
        aes = AES.new('1122334455667788'.encode(), AES.MODE_CBC, '1122334455667788'.encode())
        decrypted_content = aes.decrypt(a2b_hex(content.encode('utf-8')))
        return (decrypted_content.decode('utf-8'))

    def get_mac(self):
        '''
        return the MAC address of the computer
        '''
        mac = None
        if sys.platform == "win32":  #区分系统类别
            for line in os.popen("ipconfig /all"):  # 运行命令行
                if line.strip().startswith("Physical Address") or line.strip().startswith("物理地址"):# 默认获取第一个网卡mac地址
                    mac = line.split(":")[1].strip().replace("-", ":")
                    break
        else:#linux或者mac系统
            mac_address = uuid.UUID(int=uuid.getnode()).hex[-12:].upper()
            mac = ':'.join([mac_address[i:i + 2] for i in range(0, 11, 2)])
        print(mac)
        return mac





if __name__ == '__main__':
    mac = '00:FB:F1:B8:23:1A'              #主机mac地址
    deadline_use_time = '20220620'         #设置的过期时间
    pwd = '1122334455667788'               #16位字符秘钥

    get_license_file(mac, deadline_use_time, pwd).gen_license_file()   #生成license
    # check_license_file().get_mac()           #检测license

 

标签:file,license,python,self,content,mac,licence,time,授权
来源: https://www.cnblogs.com/lvye001/p/16380766.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有