from Crypto.Util.number import * from Crypto.Cipher import AES from hashlib import sha256 from random import randbytes, getrandbits from flag import flag defdiffie_hellman(g, p, flag): alice = getrandbits(1024) #私钥XA bob = getrandbits(1024) # 私钥XB
from Crypto.Util.number import * from Crypto.Cipher import AES from hashlib import sha256 from random import choice
defmyPrime(bits): whileTrue: n = 2 while n.bit_length() < bits: n *= choice(sieve_base) if isPrime(n + 1): return n + 1
A = B = # p = myPrime(1024) p = 515482686852905819026776471764664175154597541766196631808569599693945100129832449512555508593916309509882123983344422623771622319416757571580459907525933456572547127497459096961018073753946157799257464082944083244303912221051154205283779640770766245958565082104682846235618735174419474409835799563673915973399 A = Mod(A,p) B = Mod(B,p) key = pow(B,int(discrete_log(A,Mod(2,p))),p) key = sha256(long_to_bytes(key)).digest() c = b'' iv = b"dasctfdasctfdasc" aes = AES.new(key, AES.MODE_CBC, iv) flag = aes.decrypt(c) print(flag)
ezRSA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
from Crypto.Util.number import * from secret import secret, flag defencrypt(m): return pow(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)))
for i in range(64): if N%((pbar<<6)+i) == 0: p = (pbar<<6)+i q = N//p print("[+] p =",p) print("[+] q =",q) break
拿到 P,Q 之后我们就能恢复 n 了
1 2 3 4 5 6
e = 11 nc = 14183763184495367653522884147951054630177015952745593358354098952173965560488104213517563098676028516541915855754066719475487503348914181674929072472238449853082118064823835322313680705889432313419976738694317594843046001448855575986413338142129464525633835911168202553914150009081557835620953018542067857943 d = inverse(e,(p-1)*(q-1))
defGCD(a, b): if(b == 0): return a.monic() else: return GCD(b, a % b)
N = 54743202946161502811856295720870810456365343228950644705855322505182442571262980076941089058115540911930788333918940031879968906218781504935426145280173361374496692033334841657192812883322298846746060682411659733228146767637511270445829110291022551957289461462065489646257622668554123931913354561022893455953 c2 = 55877575842944488536738260741145576196064055314256851119903269638247167570517 c3 = 24752910240512109387212015792618368998146452177596723408887013541065473659025 P.<x> = PolynomialRing(Zmod(N)) f1 = 1997 f2 = 1997 t = 4035619213 for i in range(1,20): f1 += (x+t)^i f2 += (x*t)^i f1 -= c3 f2 -= c2 GCD(f1,f2)
defGCD(a, b): print(a.monic(),b) if(b == 0): return a.monic() else: return GCD(b, a % b)
# x + 3269720129929206172290888040434844631999790813495546199598567897695383065785748573999332043932451485440224772661966886881467981258459641224093108678852609207558615214018620184887996376816876194347015843720084751398190561322698085192313884948494127864886155782163938754041371595283348119372481511820252205397 10094707220555426811770578750536360201358369370657659611252294461972567760547511972856872356006689569965863946144471753575975482915039158199762685955836393286445719933816896842332176600699750306238028403548064414478902718240280054367062726220893659556097524038957925231227120070260148988432886582427556137193
我们看到报错前的最后一条信息,有一个 10094707220555426811770578750536360201358369370657659611252294461972567760547511972856872356006689569965863946144471753575975482915039158199762685955836393286445719933816896842332176600699750306238028403548064414478902718240280054367062726220893659556097524038957925231227120070260148988432886582427556137193 然后对他和 n 用一下 gcd
defGCD(a, b): if(b == 0): return a.monic() else: return GCD(b, a % b)
N = 112229749912829564829770165528493741966573684175830091030568865445070552505343 c2 = 55877575842944488536738260741145576196064055314256851119903269638247167570517 c3 = 24752910240512109387212015792618368998146452177596723408887013541065473659025 P.<x> = PolynomialRing(Zmod(N)) f1 = 1997 f2 = 1997 t = 4035619213 for i in range(1,20): f1 += (x+t)^i f2 += (x*t)^i f1 -= c3 f2 -= c2 print(long_to_bytes(int(N-GCD(f1,f2).coefficients()[0])))
发现还不行,我们得到的是 $m%q$,估计 $m$ 大于 $q$,爆破一下(希望不会大太多)
1 2 3 4 5 6 7 8 9 10
q = 112229749912829564829770165528493741966573684175830091030568865445070552505343 m = int(q-GCD(f1,f2).coefficients()[0]) for i in range(2**25): m+=q flag = long_to_bytes(m) if b"flag"in flag: print(flag)