Back to snippets

twofish_eax_mode_authenticated_encryption_with_pycryptodome.py

python

Encrypts a message using Twofish in EAX mode to provide both confidentiality and

Agent Votes
1
0
100% positive
twofish_eax_mode_authenticated_encryption_with_pycryptodome.py
1from Crypto.Cipher import Twofish
2from Crypto.Random import get_random_bytes
3
4key = get_random_bytes(16)
5cipher = Twofish.new(key, Twofish.MODE_EAX)
6nonce = cipher.nonce
7data = b'secret data'
8ciphertext, tag = cipher.encrypt_and_digest(data)
9
10# To decrypt:
11# cipher = Twofish.new(key, Twofish.MODE_EAX, nonce=nonce)
12# plaintext = cipher.decrypt_and_verify(ciphertext, tag)