You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

1 year ago
# 导入库
# pip install cryptography
# 参考文档https://blog.csdn.net/t_sheng/article/details/138134870
import time
import base64
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
aes_key = '1d35eefc2b8207d615028d056ce5296c'
aes_associatedData = "12345"
aes_nonce = "3229172322917278"
# 加密
def st_aes_en_func(de_data):
aesgcm = AESGCM(aes_key.encode())
ct = aesgcm.encrypt(aes_nonce.encode(), de_data.encode(), aes_associatedData.encode())
return base64.b64encode(ct).decode('utf-8')
# 解密
def st_aes_de_func(en_data):
en_data = base64.b64decode(en_data.encode('utf-8'))
aesgcm = AESGCM(aes_key.encode())
ct = aesgcm.decrypt(aes_nonce.encode(), en_data, aes_associatedData.encode())
return ct.decode()
def getenData(machine_id):
# 获取当前时间的时间戳(秒)
timestamp_in_seconds = time.time()
# 将时间戳转换为毫秒
timestamp_in_milliseconds = int(timestamp_in_seconds * 1000)
de_data = '{"timestamp":%s,"machine_id":"%s"}' % (timestamp_in_milliseconds, machine_id)
enData = st_aes_en_func(de_data)
return enData