from Crypto.Util.number import inverse, long_to_bytes defadd(p1, p2): if p1 == (0, 0): return p2 if p2 == (0, 0): return p1 if p1[0] == p2[0] and (p1[1] != p2[1] or p1[1] == 0): return (0, 0) if p1[0] == p2[0]: tmp = (3 * p1[0] * p1[0]) * inverse(2 * p1[1], n) % n else: tmp = (p2[1] - p1[1]) * inverse(p2[0] - p1[0], n) % n x = (tmp * tmp - p1[0] - p2[0]) % n y = (tmp * (p1[0] - x) - p1[1]) % n return (int(x), int(y)) defmul(n, p): r = (0, 0) tmp = p while0 < n: if n & 1 == 1: r = add(r, tmp) n, tmp = n >> 1, add(tmp, tmp) return r n = 80263253261445006152401958351371889864136455346002795891511487600252909606767728751977033280031100015044527491214958035106007038983560835618126173948587479951247946411421106848023637323702085026892674032294882180449860010755423988302942811352582243198025232225481839705626921264432951916313817802968185697281 e = 67595664083683668964629173652731210158790440033379175857028564313854014366016864587830963691802591775486321717360190604997584315420339351524880699113147436604350832401671422613906522464334532396034178284918058690365507263856479304019153987101884697932619200538492228093521576834081916538860988787322736613809 c = (6785035174838834841914183175930647480879288136014127270387869708755060512201304812721289604897359441373759673837533885681257952731178067761309151636485456082277426056629351492198510336245951408977207910307892423796711701271285060489337800033465030600312615976587155922834617686938658973507383512257481837605, 38233052047321946362283579951524857528047793820071079629483638995357740390030253046483152584725740787856777849310333417930989050087087487329435299064039690255526263003473139694460808679743076963542716855777569123353687450350073011620347635639646034793626760244748027610309830233139635078417444771674354527028) # M = Matrix(ZZ, [[2**512, e], # [0, -n]]) # GV = M.LLL()[0] GV = (-352349739892292322999330613117511489276791137416050590322561272098135764509812116210543232353192946492907441641899508311181782397967869533800138447935440769558784357373166666205853969428654899524304252899588626355058283586558885888, -406850608655407486298019095013146348847805975120061760929682791882948049742096195978800022454159691659865169100330308708576847735609146508679126419372034710027124703842712262177437006326228856546452636094881051757653949488135598409) x = -GV[0] >> 512 y = (e*x+GV[1])//n k = e*x-n*y K = k//y deffactor(K, N): l = 0 r = K for i inrange(518): s = (l+r)//2 v = s*s - (9*s**2*(K-1-s)**2//(round(N**0.25)**2)) if v < 4*N: l = s else: r = s return r S = factor(K, n) d = inverse(e, n+1+S) print(b''.join(long_to_bytes(i) for i in mul(d, c)))
# 商业转载请联系作者获得授权,非商业转载请注明出处。 # For commercial use, please contact the author for authorization. For non-commercial use, please indicate the source. # 协议(License):署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) # 作者(Author):Wankko Ree # 链接(URL):https://www.wkr.moe/ctf/523.html # 来源(Source):Wankko Ree's Blog
from Crypto.Util.number import * import gmpy2 n = 123814470394550598363280518848914546938137731026777975885846733672494493975703069760053867471836249473290828799962586855892685902902050630018312939010564945676699712246249820341712155938398068732866646422826619477180434858148938235662092482058999079105450136181685141895955574548671667320167741641072330259009 e = 3 c = 19105765285510667553313898813498220212421177527647187802549913914263968945493144633390670605116251064550364704789358830072133349108808799075021540479815182657667763617178044110939458834654922540704196330451979349353031578518479199454480458137984734402248011464467312753683234543319955893 k = 0 while (gmpy2.iroot(c + k * n, e)[1] == False): k += 1 print(long_to_bytes(gmpy2.iroot(c + k *n,e)[0]))
解得k=0 ???鞭尸红明谷2021密码第一道RSA?
第二段共模攻击
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import gmpy2 from Crypto.Util.number import long_to_bytes,bytes_to_long
g, x, y = gmpy2.gcdext(e1, e2) #先通过扩展欧几里得算法求出x * e1 + y * e2 = 1中的x和y #print(x, y) m = pow(c1, x, n) * pow(c2, y, n) % n #计算(c1的x次方 * c2的y次方) mod n #print(m) print(long_to_bytes(m))
第三段coppersmith定理已知高位p
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#Sage from sage.allimport * n = 113432930155033263769270712825121761080813952100666693606866355917116416984149165507231925180593860836255402950358327422447359200689537217528547623691586008952619063846801829802637448874451228957635707553980210685985215887107300416969549087293746310593988908287181025770739538992559714587375763131132963783147L p4 = 7117286695925472918001071846973900342640107770214858928188419765628151478620236042882657992902 e = 65537 pbits = 512 kbits = pbits - p4.nbits() print(p4.nbits()) p4 = p4 << kbits PR.<x> = PolynomialRing(Zmod(n)) f = x + p4 roots = f.small_roots(X=2^kbits, beta=0.4) #经过以上一些函数处理后,n和p已经被转化为10进制 if roots: p = p4+int(roots[0]) print("n: "+str(n)) print("p: "+str(p)) print("q: "+str(n//p))