Files
nosys_libs/fspn/utils/aes_util.py
2026-01-25 13:55:46 +10:00

18 lines
483 B
Python

from Crypto.Cipher import AES
def encrypt(data:bytes, key:bytes):
cipher = AES.new(key, AES.MODE_EAX)
nonce = cipher.nonce
ciphertext, mac = cipher.encrypt_and_digest(data)
return nonce, ciphertext, mac
def decrypt_and_verify(nonce:bytes, data:bytes, mac:bytes, key:bytes):
cipher = AES.new(key, AES.MODE_EAX, nonce=nonce)
plaintext = cipher.decrypt(data)
try:
cipher.verify(mac)
except ValueError:
return None
return plaintext