>lhzctf

EZRSA

1.打开文件

m1 = bytes_to_long(flag[:19])
m2 = bytes_to_long(flag[19:])

p1 = getPrime(512)
q1 = next_prime(p1)
n1 = p1 * q1
c1 = pow(m1, e, n1)

p2 = (3 * v**2 + 1) // 4
q2 = getPrime(512)
n2 = p2 * q2
c2 = pow(m2, e, n2)

给了两个RSA实例

2.解题

n1的两个因子十分接近 直接Fermat分解

n2使用CMD=3分解

完整脚本

from math import isqrt, gcd
from random import Random

E = 65537

n1 = 48574622066244676965235410194951656048391858093851188940475070135515179274646260335508645731054941127017455694181589850631448005933690576236509109357906158501005100106426404677648467234352820220149908495893097527494714498231744650953221603274064633531511457004086865188334487164218254500458362660378142943583
c1 = 44352287639488462165424617738514479773688827969802680585178887351317394106539285567053964316546811241789408708004715640089209417889736801591247634202760972532838901086026883693741170006246975299280655319538692988898452930026925094172672932284084394350095400055925861477391946069692240145079202691870481219289

n2 = 98941261841403418592262339751911969375524549896756276640140105958959901510901730725832501964388830266111803793483930043132434255822037239946049186259542374973686881689165179046939235048017397884327825953336466635430354727609091604954283688514828832761379269654169789137858888365761554433253040096188200568281
c2 = 76163449092355468546113624354066065692121574631154372575743562609890072976310941000089625243267080172093680198190555477193899352858436212815181402948223806018139064841880151043619206074951833038284855323529675836457253286593953615350962135867772323983922522831905394556954697834599614525687418750984269968551


def fermat_factor(n, max_iter=1_000_000):
    """Factor n = p*q when p and q are close."""
    a = isqrt(n)
    if a * a < n:
        a += 1
    for _ in range(max_iter):
        b2 = a * a - n
        b = isqrt(b2)
        if b * b == b2:
            return a - b, a + b
        a += 1
    raise RuntimeError("Fermat failed")


def cm_d3_factor(n, seed=12345, attempts=500):
    """
    Cheng's 4p-1 factorization method for D=3.

    If p | n and 4p - 1 = 3*v^2, then the curve y^2 = x^3 + b has a
    sextic twist with #E(F_p) = p.  For a random b and x, work in

        S = Z_n[Y] / (Y^2 - (x^3 + b))

    with P = (x, Y).  For the correct twist, [n]P = O mod p, hence the
    Jacobian Z-coordinate vanishes mod p.  Taking the norm of Z gives a
    multiple of p, and gcd(norm(Z), n) reveals p.
    """
    rng = Random(seed)

    for attempt in range(attempts):
        b = rng.randrange(1, n)
        x0 = rng.randrange(0, n)
        f = (pow(x0, 3, n) + b) % n

        g = gcd(f, n)
        if 1 < g < n:
            return g, n // g, ("gcd(f,n)", attempt, b, x0)

        def add(u, v):
            return ((u[0] + v[0]) % n, (u[1] + v[1]) % n)

        def sub(u, v):
            return ((u[0] - v[0]) % n, (u[1] - v[1]) % n)

        def mul(u, v):
            a0, a1 = u
            b0, b1 = v
            return ((a0 * b0 + a1 * b1 * f) % n,
                    (a0 * b1 + a1 * b0) % n)

        def smul(k, u):
            return ((k * u[0]) % n, (k * u[1]) % n)

        ZERO = (0, 0)
        ONE = (1, 0)
        Y = (0, 1)

        def dbl(P):
            X, Yc, Z = P
            XX = mul(X, X)
            YY = mul(Yc, Yc)
            YYYY = mul(YY, YY)
            S = mul(smul(4, X), YY)
            M = smul(3, XX)
            X3 = sub(mul(M, M), smul(2, S))
            Y3 = sub(mul(M, sub(S, X3)), smul(8, YYYY))
            Z3 = mul(smul(2, Yc), Z)
            return X3, Y3, Z3

        def mixed_add(P, Q):
            X1, Y1, Z1 = P
            x2, y2 = Q
            x2 = (x2, 0)
            Z1Z1 = mul(Z1, Z1)
            U2 = mul(x2, Z1Z1)
            S2 = mul(y2, mul(Z1, Z1Z1))
            H = sub(U2, X1)
            HH = mul(H, H)
            I = smul(4, HH)
            J = mul(H, I)
            r = smul(2, sub(S2, Y1))
            V = mul(X1, I)
            X3 = sub(sub(mul(r, r), J), smul(2, V))
            Y3 = sub(mul(r, sub(V, X3)), smul(2, mul(Y1, J)))
            T = add(Z1, H)
            Z3 = sub(sub(mul(T, T), Z1Z1), HH)
            return X3, Y3, Z3

        P = ((x0, 0), Y, ONE)
        Q = P
        for bit in bin(n)[3:]:
            Q = dbl(Q)
            if bit == "1":
                Q = mixed_add(Q, (x0, Y))

        X3, Y3, Z3 = Q
        norm_z = (Z3[0] * Z3[0] - Z3[1] * Z3[1] * f) % n
        g = gcd(norm_z, n)
        if 1 < g < n:
            return g, n // g, ("norm(Z)", attempt, b, x0)

    raise RuntimeError("CM D=3 factorization failed")


def decrypt(n, c, p, q):
    d = pow(E, -1, (p - 1) * (q - 1))
    m = pow(c, d, n)
    return m.to_bytes((m.bit_length() + 7) // 8, "big")


def is_d3_prime(p):
    t = 4 * p - 1
    if t % 3:
        return False
    s = t // 3
    r = isqrt(s)
    return r * r == s


if __name__ == "__main__":
    p1, q1 = fermat_factor(n1)
    print("p1 =", p1)
    print("q1 =", q1)
    m1 = decrypt(n1, c1, p1, q1)
    print("m1 =", m1)

    p, q, info = cm_d3_factor(n2)
    print("CM info =", info)
    p2, q2 = (p, q) if is_d3_prime(p) else (q, p)
    print("p2 =", p2)
    print("q2 =", q2)
    m2 = decrypt(n2, c2, p2, q2)
    print("m2 =", m2)

    print("flag =", (m1 + m2).decode())

输出flag

MISC