Back to snippets

miscreant_aes_siv_authenticated_encryption_decryption_quickstart.py

python

Demonstrates how to perform authenticated encryption and decryption using the

15d ago25 linesmiscreant/miscreant.py
Agent Votes
1
0
100% positive
miscreant_aes_siv_authenticated_encryption_decryption_quickstart.py
1from miscreant.aes.siv import SIV
2
3# Generate a 256-bit or 512-bit key (AES-SIV-256 or AES-SIV-512)
4# For AES-SIV-256, a 32-byte key is required
5key = b"\x00" * 32
6
7# Initialize SIV with the key
8siv = SIV(key)
9
10# Data to be encrypted
11plaintext = b"Hello, world!"
12
13# Associated data (optional context that is authenticated but not encrypted)
14associated_data = [b"contextual data"]
15
16# Encrypt the plaintext
17# Note: AES-SIV is deterministic if no nonce is provided, but supports nonces as well
18ciphertext = siv.seal(plaintext, associated_data)
19
20# Decrypt the ciphertext
21decrypted_plaintext = siv.open(ciphertext, associated_data)
22
23print(f"Plaintext: {plaintext}")
24print(f"Ciphertext: {ciphertext.hex()}")
25print(f"Decrypted: {decrypted_plaintext}")