from Crypto.Util.number import getPrime, bytes_to_long
defYiJiuJiuQiNian(Wo, Xue, Hui, Le, Kai): Qi = 1997 Che = Wo+Hui if Le==1else Wo*Hui while(Xue): Qi += (pow(Che, Xue, Kai)) % Kai Xue -= 1 return Qi l = 512 m = bytes_to_long(flag) p = getPrime(l) q = getPrime(l//2) r = getPrime(l//2) n = p * q * r t = getrandbits(32) c1 = YiJiuJiuQiNian(t, 4, p, 1, n) c2 = YiJiuJiuQiNian(m, 19, t, 0, q) c3 = YiJiuJiuQiNian(m, 19, t, 1, q) print(f"n = {n}") print(f"c1 = {c1}") print(f"c2 = {c2}") print(f"c3 = {c3}") """ n = 119156144845956004769507478085325079414190248780654060840257869477965140304727088685316579445017214576182010373548273474121727778923582544853293534996805340795355149795694121455249972628980952137874014208209750135683003125079012121116063371902985706907482988687895813788980275896804461285403779036508897592103 c1 = 185012145382155564763088060801282407144264652101028110644849089283749320447842262397065972319766119386744305208284231153853897076876529326779092899879401876069911627013491974285327376378421323298147156687497709488102574369005495618201253946225697404932436143348932178069698091761601958275626264379615139864425 c2 = 722022978284031841958768129010024257235769706227005483829360633993196299360813 c3 = 999691052172645326792013409327337026208773437618455136594949462410165608463231 """
第一部分:
c1 = 1997 + (p+t)^4 + (p+t)^3 +(p+t)^2+(p+t) mod n c1 = 1997 + (p+t)^4 + (p+t)^3 +(p+t)^2+(p+t) mod p c1 = 1997 + t^4 + t^3 + t^2 + t mod p 该式显然对模n成立 c1 = 1997 + t^4 + t^3 + t^2 + t mod n
m=B[0].constant_coefficient()#wp看到的,这函数干嘛的,应该是求解这个全局基用的,但是按我的理解这里的m不应该是-211...吗为什么这里出来是正的,看wp那边也是手动加了负号,emmm迟点再查查 m=(-m)%q #m直接出来东西不对。因为是在模q下,真m值可能大于q,看都是爆破的,前面在模q下解copper试了一下也没出来东西,那我也来爆一个 for i inrange(10000000): flag=long_to_bytes(int(m)+i*int(q)) ifb"dasctf{"in flag : print(flag) break
dasctf{ShangPoXiaPoYaSiLeYiQianDuo}
出来了。另外有个求出q后不继续求解groebner_basis,用其他方式求m的。
想明白了,直接贴一个吧。
1 2 3 4 5 6 7
P.<x> = PolynomialRing(Zmod(q)) f1=1997-c2 f2=1997-c3 for i inrange(1,20): f1+=(x*t)^i f2+=(x+t)^i print(-gcd(f1,f2)[0])
from Crypto.Util.number import * from secret import secret, flag defencrypt(m): returnpow(m, e, n) assert flag == b"dasctf{" + secret + b"}" e = 11 p = getPrime(512) q = getPrime(512) n = p * q P = getPrime(512) Q = getPrime(512) N = P * Q gift = P ^ (Q >> 16) print(N, gift, pow(n, e, N)) print(encrypt(bytes_to_long(secret)), encrypt(bytes_to_long(flag)))
defGCD(a,b): if b==0: return a.monic()#a.monic()是一个多项式a的方法,用于将多项式转化为首项系数为1的首一多项式(monic polynomial) else: return GCD(b,a%b) PR.<x>=PolynomialRing(Zmod(n)) f1=x^e-c2 for i inrange(50): f2=(bytes_to_long(b"dasctf{"+b'\x00'*i+b"}")+256*x)^e-c3 res=GCD(f1,f2) if res[0]!=1: print(long_to_bytes(int(-res[0]%n)))
from gmpy2 import * from sage.allimport * from Crypto.Util.number import* N=75000029602085996700582008490482326525611947919932949726582734167668021800854674616074297109962078048435714672088452939300776268788888016125632084529419230038436738761550906906671010312930801751000022200360857089338231002088730471277277319253053479367509575754258003761447489654232217266317081318035524086377 c1=14183763184495367653522884147951054630177015952745593358354098952173965560488104213517563098676028516541915855754066719475487503348914181674929072472238449853082118064823835322313680705889432313419976738694317594843046001448855575986413338142129464525633835911168202553914150009081557835620953018542067857943 c2=69307306970629523181683439240748426263979206546157895088924929426911355406769672385984829784804673821643976780928024209092360092670457978154309402591145689825571209515868435608753923870043647892816574684663993415796465074027369407799009929334083395577490711236614662941070610575313972839165233651342137645009 c3=46997465834324781573963709865566777091686340553483507705539161842460528999282057880362259416654012854237739527277448599755805614622531827257136959664035098209206110290879482726083191005164961200125296999449598766201435057091624225218351537278712880859703730566080874333989361396420522357001928540408351500991 e=11 P=8006847171912577069085166877758626954304824756138758266557706391662987806065132448544117840031499707938227955094109779732609035310252723066470330862622641 Q=9366986529377069783394625848920106951220134111548343265311677163992169555436421569730703291128771472885865288798344038000984911921843088200997725324682297 phi=(Q-1)*(P-1) d=gmpy2.invert(e,phi) n=pow(c1,d,N) n=int(n)+N defgetflag(a,e,n,c2,c3): P.< m,m0 >= PolynomialRing(Zmod(n)) f1 = m ^ e - c2 f2 = (a+256*m) ^ e - c3 F=[f1,f2] B=Ideal(F).groebner_basis() res=[i.constant_coefficient() for i in B] #print(B) print(long_to_bytes(int(-res[0]%n)))
for i inrange(50): getflag(bytes_to_long(b"dasctf{"+b'\x00'*i+b"}"),e,n,c2,c3)