例题

p = 8637633767257008567099653486541091171320491509433615447539162437911244175885667806398411790524083553445158113502227745206205327690939504032994699902053229 
q = 12640674973996472769176047937170883420927050821480010581593137135372473880595613737337630629752577346147039284030082593490776630572584959954205336880228469 
dp = 6500795702216834621109042351193261530650043841056252930930949663358625016881832840728066026150264693076109354874099841380454881716097778307268116910582929 
dq = 783472263673553449019532580386470672380574033551303889137911760438881683674556098098256795673512201963002175438762767516968043599582527539160811120550041 
c = 24722305403887382073567316467649080662631552905960229399079107995602154418176056335800638887527614164073530437657085079676157350205351945222989351316076486573599576041978339872265925062764318536089007310270278526159678937431903862892400747915525118983959970607934142974736675784325993445942031372107342103852

攻击条件

已知dp,dq,p,q,c

其中:

dp=d%(p-1)

dq=d%(q-1)

原理

正常来说,解密用到是这个公式: m ≡ c d ( m o d n ) m \equiv c^{d} \pmod{n} mcd(modn)。但是由于 n = p q n=pq n=pq,所以刚刚那个式子可以化为 { m 1 ≡ c d ( m o d p ) ( 1 ) m 2 ≡ c d ( m o d q ) ( 2 ) \left\{\begin{matrix}m_{1} \equiv c^{d} \pmod{p} & (1) \\ m_{2} \equiv c^{d} \pmod{q} & (2) \end{matrix}\right. {m1cd(modp)m2cd(modq)(1)(2)。证明过程如下:

对于公式 m ≡ c d ( m o d n ) m \equiv c^{d} \pmod{n} mcd(modn)我们可以这样理解: m m m除以 n n n得到某个商 k k k和余数 c d c^{d} cd
于是可以转化成等式: m = c d + k n m=c^{d} +kn m=cd+kn。而 n = p ∗ q n=p*q n=pq,所以可以进一步写成 m = c d + k n = c d + k p q m=c^{d} +kn=c^{d} +kpq m=cd+kn=cd+kpq,其中 k k k为假设的商。
然后两边分别同时对 p p p, q q q取余, k p q kpq kpq那一项就被消去了,就得到 { m 1 ≡ c d ( m o d p ) m 2 ≡ c d ( m o d q ) \left\{\begin{matrix}m_{1} \equiv c^{d} \pmod{p} \\ m_{2} \equiv c^{d} \pmod{q} \end{matrix}\right. {m1cd(modp)m2cd(modq)

由于 d p = d ( m o d p − 1 ) dp= d \pmod {p-1} dp=d(modp1), d q = d ( m o d q − 1 ) dq= d \pmod {q-1} dq=d(modq1),再结合欧拉定理(或费马小定理),上面的同余式组可以进一步化为 { m 1 ≡ c d p ( m o d p ) ( 3 ) m 2 ≡ c d q ( m o d q ) ( 4 ) \left\{\begin{matrix}m_{1} \equiv c^{dp} \pmod{p} & (3) \\ m_{2} \equiv c^{dq} \pmod{q} & (4) \end{matrix}\right. {m1cdp(modp)m2cdq(modq)(3)(4).

由式(1)可得 m 1 + k p = c d m_{1}+kp=c^{d} m1+kp=cd(这里的 k k k和上面证明中的 k k k没有关系),不妨将其记为(5)式,然后代入(2)式可得 k p ≡ ( m 2 − m 1 ) ( m o d q ) kp \equiv (m_{2}-m_{1}) \pmod{q} kp(m2m1)(modq)

又因为 g c d ( p , q ) = 1 gcd(p,q)=1 gcd(p,q)=1,即 p p p, q q q互素,所以可以在刚刚得到式子两边同时乘上 p p p的逆元,得到 k ≡ p ′ ( m 2 − m 1 ) ( m o d q ) k \equiv p'(m_{2}-m_{1}) \pmod{q} kp(m2m1)(modq),其中 p ′ p' p p p p关于 q q q的逆元。

将这个 k k k的表达式代入(5)式,得到 c d = m 1 + [ p ′ ( m 2 − m 1 ) ( m o d q ) ] ∗ p c^{d}=m_{1} +\left [ p'(m_{2}-m_{1}) \pmod {q}\right ]*p cd=m1+[p(m2m1)(modq)]p。其中的 m 1 m_{1} m1 m 2 m_{2} m2可以通过上面的式(3)(4)计算得到。最后就可以得到 c d c^{d} cd的值从而解出明文 m m m

脚本

from gmpy2 import *
from Crypto.Util.number import *

# dp+dq+p+q+c => m

def rsa(dp,dq,p,q,c):
    m1=pow(c,dp,p)
    m2=pow(c,dq,q)
    p_q=invert(p,q)
    m=m1+p_q*((m2-m1)%q)*p
    print(long_to_bytes(m))



if __name__ == "__main__":
    p = 8637633767257008567099653486541091171320491509433615447539162437911244175885667806398411790524083553445158113502227745206205327690939504032994699902053229 
    q = 12640674973996472769176047937170883420927050821480010581593137135372473880595613737337630629752577346147039284030082593490776630572584959954205336880228469 
    dp = 6500795702216834621109042351193261530650043841056252930930949663358625016881832840728066026150264693076109354874099841380454881716097778307268116910582929 
    dq = 783472263673553449019532580386470672380574033551303889137911760438881683674556098098256795673512201963002175438762767516968043599582527539160811120550041 
    c = 24722305403887382073567316467649080662631552905960229399079107995602154418176056335800638887527614164073530437657085079676157350205351945222989351316076486573599576041978339872265925062764318536089007310270278526159678937431903862892400747915525118983959970607934142974736675784325993445942031372107342103852

    rsa(dp,dq,p,q,c)

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐