Håstad 广播攻击

Håstad 广播攻击

1.定义

Håstad 广播攻击是一种针对RSA小公钥指数的攻击方法,通过收集相同消息的多个密文并利用中国剩余定理,可以在不用分解模数或获取私钥的情况下恢复原文。

2.攻击原理

Håstad 广播攻击针对的场景:同一消息m被使用相同小公钥指数e加密,发送给多个接收者,每个接受者个模数\(N_i\)互素。攻击者窃听到所有密文\(c_i\)和对应的公钥\(N_i\)后,可以通过CRT恢复原文。

题目 Endless Emails-CryptoHack

题目:

#!/usr/bin/env python3

from Crypto.Util.number import bytes_to_long, getPrime
from secret import messages


def RSA_encrypt(message):
    m = bytes_to_long(message)
    p = getPrime(1024)
    q = getPrime(1024)
    N = p * q
    e = 3
    c = pow(m, e, N)
    return N, e, c


for m in messages:
    N, e, c = RSA_encrypt(m)
    print(f"n = {N}")
    print(f"e = {e}")
    print(f"c = {c}")

对不同的明文进行加密,因此需要找出对同一组明文加密的密文。先匹配然后使用CRT进行恢复。

EXP

import itertools
import re
from math import gcd
from pathlib import Path

from sympy import integer_nthroot
from sympy.ntheory.modular import crt


DATA_FILE = Path(r"D:\Blog\output.txt")

text = DATA_FILE.read_text(encoding="utf-8-sig")

pattern = re.compile(
    r"(?m)^n[ \t]*=[ \t]*(\d+)[ \t]*\r?\n"
    r"^e[ \t]*=[ \t]*(\d+)[ \t]*\r?\n"
    r"^c[ \t]*=[ \t]*(\d+)"
)

records = [
    (int(n), int(e), int(c))
    for n, e, c in pattern.findall(text)
]

if not records:
    raise SystemExit(f"没有在 {DATA_FILE} 中找到 n/e/c 数据")

print(f"读取到 {len(records)} 组 RSA 数据")

found = False

# e = 3,因此尝试每组三条记录
for indices in itertools.combinations(range(len(records)), 3):
    selected = [records[i] for i in indices]
    moduli = [n for n, _, _ in selected]
    exponents = [e for _, e, _ in selected]
    ciphertexts = [c for _, _, c in selected]

    if exponents != [3, 3, 3]:
        continue

    # CRT 广播攻击要求这些模数两两互素
    if any(
        gcd(moduli[i], moduli[j]) != 1
        for i in range(3)
        for j in range(i + 1, 3)
    ):
        continue

    result, modulus_product = crt(moduli, ciphertexts)
    if result is None:
        continue

    result = int(result)
    modulus_product = int(modulus_product)

    # 检查 CRT 结果是不是一个精确的整数立方
    root, exact = integer_nthroot(result, 3)
    if not exact:
        continue

    m = int(root)

    # 再用原始 RSA 公式核对三条密文
    if not all(
        pow(m, e, n) == c
        for n, e, c in selected
    ):
        continue

    raw = m.to_bytes(max(1, (m.bit_length() + 7) // 8), "big")
    flags = re.findall(rb"crypto\{[^}]+\}", raw)

    print("匹配的记录(从 1 开始编号):", [i + 1 for i in indices])
    print("解出的明文:")
    print(raw.decode("utf-8", errors="replace"))

    if flags:
        print("Flag:", flags[0].decode())
    else:
        print("未找到 crypto{...} 格式的 flag")

    found = True
    break

if not found:
    print("没有找到符合条件的三组记录")
posted @ 2026-10-16 13:05  laiyi一一  阅读(12)  评论(0)    收藏  举报