Crypto_BUUCTF_WriteUp | rsarsa
题目
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。根据公式
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}
参考
本文作者:Guanz
本文链接:https://www.cnblogs.com/Guanz/p/17825075.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步