BUUCTF_Crypto_WriteUp | rsarsa

发布时间 2023-11-10 21:14:03作者: Guanz

题目

Math is cool! Use the RSA algorithm to decode the secret message, c, p, q, and e are parameters for the RSA algorithm.

p = 9648423029010515676590551740010426534945737639235739800643989352039852507298491399561035009163427050370107570733633350911691280297777160200625281665378483
q = 11874843837980297032092405848653656852760910154543380907650040190704283358909208578251063047732443992230647903887510065547947313543299303261986053486569407
e = 65537
c = 83208298995174604174773590298203639360540024871256126892889661345742403314929861939100492666605647316646576486526217457006376842280869728581726746401583705899941768214138742259689334840735633553053887641847651173776251820293087212885670180367406807406765923638973161375817392737747832762751690104423869019034

Use RSA to find the secret message

分析

根据题目,我们的目标是计算消息明文 m。根据公式 \(m = c^{d}\mod n\),我们需要:
Step 1. 由 p,q,e 求解 d
Step 2. 由 c,d,p,q 求解 m

Step 1
根据 RSA 算法描述,通过扩展欧几里得算法计算 d

def ExEuclid(a, b):
    # k
    s0, s1, s2 = 1, 0, -1
    # d
    t0, t1, t2 = 0, 1, -1

    if a < b:
        temp = a
        a = b
        b = temp
    while b != 1:
        rem = a % b
        quo = a // b  # // 整数除法获得商

        s2, t2 = s0 - s1 * quo, t0 - t1 * quo
        s0, t0 = s1, t1
        s1, t1 = s2, t2

        a = b
        b = rem

    return t2

Step 2
这里我们使用 python 的 pow 函数直接计算结果

def rsarsa(p, q, e, c):
    # n= pq
    n = p * q
    # φ(n) = (p-1)(q-1)
    varphi_n = (p - 1) * (q - 1)

    # ed ≡ 1 mod φ(n) → ed + k*φ(n) = 1
    # Extend Euclidean Algorithm
    d = ExEuclid(varphi_n, e)
    print(d)

    m = pow(c, d, n)
    return m

完整代码

点击查看代码
p = 9648423029010515676590551740010426534945737639235739800643989352039852507298491399561035009163427050370107570733633350911691280297777160200625281665378483
q = 11874843837980297032092405848653656852760910154543380907650040190704283358909208578251063047732443992230647903887510065547947313543299303261986053486569407
e = 65537
c = 83208298995174604174773590298203639360540024871256126892889661345742403314929861939100492666605647316646576486526217457006376842280869728581726746401583705899941768214138742259689334840735633553053887641847651173776251820293087212885670180367406807406765923638973161375817392737747832762751690104423869019034

def ExEuclid(a, b):
    # k
    s0, s1, s2 = 1, 0, -1
    # d
    t0, t1, t2 = 0, 1, -1

    if a < b:
        temp = a
        a = b
        b = temp
    while b != 1:
        rem = a % b
        quo = a // b  # // 整数除法获得商

        s2, t2 = s0 - s1 * quo, t0 - t1 * quo
        s0, t0 = s1, t1
        s1, t1 = s2, t2

        a = b
        b = rem

    return t2

def rsarsa(p, q, e, c):
    # n= pq
    n = p * q
    # φ(n) = (p-1)(q-1)
    varphi_n = (p - 1) * (q - 1)

    # ed ≡ 1 mod φ(n) → ed + k*φ(n) = 1
    # Extend Euclidean Algorithm
    d = ExEuclid(varphi_n, e)
    print(d)

    m = pow(c, d, n)
    return m

flag = rsarsa(p, q, e, c)
print(flag)

Flag

flag{5577446633554466577768879988}

参考

RSA 算法-百度百科
扩展欧几里得算法-bilibili
python pow() 函数-菜鸟教程