| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- from cryptography.fernet import Fernet
- import json
- import datetime
- import socket
- machine_name = socket.gethostname()
- current_time = datetime.datetime.now()
- # 示例使用
- params = {
- "host": "DESKTOP-5Q00G3P",
- "license": "2024-11-30"
- }
- # 生成一个密钥
- key = Fernet.generate_key()
- print(key)
- #
- # # 使用密钥创建一个Fernet对象
- cipher_suite = Fernet(key)
- #
- # # 需要加密的数据
- message = str(params).encode("utf-8")
- #
- # # 加密数据
- encrypted_message = cipher_suite.encrypt(message)
- print(f"加密的消息: {encrypted_message}")
- # #
- # # # 解密数据
- # decrypted_message = cipher_suite.decrypt(encrypted_message)
- # print(f"解密的消息: {decrypted_message.decode('utf-8')}")
- # licensepath = "D:\\Program Files\\QGIS 3.34.9\\bin\\license.txt"
- # # 打开文件并读取内容
- # text = []
- # with open(licensepath, 'r') as file:
- # for line in file:
- # text.append(line.replace('\n', ' '))
- # # 打印文件内容
- # print(text)
- #
- #
- # def str_to_time(time_str):
- # return datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
- #
- #
- # def str_to_dict(string):
- # # 去除首尾的花括号
- # string = string.strip("{}")
- # # 分割每个键值对
- # pairs = string.split(", ")
- # # 创建空字典
- # result = {}
- # # 遍历键值对并添加到字典中
- # for pair in pairs:
- # key, value = pair.split(": ")
- # # 去除键和值两边的引号
- # key = key.strip("'")
- # value = value.strip("'")
- # # 添加到字典中
- # result[key] = value
- # return result
- #
- #
- # fernet = Fernet(text[0].encode("utf-8"))
- # decrypted_message = fernet.decrypt(text[1].encode("utf-8"))
- # license = decrypted_message.decode('utf-8')
- # licenseDict = str_to_dict(license)
- # host = licenseDict["host"]
- # date = licenseDict["license"]
- #
- # time_str = f"{date} 23:59:59"
- # licensetime = str_to_time(time_str)
- # if host != machine_name:
- # print("机器名错误!")
- # exit(1)
- # elif current_time > licensetime:
- # print("许可已过期!")
- # else:
- # print("许可正常")
- # print(f"解密的消息: {decrypted_message.decode('utf-8')}")
|