M = (N - 1) // (2 * g) u = M // (2 * g) v = M - 2 * g * u GF = Zmod(N) x = GF.random_element() y = x ^ (2 * g) # c的范围大概与N^(0.5-2*gamma)很接近 c = bsgs(y, y ^ u, ((2**(cbits-1)), (2**(cbits+1)))) ab = u - c apb = v + 2 * g * c P.<x> = ZZ[] f = x ^ 2 - apb * x + ab a = f.roots() if a: a, b = a[0][0], a[1][0] p = 2 * g * a + 1 q = 2 * g * b + 1 assert p * q == N
d = inverse(e,(p-1)*(q-1)) m = pow(enc,d,N) print(long_to_bytes(m)) # flag{927ab370-fd1e-422d-8658-60b16447c86e}
import itertools from Crypto.Util.number import long_to_bytes
# n, c, hints hints = [] n, e = (73566307488763122580179867626252642940955298748752818919017828624963832700766915409125057515624347299603944790342215380220728964393071261454143348878369192979087090394858108255421841966688982884778999786076287493231499536762158941790933738200959195185310223268630105090119593363464568858268074382723204344819, 65537) enc2 = 30332590230153809507216298771130058954523332140754441956121305005101434036857592445870499808003492282406658682811671092885592290410570348283122359319554197485624784590315564056341976355615543224373344781813890901916269854242660708815123152440620383035798542275833361820196294814385622613621016771854846491244
V = hints[:4] k = 2^800 M = Matrix.column([k * v for v in V]).augment(Matrix.identity(len(V))) B = [b[1:] for b in M.LLL()] M = (k * Matrix(B[:len(V)-2])).T.augment(Matrix.identity(len(V))) B = [b[-len(V):] for b in M.LLL() ifset(b[:len(V)-2]) == {0}]
for s, t in itertools.product(range(4), repeat=2): T = s*B[0] + t*B[1] a1, a2, a3, a4 = T kq = gcd(a1 * hints[1] - a2 * hints[0], n) if1 < kq < n: print('find!', kq, s, t) break for i inrange(2**16, 1, -1): if kq % i == 0: kq //= i q = int(kq) p = int(n // kq) d = pow(0x10001, -1, (p - 1) * (q - 1)) m = pow(enc2, d, n) flag = long_to_bytes(m).decode() print(flag) enc3 = 17737974772490835017139672507261082238806983528533357501033270577311227414618940490226102450232473366793815933753927943027643033829459416623683596533955075569578787574561297243060958714055785089716571943663350360324047532058597960949979894090400134473940587235634842078030727691627400903239810993936770281755 flag = long_to_bytes(pow(enc3,d,n)).decode() print(flag) # flag{yOu_can_s0lve_the_@pbq_prob1em!!}
x = 68510878638370415044742935889020774276546916983689799210290582093686515377232591362560941306242501220803210859757512468762736941602749345887425082831572206675493389611203432014126644550502117937044804472954180498370676609819898980996282130652627551615825721459553747074503843556784456297882411861526590080037 y = 117882651978564762717266768251008799169262849451887398128580060795377656792158234083843539818050019451797822782621312362313232759168181582387488893534974006037142066091872636582259199644094998729866484138566711846974126209431468102938252566414322631620261045488855395390985797791782549179665864885691057222752 n = 94789409892878223843496496113047481402435455468813255092840207010463661854593919772268992045955100688692872116996741724352837555794276141314552518390800907711192442516993891316013640874154318671978150702691578926912235318405120588096104222702992868492960182477857526176600665556671704106974346372234964363581 c = 17737974772490835017139672507261082238806983528533357501033270577311227414618940490226102450232473366793815933753927943027643033829459416623683596533955075569578787574561297243060958714055785089716571943663350360324047532058597960949979894090400134473940587235634842078030727691627400903239810993936770281755 e = 65537
R = Integers(n) P.<a, b, p, q> = PolynomialRing(Integers(n)) f1 = a*p + q f2 = p + b*q f3 = p*q I = Ideal([f1 - x, f2 - y, f3 - n]) B = I.groebner_basis()
g = B[-1]
z = ZZ(g.coefficient({q: 1})) assert g.constant_coefficient() == R(-y)
from Crypto.Util.number import * from tqdm import * import gmpy2 from pwn import *
sh1 = remote("",) sh2 = remote("",)
sh1.recvuntil(b"[+] Welcome to the game!") sh2.recvuntil(b"[+] Welcome to the game!") sh1.recvuntil(b"[+] rsa public key:") sh2.recvuntil(b"[+] rsa public key:") public_key = eval(sh2.recvline().strip().decode()) for i inrange(100): sh1.recvuntil(b"[-] b:") sh1.sendline(b"1") sh2.recvuntil(b"[-] b:") ifb"wrong!"in sh1.recvline().strip(): sh2.sendline(b"0") print(sh2.recvline().strip()) else: sh2.sendline(b"1") print(sh2.recvline().strip())
# step1 calculate k k = (e*dh) // n + 1 # step2 dh # step3 recover the MSBs of p S = n + 1 - ((e*dh - 1) // k) D = gmpy2.iroot(abs(S**2 - 4*n),2)[0] ph = int((S + D) // 2) >> (512 - 105) << (512 - 105)
# step4 calculate p+q % blind s = inverse(k,blind) * (1 + k * (n+1) - e*dl) % blind R.<x> = PolynomialRing(Zmod(blind)) f = x^2 - s*x + n res = f.roots() may_p_mod_blind = [int(res[0][0]),int(res[1][0])]
# step5 calculate p+q % e k_inv = inverse(k,e) s = (n + 1 + k_inv) % e
# step6 factor n R.<x> = PolynomialRing(Zmod(e)) f = x^2 - s*x + n res = f.roots() may_p_mod_e = [int(res[0][0]),int(res[1][0])] moduls = int(blind) * int(e)
for p_mod_e in may_p_mod_e: for p_mod_blind in may_p_mod_blind: pl = crt([p_mod_blind,p_mod_e],[blind,e]) th = (ph - pl) // moduls PR.<x> = PolynomialRing(Zmod(n)) f = (th + x) * moduls + pl for i in trange(240, 246): roots = f.monic().small_roots(X=2^i,beta=0.49,epsilon=0.02) if roots != []: p = int(f(roots[0])) if n % p == 0: print(p) q = n // p d = inverse(e,(p-1)*(q-1)) m = pow(c,d,n) msg = long_to_bytes(m) print(msg.hex()) sh2.sendlineafter(b"[-] guess token:",msg.hex()) print(sh2.recvline()) break