p+q部分泄露

RSA有这样一道题 p+q高位泄露

import gmpy2
from Crypto.Util.number import *

p=getPrime(512)
q=getPrime(512)
print(p)
print(q)
print((p*q)>>233)

个人思路,不一定对

p+q = A0+x

p2 -A0 p +n -xp =0 ---①

去掉x一项有p2 -A0 p +n=0

那么此时p一定不是我们所求的p

有(p-t)2 -A0 (p-t) +n=0 ---②

结合①有 t2-2pt+A0+xp=0

然后测一下t的大小

import gmpy2
from Crypto.Util.number import *

p=getPrime(512)
q=getPrime(512)
print(p)
print(q)
print(p*q)
A0=((p+q)>>233)<<233
x=(p+q)-A0
print(len(bin(p-gmpy2.iroot(p**2-A0-x*p,2)[0])))

可以发现大约234位

那么取得p-t后则可以coppersmith求出p

注意解②时需要在高精度的实数域下 因为不一定是整数解

下面给出sage脚本

#Run in Sagemath
import gmpy2
import math
from Crypto.Util.number import *
n=107847645025513535850791225440130707186374273414737521825714442885442542984529827092766916748308266623066053421754530398675086374798530909982183476265477598220972320436343536618913608933549054793948336438240303192183575843300257235195070331758555882832698815894168120103459360566404203412796086643598204242029
hint=1505246122706281178983443268987790509406564031452059494152554645294649335244676839890
c=16589212605025468264862689939961340646400626438824064459026078002373801735937861932598660491280834517490394340315480853421268981930217857435669572426110105938015092688880859031418213297139875807487434491329423279700179082306961439610731922669459168752958192842026918421354512654494583087220572962719510130677


hint=hint<<233
e = 0x10001
PR.<x> = PolynomialRing(RealField(1000))
f = x*(hint-x) - n
phigh = int(f.roots()[0][0])
PR.<x> = PolynomialRing(Zmod(n))
f = phigh + x
res = f.small_roots(X=2**238, beta=0.4,epsilon=0.01)[0]
p=int(res+phigh)
q=n//p
d=gmpy2.invert(e,(p-1)*(q-1))
m=int(pow(c,d,n))
print(long_to_bytes(m))
posted @ 2024-08-19 12:06  m0feng  阅读(50)  评论(0编辑  收藏  举报