from Crypto.Util.number import * from flag import flag
defnext_prime(r): whileTrue: if isPrime(r): return r r += 1
defkeygen(nbit): p = getPrime(nbit // 2) P, Q = [next_prime(p * getPrime(nbit // 2 - 10)) for _ in'01'] n, K = P * Q, 2 ** (nbit >> 1) u, y = getRandomRange(1, n), getRandomRange(1, K) v = - (p + u * y) % n
pkey = (u, v, n) skey = (y, P, Q)
return pkey, skey
defencrypt(msg, pkey): u, v, n = pkey m = bytes_to_long(msg) assert m < n m0 = (u*m - v) % n whileTrue: if isPrime(u + v): break else: u += 1 e = u + v c = pow(m0, e, n) return c
nbit = 1024 pkey, skey = keygen(nbit) u, v, n = pkey c = encrypt(flag, pkey)
u = 1462429177173255359388007765931796537885368646963625242711326952977985471932962383861842635724040143033586225507124897993946275538644782337485276659791768803892242293535522679957964876776825548307042572518152706215000123872096447939862588392736653305289270336986724756913886373602016050815040147667630379593859685646307913471020561969933852495456652645591262189417744078633139731004839760821762709078987432999550663454348821414654652257866073987807333618308663409376712742627162896125313056171829232263020741802783450992451834392620606043876037571745527804406103083287186596413204262417693475997360716169601004 v = 3361480002432693752626969088049143371033687796839032797315025143270946165139685061767026950394284498430926616845318237749712235930625309923903553850166793512181385788796869552215035455995370731816925378753732950039662516557875218374075823193808692392905081204067496016151667029418998917540743277631790419809752354652686500452367372802836483170592925224959479584778030250914074383997961924181706306681930041686426001864642557173165132110893305941080323382987813126090590272821376238345672517574935462126595211630982601294558596563972548400634497302430346025087052735168147932593694561898028225523940866874133379 n = 4008883280270490147018156798752367239459738170301430156348460445088206527048348763760917689680659443318901951360516237262067529304338022837630483645196033621304254000080347982506422415455884933061116059048068199094286198189562171954474774550333796393036361152513608385296841124457358944339309759412021626022854509621495881349117414093445491445654319715891479654096144019797840785614103437600093538599616479514552612464205903106999476721076731416925688036797972413175747167321276835717505959961674004440955460813234396658192578904514644322909786797887720838286121169342271751904104529587650648676532260230880251 c = 3309629508959584128230612074347190739927545664904299989648238914737928604135368325911619522168319646327710121629807700297133099757696348608872841783442909500945895046635610910032194240479153787968351434814626345330003107353783812257020006242435243345046344898591276950746249307045935633972732008301954536072882886730280063790321460574169136949746594095419049316818205661247488681646844441794828909897999773387001878462330994053758731317170118624021445627807773860715311680550099039292463923535627372276120134300337541349809506039721311255832543793638212911911226396687521214715073293308087345418705502341630822
import itertools from Crypto.Util.number import * from tqdm import *
u = 1462429177173255359388007765931796537885368646963625242711326952977985471932962383861842635724040143033586225507124897993946275538644782337485276659791768803892242293535522679957964876776825548307042572518152706215000123872096447939862588392736653305289270336986724756913886373602016050815040147667630379593859685646307913471020561969933852495456652645591262189417744078633139731004839760821762709078987432999550663454348821414654652257866073987807333618308663409376712742627162896125313056171829232263020741802783450992451834392620606043876037571745527804406103083287186596413204262417693475997360716169601004 v = 3361480002432693752626969088049143371033687796839032797315025143270946165139685061767026950394284498430926616845318237749712235930625309923903553850166793512181385788796869552215035455995370731816925378753732950039662516557875218374075823193808692392905081204067496016151667029418998917540743277631790419809752354652686500452367372802836483170592925224959479584778030250914074383997961924181706306681930041686426001864642557173165132110893305941080323382987813126090590272821376238345672517574935462126595211630982601294558596563972548400634497302430346025087052735168147932593694561898028225523940866874133379 n = 4008883280270490147018156798752367239459738170301430156348460445088206527048348763760917689680659443318901951360516237262067529304338022837630483645196033621304254000080347982506422415455884933061116059048068199094286198189562171954474774550333796393036361152513608385296841124457358944339309759412021626022854509621495881349117414093445491445654319715891479654096144019797840785614103437600093538599616479514552612464205903106999476721076731416925688036797972413175747167321276835717505959961674004440955460813234396658192578904514644322909786797887720838286121169342271751904104529587650648676532260230880251 c = 3309629508959584128230612074347190739927545664904299989648238914737928604135368325911619522168319646327710121629807700297133099757696348608872841783442909500945895046635610910032194240479153787968351434814626345330003107353783812257020006242435243345046344898591276950746249307045935633972732008301954536072882886730280063790321460574169136949746594095419049316818205661247488681646844441794828909897999773387001878462330994053758731317170118624021445627807773860715311680550099039292463923535627372276120134300337541349809506039721311255832543793638212911911226396687521214715073293308087345418705502341630822
#coppersmith defsmall_roots(f, bounds, m=1, d=None): ifnot d: d = f.degree() R = f.base_ring() N = R.cardinality() f /= f.coefficients().pop(0) 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) 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 []
#part1 get p,y PR.<p,y>=PolynomialRing(Zmod(n)) f= v + p + u*y res = small_roots(f,bounds=(2^512,2^512),m=3,d=2) p,y = res[0]
#part2 get r1 if(0): for delta1 in trange(1,2000): PR.<r1>=PolynomialRing(Zmod(n)) f = p*r1 + delta1 f = f.monic() res = f.small_roots(X = 2^502,beta = 0.5) if(res != []): print(delta1) print(res) break
#part3 get flag r1 = 9982434694399603508133337302525981381172077094594547911881389765404140839294990193381283506463508822263498107585942085834150918110565475926894148950133 delta1 = 360 P = int(p*r1+delta1) Q = n // P phi = (P-1)*(Q-1) e = u + v whileTrue: if isPrime(int(e)): break else: e += 1 d = inverse(e,phi) c1 = pow(c,d,n) flag = (c1+v)*inverse(u,n) % n print(long_to_bytes(int(flag)))
from Crypto.Util.number import * from secret import flag, seed
defencrypt(m, seed, precision): r = (20 * sin(m) ** 3 * cos(m) ** 3 - 6 * sin(m) * cos(m) * (sin(m) ** 4 + cos(m) ** 4)).n(precision) s = (1 - cos(6 * m) - seed * r).n(precision) t = (sin(6 * m) + seed * (cos(6 * m) + 1)).n(precision) u = (s / t).n(precision) return u
defshift(x, precision): u, v = (x**3 - 3*x).n(precision), (1 - 3*x**2).n(precision) w = (u / v).n(precision) return w
precision = 1363 m = bytes_to_long(flag) t = encrypt(m, seed, precision) s = shift(t, precision)
print(f's = {s}')
output:
1
s = 4.4061969948574999706381252707706339596595993989993753525157058049520620878450909599070901658740035834714697099225869545917495720287359577329698453888804452908560270310064490162218842432355207070730163222140239639986509963808182579875037244043013930898502696038143722917574699793054569551851806943599434585896730793457949140792425837528999663586881638690611528789842156130245622849852139290458664441887058153106
import os from hashlib import * from binascii import * from Crypto.Cipher import AES from Crypto.Util.number import * from flag import flag
defnext_prime(n): whileTrue: if isPrime(n): return n n += 1
deffind(s, ch): return [i for i, ltr inenumerate(ch) if ltr == s]
defworth(c, msg): assertlen(c) == 1 w = 0 if c in msg: a = find(c, msg) w += sum([(_ + 1)**_ - _ for _ in a]) return w
defxor(mm, ym): xm = [] for i inrange(len(mm)): xm.append(mm[i] ^ ym[i]) returnbytes(xm)
deftaft(key, otp): assertlen(key) == len(otp) T = [] for _ inrange(len(key)): __ = xor(key, otp) T.append(hexlify(__)) otp = sha1(otp).hexdigest().encode() return T
defgaz(T): G = [] for t in T: R = [] for c inrange(16): R.append(worth(hex(c)[2:], t.decode())) p = next_prime(sum(R)) a = getPrime(p.bit_length() >> 3) R = [r * a % p for r in R] G.append(R) return G
deflili(gz, s): i, L = 0, [] for g in gz: s_i = bin(bytes_to_long(s[2*i:2*i + 2]))[2:].zfill(16) L.append(sum([g[_] * int(s_i[_]) for _ inrange(16)])) i += 1 return L
defgaz(T): G = [] for t in T: R = [] for c inrange(16): R.append(worth(hex(c)[2:], t.decode())) p = next_prime(sum(R)) a = getPrime(p.bit_length() >> 3) R = [r * a % p for r in R] G.append(R) return G
from M7P_output import * from Crypto.Util.number import * from tqdm import * from Crypto.Cipher import AES from hashlib import *
#part1 get keys defknapsack1(knap,sum): L = matrix(ZZ,len(knap)+1,len(knap)+1) for i inrange(len(knap)): L[i,i] = 1 L[i,-1] = knap[i] L[-1,-1] = -sum res = L.LLL() if(res[1][0] != 0and res[1][0] != 1): return res[0][:-1] else: ind = 0 for i inrange(len(res[0])): if(res[0][i] == 1): ind = i break temp = list(res[1][:-1]) temp[ind] = 1 return temp
if(1): keys = b"" for i inrange(len(G)): temp = knapsack1(G[i],L[i]) temp1 = "" for j in temp: temp1 += str(j) keys += long_to_bytes(int(temp1,2)).zfill(2)
#part2 get T defgenknap(): knap = [(_ + 1)**_ - _ for _ inrange(80)][1:] p = next_prime(sum(knap)) return knap,p
defknapsack2(knap,sum): L = matrix(ZZ,len(knap)+2,len(knap)+2) for i inrange(len(knap)): L[i,i] = 1 L[i,-1] = knap[i] L[-2,-2] = 1 L[-2,-1] = -sum L[-1,-1] = p res = L.LLL()[1][:-2] return res
defget_a(Ri): defclear_small_factors(num): temp = num for i inrange(2,10000): while(temp % i == 0): temp //= i return temp for i inrange(len(Ri)): for j inrange(i+1,len(Ri)): temp = int(clear_small_factors(GCD(Ri[i],Ri[j]))) if(isPrime(temp) and temp.bit_length() == 62): return temp
if(0): knap,p = genknap() T = [] for Ri in tqdm(G): ai = get_a(Ri) #print(ai) if(1): this_str = ["*"for i inrange(80)] count_hex = 0 for i in Ri: sumi = inverse(ai,p)*i % p index = knapsack2(knap,sumi) for j inrange(len(index)): if(index[j] != 0): if(this_str[j+1] != "*"): this_str[j+1] = "!" + this_str[j+1] + hex(count_hex)[2:] + "!" else: this_str[j+1] = hex(count_hex)[2:] #print(this_str) count_hex += 1 T.append("".join(this_str)) print(T)
#part3 get some defdec_taft(T): table = "0123456789abcdef"
temp = ["*"for i inrange(len(T))] for i inrange(len(T)): if(T[i][1] == "!"): temp[i] = long_to_bytes(int(T[i][5:],16)) else: temp[i] = long_to_bytes(int(T[i][2:],16)) temp = temp[1:]
some = "" possible_list = [] for i inrange(40-1): possible_set = set() for length inrange(len(temp)): this_possible_set = set() tempchr = temp[length][i] for j inrange(256): if(chr(tempchr ^^ j) in table): this_possible_set.add(j) if(length == 0): possible_set = this_possible_set else: possible_set = possible_set.intersection(this_possible_set) possible_list.append(possible_set) return possible_list
if(0): T = ['*!4c!de96c34bb1561914c71ae71765d734ee29d753ce794f59101646152a8bce350c5cacd70283ea64', '*fe32b81f6daef130496fb6ff2b5be34ee93b8cb7976f8c0240c68842c6faacc90d84a701cb0f391', '*!18!e125d6a7d1eb465299aa6ef6b5ed67b2c1bccd2b70ffc076066dd77f6ca69cc2d349231be7a1c3', '*!26!e7248ca6d4eb4751c5aa34f3bcbc3abb91b89a7a72f8c4225438872b38f69fc6891a2649e0a3cc', '*2b77985f6d4e84f05c3ab31a8e6ba67b296bbcd2f26acc220573684223af7c8c6d81b704eb1a0c3', '*!25!e77c8ca2daeb410c93fe31f4e2ea3ab896e99d7922aa92270339d82333f199c6dc1a701ebbf4c0', '*7b229d7f2d0ee4250c7aa31a1e2e860ba90b4912870f19e77566d822938f5cc97d34d731eb5a5c1', '*!57!b32982a5d6bc420dc7fe63a0b0e635ef9ded907874a99070023680796ca49f95d818714db7f4cc', '*7b07c86f1d6ea415093a26fa2bcbd33ea94efcc2822f996205639807838a7cbc18c1f244db4a396', '*7b22c87aa83e51702c5ad62f1e0bb32b39cbf9c2a74f090700d37d82e3bf798ca89117349b6a797', '*7ee2ed0a483e84557c3f931f1bcef3befc1e99d7c78ae9e235037d02d38a7cb96de1e744be0abcc', '*!27!b52ad7f587e54e0096fb60f1bdbe32b392eaca7f72abc220503ad72e6ea6cfc6d31e234ee1abcc', '*2b32e82aa87bb4f0698ad34f3b0e935e8c3e9c82b23a990230c6fd82f6fa49fc2891e7048e7f4c7', '**ef788da083ec420293a962a3e2ba34b3c1b5917f26fc932a5037802e68a2cdc28811244db2f1cc', '*2ef7e85a4d1b8400399ac65f4bded34be92b99f2823aa9e220c3bd52e32f69f97d24e731eb2f497', '**b42b83abd1b8430794fe35f2b4be33e894b8c87b78f89526573e87296fa5ca92894c2648e1a0c3', '**b42480f080ee4252c2ad64f6bcbd30e996e8997922fc902a576fd42d6eabcbc1da1f794db2a791', '*7ee2b87f7d3ee1005c3ff64a9e0bd3bef95bbca7971f997770137d62d3df099cadf1c794eb7abc7', '*0b07cd1a680bb4f5696ff60f5bcec65b39cb9cb7b72f891250468d12e6fa1cb928b1c2447e2f3c4', '**e279d7a2d3ed420c99ad66f6b2bd30bbc1bbc87976ad97245036d82e3fa79bc5d31c704ee5a3c5', '*!77!b52d81f7daea400298f831f3e5e73bbc94efcd2b78fe9f210439d52d38f5c8c3da4e7919e0a197','**e128d0a384ec415598ad32a2b7bd65b293ba907e22fac3230137d92239a1cc918b107948b6abc6','**b2788daa81eb1202c7f867a5e7be61b290e9cb2d24aa9773533b857b3da39fc2da4a794ce5f697','*!25!b52cd4f5dbea4f06c7fe66f4b0eb65bc97e89b7723fec277066ad07c3ba199c4884e701ee2a392','*!26!b52986f0d4e41701c3f831a1e2ea66b8c6b8cb7c77f194770d3b832a32a49ec28b19724ae1a4c7'] possible_list = dec_taft(T) some = b"*" for i in possible_list: if(len(i) == 1): some += long_to_bytes(list(i)[0]) else: some += b"*" print(some)
#part4 get flag c = b"\x8d\x04>\xeb4S\xf3\xea\x9c\xc1\xec3\x07\xfc'\xf9\xd7\xfcQ\\\xcf&\xc6N\xa1\xf1\xa4\x87\xee\xf6\xe3\x0c\xfbF,yyq\xfdK\x95\x94\x152\x9c\xcbK\x97\n\xe2#" nonce = b'B\xe3@\xee\x1ex\xee\xca \x14\x1bPT\xee\x92\x9c' tag = b'\xeb\xacdC\x85=\x17^}\x80/N\xcc\xc4F\xa3'
defxor(mm, ym): xm = [] for i inrange(len(mm)): xm.append(mm[i] ^^ ym[i]) returnbytes(xm)
defAES_dec(c,nonce,tag,key): try: cipher = AES.new(key, AES.MODE_GCM,nonce) res = cipher.decrypt_and_verify(c,tag) return res except: pass
if(1): for i inrange(256): for j in {169, 250, 253, 174}: temp = some temp = long_to_bytes(i) + temp[1:] temp = temp[:31] + long_to_bytes(j) + temp[32:] mask = AES_dec(c,nonce,tag,sha256(temp).digest()) if(mask != None): flag = xor(mask, sha512(keys).digest()[:len(mask)]) print(flag) #ASIS{h0W_dO_yUo_5oLvE_tH!s_m1X3D_5eMi_H4rD_T4Sk!!?}