import gmpy2 from Crypto.Util.number import * from flag import flag
m=bytes_to_long(flag) n = 25348605574630284342864323710011622959543974652863854537355760576386763162531478272446867731299572532294812374775121121761898206639041068156270466457595336452690367719842145233764550634280981441631262047763246059814963741143303914063537003244814908763379320576260885158458898112416692583017869283284022878603506583499699525249773663841642694427307104140944360804367072787670581252816486834658346431010523135392357008103555699542414687172408709153334263858639251735462278292703380745537045458408951791720967957274781161667526873251066303708008043058246747534357368350540174588670636827470901518225473676343782182718627 e = 65537
p_high = 1514296530850131082973956029074258536069144071110652176122006763622293335057110441067910479 q_low = 40812438243894343296354573724131194431453023461572200856406939246297219541329623 n = 21815431662065695412834116602474344081782093119269423403335882867255834302242945742413692949886248581138784199165404321893594820375775454774521554409598568793217997859258282700084148322905405227238617443766062207618899209593375881728671746850745598576485323702483634599597393910908142659231071532803602701147251570567032402848145462183405098097523810358199597631612616833723150146418889589492395974359466777040500971885443881359700735149623177757865032984744576285054725506299888069904106805731600019058631951255795316571242969336763938805465676269140733371287244624066632153110685509892188900004952700111937292221969 enc = 19073695285772829730103928222962723784199491145730661021332365516942301513989932980896145664842527253998170902799883262567366661277268801440634319694884564820420852947935710798269700777126717746701065483129644585829522353341718916661536894041337878440111845645200627940640539279744348235772441988748977191513786620459922039153862250137904894008551515928486867493608757307981955335488977402307933930592035163126858060189156114410872337004784951228340994743202032248681976932591575016798640429231399974090325134545852080425047146251781339862753527319093938929691759486362536986249207187765947926921267520150073408188188 e = 65537 mod = pow(2,265) p0 = n * inverse_mod(q_low,mod) % mod PR.<x> = PolynomialRing(Zmod(n)) for i inrange(2**5): f = p_high * (2^724) + p0 + (x * 32 + i) * mod f = f.monic() out_p = f.small_roots(2^454,0.4) iflen(out_p) != 0: print(out_p[0]) break p = out_p[0] * 32 + i * mod + p_high * (2^724) + p0 # print(p) p = 133637329398256221348922087205912367118213472434713498908220867690672019569057789598459580146410501473689139466275052698529257254973211963162087316149628000798221014338373126500646873612341158676084318494058522014519669302359038980726479317742766438142835169562422371156257894374341629012755597863752154328407 assert n % p == 0 q = n // p fai_n = (p-1) * (q-1) d = inverse_mod(e,fai_n) m = pow(enc,d,n) print(bytes.decode(long_to_bytes(m)))
p = getStrongPrime(512) q = getStrongPrime(512) n = p * q phi = (p - 1) * (q - 1) e = 7621 d = gmpy2.invert(e, phi)
flag = b"flag{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}" c = pow(bytes_to_long(flag), e, n)
dp = d % (p - 1) print(dp >> 200) print(c, e, n)
'''
c = 46735962204857190520476434898881001530665718155698898882603422023484998388668858692912250418134186095459060506275961050676051693220280588047233628259880712415593039977585805890920089318643002597837000049626154900908543384761210358835843974072960080857150727010985827690190496793207012355214605393036388807616 e = 7621 n = 140376049134934822153964243403031201922239588054133319056483413311963385321279682186354948441840374124640187894619689719746347334298621083485494086361152915457458004998419817456902929318697902819798254427945343361548635794308362823239150919240307072688623000747781103375481834571274423004856276841225675241863 '''
n = 140376049134934822153964243403031201922239588054133319056483413311963385321279682186354948441840374124640187894619689719746347334298621083485494086361152915457458004998419817456902929318697902819798254427945343361548635794308362823239150919240307072688623000747781103375481834571274423004856276841225675241863 e = 7621 c = 46735962204857190520476434898881001530665718155698898882603422023484998388668858692912250418134186095459060506275961050676051693220280588047233628259880712415593039977585805890920089318643002597837000049626154900908543384761210358835843974072960080857150727010985827690190496793207012355214605393036388807616 s = 1153696846823715458342658568392537778171840014923745253759529432977932183322553944430236879985
for k inrange(1, e): bits = 200 x0 = coppersmith(bits,k) iflen(x0) != 0: x = Integer(x0[0]) dp = x + (s << bits) p = (e*dp - 1) // k+1 if p != -1: q = n // p assert n == p * q phi = (p-1)*(q-1) d = inverse_mod(e,phi) m=pow(c,d,n)
f /= f.coefficients().pop(0) #最高次项系数化为0,coefficients是多项式的降次幂排列系数 f = f.change_ring(ZZ)
G = Sequence([], f.parent())
for i inrange(m + 1): base = N ^ (m - i) * f ^ i #收集基多项式
for shifts in itertools.product(range(d), repeat=f.nvariables()): g = base * prod(map(power, f.variables(), shifts)) G.append(g) print(G) B, monomials = G.coefficient_matrix() monomials = vector(monomials)
factors = [monomial(*bounds) for monomial in monomials] for i, factor inenumerate(factors): B.rescale_col(i, factor)
B = B.dense_matrix().LLL()
B = B.change_ring(QQ) for i, factor inenumerate(factors): B.rescale_col(i, 1 / factor)
H = Sequence([], f.parent().change_ring(QQ)) for h infilter(None, B * monomials): H.append(h) I = H.ideal() if I.dimension() == -1: H.pop() elif I.dimension() == 0: roots = [] for root in I.variety(ring=ZZ): root = tuple(R(root[var]) for var in f.variables()) roots.append(root) return roots return []
具体参数说明,例如
1 2
PR.<r,s,t> = PolynomialRing(Zmod(prime)) f = r*(s^2+2*s)-t