siwei_config.py 874 B

12345678910111213141516171819202122232425262728293031
  1. # siwei_config.py
  2. import os
  3. import json
  4. from cryptography.fernet import Fernet
  5. # 加载密钥
  6. def load_key(key_path='secret.key'):
  7. current_dir = os.path.dirname(os.path.abspath(__file__))
  8. key_path = os.path.join(current_dir, key_path)
  9. with open(key_path, 'rb') as key_file:
  10. return key_file.read()
  11. # 解密并返回 JSON 数据(不写入磁盘)
  12. def decrypt_json_to_dict(encrypted_file_path, key_path='secret.key'):
  13. key = load_key(key_path)
  14. fernet = Fernet(key)
  15. with open(encrypted_file_path, 'rb') as f:
  16. encrypted_data = f.read()
  17. decrypted_data = fernet.decrypt(encrypted_data)
  18. return json.loads(decrypted_data.decode('utf-8'))
  19. CONFIG_PATH = r'C:\siwei_model\siwei_config.json.enc'
  20. # 解密并赋值给 CONFIG
  21. CONFIG = decrypt_json_to_dict(CONFIG_PATH)
  22. # 可导出为全局访问变量
  23. globals()['CONFIG'] = CONFIG