import os import gmpy2 from Crypto.Util.number import * import random from secrets import flag defpad(s,l): return s + os.urandom(l - len(s)) defgen(): g = getPrime(8) whileTrue: p = g * random.getrandbits(138) + 1 if isPrime(p): break whileTrue: q = g * random.getrandbits(138) + 1 if isPrime(q): break N = p ** 5 * q phi = p ** 4 * (p - 1) * (q - 1) d = random.getrandbits(256) e = inverse(d, phi) E = e * g hint = gmpy2.gcd(E, phi) return N, E, hint
flag = pad(flag,64) m = bytes_to_long(flag) n,e,hint = gen() c = pow(m,e,n) print(f'hint = {hint}') print(f'n = {n}') print(f'e = {e}') print(f'c = {c}') # hint = 251 # n = 108960799213330048807537253155955524262938083957673388027650083719597357215238547761557943499634403020900601643719960988288543702833581456488410418793239589934165142850195998163833962875355916819854378922306890883033496525502067124670576471251882548376530637034077 # e = 3359917755894163258174451768521610910491402727660720673898848239095553816126131162471035843306464197912997253011899806560624938869918893182751614520610693643690087988363775343761651198776860913310798127832036941524620284804884136983215497742441302140070096928109039 # c = 72201537621260682675988549650349973570539366370497258107694937619698999052787116039080427209958662949131892284799148484018421298241124372816425123784602508705232247879799611203283114123802597553853842227351228626180079209388772101105198454904371772564490263034162
from Crypto.Util.number import * import itertools import gmpy2
hint = 251 n = 108960799213330048807537253155955524262938083957673388027650083719597357215238547761557943499634403020900601643719960988288543702833581456488410418793239589934165142850195998163833962875355916819854378922306890883033496525502067124670576471251882548376530637034077 e = 3359917755894163258174451768521610910491402727660720673898848239095553816126131162471035843306464197912997253011899806560624938869918893182751614520610693643690087988363775343761651198776860913310798127832036941524620284804884136983215497742441302140070096928109039 c = 72201537621260682675988549650349973570539366370497258107694937619698999052787116039080427209958662949131892284799148484018421298241124372816425123784602508705232247879799611203283114123802597553853842227351228626180079209388772101105198454904371772564490263034162
R.<x> = PolynomialRing(Zmod(n))
f = e*x - 251 f = f.monic() root = f.small_roots(X = 2^256,beta=0.16) # print(root) # 解得x = 39217838246811431279243531729119914044224429322696785472959081158748864949269
tmp = int(f(root[0])) #这里是ex-251的值
t = gmpy2.gcd(tmp,n) #求公因数 # print(t.) p = gmpy2.iroot(t,4)[0] q = n // p ^ 5 print("p =",p) print("q =",q)
n_list = [ZZ(p_list[i]) ** mi[i] for i inrange(len(mi))] #就是[p^5 , q] res = []
for pi in n_list: d = inverse(int(e//251),euler_phi(pi)) # 对n_list 每一个 pi 求欧拉函数 m = pow(c,d,pi) # 在子群求解m^251 res.append(Zmod(pi)(m).nth_root(251, all=True)) #这里用.nth_root是为了求所有可能的根? for vc in itertools.product(*res): _c = [int(x) for x in vc] m = crt(_c, n_list) flag = long_to_bytes(int(m)) ifb"flag"in flag: print(flag) break