from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
class SecureStorage:
def __init__(self, key):
self.key = key
def encrypt_data(self, data):
cipher = AES.new(self.key, AES.MODE_EAX)
nonce = cipher.nonce
ciphertext, tag = cipher.encrypt_and_digest(data)
return nonce, ciphertext, tag
def decrypt_data(self, nonce, ciphertext, tag):
cipher = AES.new(self.key, AES.MODE_EAX, nonce=nonce)
data = cipher.decrypt_and_verify(ciphertext, tag)
return data
# Example usage:
key = get_random_bytes(32) # AES-256 requires a 32-byte key
storage = SecureStorage(key)
# Encrypt data
data = b'Secret data'
nonce, ciphertext, tag = storage.encrypt_data(data)
print(f"Encrypted data: {ciphertext}")
# Decrypt data
decrypted_data = storage.decrypt_data(nonce, ciphertext, tag)
print(f"Decrypted data: {decrypted_data}")