
# aes_gcm_nonce_reuse_sim.py - Simulated encryption oracle with fixed nonce
from Crypto.Cipher import AES
import os
key = os.urandom(16)
nonce = b'\x00' * 12 # ALWAYS SAME NONCE
def encrypt(plaintext):
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
ct, tag = cipher.encrypt_and_digest(plaintext)
return ct, tag
# Vulnerable: attacker can obtain many ciphertexts with same nonce
ct1, tag1 = encrypt(b"Secret message 1")
ct2, tag2 = encrypt(b"Secret message 2")
print("Ciphertexts collected. Nonce reuse allows key recovery and forgery.")
An application uses AES‑GCM encryption with a fixed nonce (or repeats a nonce). This allows an attacker who observes multiple ciphertexts under the same nonce to recover the authentication key, forge arbitrary messages, and potentially recover the encryption key.
Run the simulation to see the danger:
pip install pycryptodome
python aes_gcm_nonce_reuse_sim.py
The program outputs that ciphertexts with the same nonce were generated, highlighting the vulnerability.