18 lines
483 B
Python
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
|
|
|