Rsa共模攻击

runwu2204 Lv6

共模攻击主要利用的是,同一个明文通过两个互质的e对同一个n取余。(如下)

1
2
c1 = pow(m,e1,n)
c2 = pow(m,e2,n)

数学上存在一个关系

1
e1*x+e2*y=gcd(e1,e2) #定有x y满足这个式子

同时又有(前提 m<n 否则会在取余时丢失数据)

1
2
3
4
(m^e1)^x * (m^e2)^y %n = m^(e1*x+e2*y) %n
(c1^x) * (c2^y) %n = m^gcd(e1,e2) %n
#如果两边都开gcd(e1,e2)次方则
((c1^x) * (c2^y))^(1/gcd(e1,e2))%n=m %n

exp:

1
2
3
4
5
6
7
def gongmogongji(c1, c2, e1, e2,n):
from Crypto.Util.number import long_to_bytes
from gmpy2 import gcdext,iroot
gcd,x,y=gcdext(e1,e2)#求出e1*x+e2*y=gcd(e1,e2)的解
m=iroot((pow(c1,x,n)*pow(c2,y,n))%n,gcd)[0]#根据((c1^x) * (c2^y))^(1/gcd(e1,e2))%n=m %n 计算出对应的m
return long_to_bytes(m)#将m转换回bytes数组

m>n时

1
2
3
4
5
6
7
8
9
10
def gongmogongji(c1, c2, e1, e2,n):
from Crypto.Util.number import long_to_bytes
from gmpy2 import gcdext,iroot
gcd,x,y=gcdext(e1,e2)#求出e1*x+e2*y=gcd(e1,e2)的解
tmp=0
m=int(iroot((pow(c1,x,n)*pow(c2,y,n))%n+tmp*n,gcd)[0])#根据((c1^x) * (c2^y))^(1/gcd(e1,e2))%n=m %n 计算出对应的m
while b'DASCTF' not in long_to_bytes(m):
tmp+=1
m=int(iroot((pow(c1,x,n)*pow(c2,y,n))%n+tmp*n,gcd)[0])#根据((c1^x) * (c2^y))^(1/gcd(e1,e2))%n=m %n 计算出对应的m
return long_to_bytes(m)#将m转换回bytes数组
  • 标题: Rsa共模攻击
  • 作者: runwu2204
  • 创建于 : 2023-07-09 23:16:37
  • 更新于 : 2023-09-29 01:19:20
  • 链接: https://runwu2204.github.io/2023/07/09/Crypto/RSA/Rsa共模攻击/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
Rsa共模攻击