test.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from cryptography.fernet import Fernet
  2. import json
  3. import datetime
  4. import socket
  5. machine_name = socket.gethostname()
  6. current_time = datetime.datetime.now()
  7. # 示例使用
  8. params = {
  9. "host": "DESKTOP-5Q00G3P",
  10. "license": "2024-11-30"
  11. }
  12. # 生成一个密钥
  13. key = Fernet.generate_key()
  14. print(key)
  15. #
  16. # # 使用密钥创建一个Fernet对象
  17. cipher_suite = Fernet(key)
  18. #
  19. # # 需要加密的数据
  20. message = str(params).encode("utf-8")
  21. #
  22. # # 加密数据
  23. encrypted_message = cipher_suite.encrypt(message)
  24. print(f"加密的消息: {encrypted_message}")
  25. # #
  26. # # # 解密数据
  27. # decrypted_message = cipher_suite.decrypt(encrypted_message)
  28. # print(f"解密的消息: {decrypted_message.decode('utf-8')}")
  29. # licensepath = "D:\\Program Files\\QGIS 3.34.9\\bin\\license.txt"
  30. # # 打开文件并读取内容
  31. # text = []
  32. # with open(licensepath, 'r') as file:
  33. # for line in file:
  34. # text.append(line.replace('\n', ' '))
  35. # # 打印文件内容
  36. # print(text)
  37. #
  38. #
  39. # def str_to_time(time_str):
  40. # return datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
  41. #
  42. #
  43. # def str_to_dict(string):
  44. # # 去除首尾的花括号
  45. # string = string.strip("{}")
  46. # # 分割每个键值对
  47. # pairs = string.split(", ")
  48. # # 创建空字典
  49. # result = {}
  50. # # 遍历键值对并添加到字典中
  51. # for pair in pairs:
  52. # key, value = pair.split(": ")
  53. # # 去除键和值两边的引号
  54. # key = key.strip("'")
  55. # value = value.strip("'")
  56. # # 添加到字典中
  57. # result[key] = value
  58. # return result
  59. #
  60. #
  61. # fernet = Fernet(text[0].encode("utf-8"))
  62. # decrypted_message = fernet.decrypt(text[1].encode("utf-8"))
  63. # license = decrypted_message.decode('utf-8')
  64. # licenseDict = str_to_dict(license)
  65. # host = licenseDict["host"]
  66. # date = licenseDict["license"]
  67. #
  68. # time_str = f"{date} 23:59:59"
  69. # licensetime = str_to_time(time_str)
  70. # if host != machine_name:
  71. # print("机器名错误!")
  72. # exit(1)
  73. # elif current_time > licensetime:
  74. # print("许可已过期!")
  75. # else:
  76. # print("许可正常")
  77. # print(f"解密的消息: {decrypted_message.decode('utf-8')}")