from Cryptodome.Cipher import AES from Cryptodome.Random import get_random_bytes data = b'Walnuts are harvested with a green outer hull, which will eventually dry out and become brittle. After it falls off, exposing the shell, the nut will look more like something that came from a store. But I think in other areas they might slowly rot and leach bad flavors into the nut, given some of the instructions I have seen for hull removal. Around here, that does not seem to be an issue. Perhaps this area is on the dry side for walnuts.' key = get_random_bytes(16) cipher = AES.new(key, AES.MODE_EAX) ciphertext, tag = cipher.encrypt_and_digest(data) file_out = open("encrypted.bin", "wb") [ file_out.write(x) for x in (cipher.nonce, tag, ciphertext) ] file_out.close() file_in = open("encrypted.bin", "rb") nonce, tag, ciphertext = [ file_in.read(x) for x in (16, 16, -1) ] file_in.close() # let's assume that the key is somehow available again cipher = AES.new(key, AES.MODE_EAX, nonce) data = cipher.decrypt_and_verify(ciphertext, tag) print(data)