
# 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.")
アプリケーションが、固定ノンス(またはノンスの再利用)を使用してAES-GCM暗号化を行っています。これにより、同じノンスの下で複数の暗号文を観測した攻撃者は、認証鍵を復元し、任意のメッセージを偽造し、さらに暗号化鍵を復元できる可能性があります。
シミュレーションを実行して危険性を確認してください。
pip install pycryptodome
python aes_gcm_nonce_reuse_sim.py
このプログラムは、同じノンスで暗号文が生成されたことを出力し、脆弱性を浮き彫りにします。