# 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.")
应用程序使用固定的 nonce(或重复使用 nonce)进行 AES‑GCM 加密。这会允许攻击者在同一 nonce 下观察多个密文,从而恢复认证密钥、伪造任意消息,并可能恢复加密密钥。
运行模拟程序即可看到风险:
pip install pycryptodome
python aes_gcm_nonce_reuse_sim.py
该程序输出表明已使用相同 nonce 生成密文,从而凸显该漏洞。