N = 20289788565671012003324307131062103060859990244423187333725116068731043744218295859587498278382150779775620675092152011336913225797849717782573829179765649320271927359983554162082141908877255319715400550981462988869084618816967398571437725114356308935833701495015311197958172878812521403732038749414005661189594761246154666465178024563227666440066723650451362032162000998737626370987794816660694178305939474922064726534186386488052827919792122844587807300048430756990391177266977583227470089929347969731703368720788359127837289988944365786283419724178187242169399457608505627145016468888402441344333481249304670223 e = 11079917583 c = 13354219204055754230025847310134936965811370208880054443449019813095522768684299807719787421318648141224402269593016895821181312342830493800652737679627324687428327297369122017160142465940412477792023917546122283870042482432790385644640286392037986185997262289003477817675380787176650410819568815448960281666117602590863047680652856789877783422272330706693947399620261349458556870056095723068536573904350085124198592111773470010262148170379730937529246069218004969402885134027857991552224816835834207152308645148250837667184968030600819179396545349582556181916861808402629154688779221034610013350165801919342549766 a = 4
from gmpy2 import iroot XpXq = Integer(iroot(N, 4)[0])
tXpa_plus_sXqa = N - e - XpXq^a tXpa_minu_sXqa = Integer(pow(tXpa_plus_sXqa^2 - 4 * e * XpXq^a, 1/2)) tXpa = (tXpa_plus_sXqa + tXpa_minu_sXqa) // 2 sXqa = tXpa_plus_sXqa - tXpa
t = gcd(e, tXpa) s = gcd(e, sXqa) assert s * t == e
Xpa = tXpa // t Xqa = sXqa // s p = Xpa + s q = Xqa + t assert p * q == N
defnth(y, n, p, k=1): F = Zmod(p^k) x0 = F(y).nth_root(n) u0 = F(1).nth_root(n) x = [] u = [] for i inrange(n): u += [u0^(i+1)] x += [x0 * u[i]] returnlist(set(x))
S1 = 28572152986082018877402362001567466234043851789360735202177142484311397443337910028526704343260845684960897697228636991096551426116049875141 S2 = 1267231041216362976881495706209012999926322160351147349200659893781191687605978675590209327810284956626443266982499935032073788984220619657447889609681888 S4 = 9739918644806242673966205531575183334306589742344399829232076845951304871478438938119813187502023845332528267974698273405630514228632721928260463654612997 S5 = 9755668823764800147393276745829186812540710004256163127825800861195296361046987938775181398489372822667854079119037446327498475937494635853074634666112736 p = 25886434964719448194352673440525701654705794467884891063997131230558866479588298264578120588832128279435501897537203249743883076992668855905005985050222145380285378634993563571078034923112985724204131887907198503097115380966366598622251191576354831935118147880783949022370177789175320661630501595157946150891275992785113199863734714343650596491139321990230671901990010723398037081693145723605154355325074739107535905777351 h = 2332673914418001018316159191702497430320194762477685969994411366563846498561222483921873160125818295447435796015251682805613716554577537183122368080760105458908517619529332931042168173262127728892648742025494771751133664547888267249802368767396121189473647263861691578834674578112521646941677994097088669110583465311980605508259404858000937372665500663077299603396786862387710064061811000146453852819607311367850587534711 c = 20329058681057003355767546524327270876901063126285410163862577312957425318547938475645814390088863577141554443432653658287774537679738768993301095388221262144278253212238975358868925761055407920504398004143126310247822585095611305912801250788531962681592054588938446210412897150782558115114462054815460318533279921722893020563472010279486838372516063331845966834180751724227249589463408168677246991839581459878242111459287
for i inrange(200, 700): print(i) D = 2^i B = matrix(ZZ, [[1, D*h], [0, D*p]]) L = B.LLL() f = Integer(abs(L[0][0])) q = Integer(abs(L[0][1]) // D) ifnot h == (f.inverse_mod(p) * q) % p: continue
a = c * f % p S3 = a * f.inverse_mod(q) % q
n = gcd((S3-S2)^2 - (S2-S1)*(S4-S3), (S4-S3)^2 - (S3-S2)*(S5-S4)) if n != 1: print('f = %d' % f) print('q = %d' % q) print('n = %d' % n) break a = (S3 - S2) * (S2 - S1).inverse_mod(n) % n b = (S2 - a * S1) % n print('a = %d' % a) print('b = %d' % b) m = (S1 - b) * a.inverse_mod(n) % n import libnum print(libnum.n2s(int(m)))
''' 213 f = 413301180038546973316137674870589882147305293496057497772073927555046884257486263836713004292833493132001243260591164367442485193468991124058625234552087046120678322178758072868296612348525542661280286775661127225791195231643203301751435284753 q = 18772753754134873622668068261315956077681347161806060082164835888595835700109818730322267553874027247668932797883276028015173786305189273696314195165370843863808886406071416313879 n = 12433235385460084327215142269091752668477278692416805859007828624838647815241707248797912107322868748847211061641608674422095027981318008221949510129177787 a = 1017579321905754831612145134014116183026524698685218523407174987842084260441 b = 1244547131344198183940330607549789182491018543684349414313485985685030480 b'DASCTF{NTRU_L0G_a6e_S1mpLe}' '''
from Crypto.Util.number import * from Crypto.Cipher import DES3 from randcrack import RandCrack from string import printable import itertools import hashlib
IV1 = bytes.fromhex(hint2[: -16]) IV = IV1 * 2 print(IV)
withopen('./task.txt', 'r') as f: data = f.read() data = data.split('\n')[:-1] data = [int(x, 16) for x in data] List1 = data[: 624] List2 = data[624:] assertlen(List2) == 312
''' Leak1 = [] for i in range(0, 624, 2): Leak1 += [List1[i]*2^16 + List1[i+1]] '''
Leak2 = [] for i inrange(312): Leak2 += [List1[2*i] * 2**16 + List2[i] % 2**16] Leak2 += [List1[2*i+1] * 2**16 + (List2[i] >> 16)] assertlen(Leak2) == 624 rc = RandCrack() for l in Leak2: rc.submit(l) K2 = long_to_bytes(rc.predict_getrandbits(64))
xx = long_to_bytes(xor)[:2] hint1 = bytes_to_long(xx * 8) K1 = long_to_bytes(hint1 ^ xor) print(K1) for f7 in printable: K3 = ('DASCTF{' + f7).encode() KEY = K1 + K2 + K3
#n = 8064259277274639864655809758868795854117113170423331934498023294296505063511386001711751916634810056911517464467899780578338013011453082479880809823762824723657495915284790349150975180933698827382368698861967973964030509129133021116919437755292590841218316278732797712538885232908975173746394816520256585937380642592772746398646558097588687958541171131704233319344980232942965050635113860017117519166348100569115174644678997805783380130114530824798808098237628247236574959152847903491509751809336988273823686988619679739640305091291330211169194377552925908412183162787327977125388852329089751737463948165202565859373 # cao n = 80642592772746398646558097588687958541171131704233319344980232942965050635113860017117519166348100569115174644678997805783380130114530824798808098237628247236574959152847903491509751809336988273823686988619679739640305091291330211169194377552925908412183162787327977125388852329089751737463948165202565859373 d = 14218766449983537783699024084862960813708451888387858392014856544340557703876299258990323621963898510226357248200187173211121827541826897886277531706124228848229095880229718049075745233893843373402201077890407507625110061976931591596708901741146750809962128820611844426759462132623616118530705745098783140913
from tqdm import tqdm fra = (d/n).continued_fraction() for i in tqdm(range(len(fra))): k = fra.numerator(i) e = fra.denominator(i)
if k != 0and (e*d-1) % k == 0: try: phi = (e*d-1) // k p_plus_q = n + 1 - phi p_min_q = (p_plus_q^2 - 4*n)^(1/2) p = (p_plus_q + p_min_q) // 2 q = n // p if p*q == n: break except: continue print('p = %s' % p) print('q = %s' % q) print('e = %s' % d)
from hashlib import md5 print("Flag: DASCTF{%s}" % md5(str(p + q).encode()).hexdigest())
''' p = 10181341212828413853336916619161138854377885230386496425058202154486415709366161346816273366144505351043947477469664133317598479763451392984403646602585037 q = 7920625690369490250766357750388349704260128405941822835255851274284409978206593795103040446837018619894098452542488850045009467407103749792461438242280929 e = 14218766449983537783699024084862960813708451888387858392014856544340557703876299258990323621963898510226357248200187173211121827541826897886277531706124228848229095880229718049075745233893843373402201077890407507625110061976931591596708901741146750809962128820611844426759462132623616118530705745098783140913 Flag: DASCTF{4ae33bea90f030bfddb7ac4d9222ef8f} '''
#n = 8064259277274639864655809758868795854117113170423331934498023294296505063511386001711751916634810056911517464467899780578338013011453082479880809823762824723657495915284790349150975180933698827382368698861967973964030509129133021116919437755292590841218316278732797712538885232908975173746394816520256585937380642592772746398646558097588687958541171131704233319344980232942965050635113860017117519166348100569115174644678997805783380130114530824798808098237628247236574959152847903491509751809336988273823686988619679739640305091291330211169194377552925908412183162787327977125388852329089751737463948165202565859373 n = 80642592772746398646558097588687958541171131704233319344980232942965050635113860017117519166348100569115174644678997805783380130114530824798808098237628247236574959152847903491509751809336988273823686988619679739640305091291330211169194377552925908412183162787327977125388852329089751737463948165202565859373 d = 14218766449983537783699024084862960813708451888387858392014856544340557703876299258990323621963898510226357248200187173211121827541826897886277531706124228848229095880229718049075745233893843373402201077890407507625110061976931591596708901741146750809962128820611844426759462132623616118530705745098783140913
D = 2^512 m = matrix(ZZ, [ [D, n+1], [0, -d] ]) L = m.LLL() w = L[0] v = m.solve_left(w) k = abs(v[0]) e = abs(v[1]) print(k, e)
if k != 0: phi = (e*d-1) // k else: phi = e*d - 1 p_plus_q = n + 1 - phi p_min_q = (p_plus_q^2 - 4*n)^(1/2) p = (p_plus_q + p_min_q) // 2 q = n // p assert p*q == n
defsolve(n, ph, pl=1, pbits=1024): hbits = ph.nbits() lbits = pl.nbits() PR.<x> = PolynomialRing(Zmod(n)) f = ph * 2^(pbits-hbits) + x * 2^lbits + pl f = f.monic() roots = f.small_roots(X=2^(pbits-hbits-lbits+1), beta=0.4) if roots: pm = Integer(roots[0]) p = ph * 2^(pbits-hbits) + pm * 2^lbits + pl if n % p == 0: q = n // p return p, q returnNone p, q = solve(n, ph>>435) print('p = %d' % p) print('q = %d' % q) assert p * q == n
leb = len(pubkey) l = [pow(3, i) for i inrange(leb)] print(len(bin(sum(l))) - 1)
assert pubkey[0] * 3 == pubkey[1] w = pubkey[0] m = gcd(3*pubkey[-2] - pubkey[-1], 3*pubkey[-3] - pubkey[-2]) print('w = %d' % w) print('m = %d' % m) assertsum(l) < m
en_e = en_e * w.inverse_mod(m) % m bine = [] for i inrange(leb): bine += [str(en_e % 3)] en_e //= 3 e = Integer(''.join(bine), 2) print('e = %d' % e)
import libnum m = pow(c, e.inverse_mod((p-1)*(q-1)), n) print(libnum.n2s(int(m)))
''' p = 139540788452365306201344680691061363403552933527922544113532931871057569249632300961012384092481349965600565669315386312075890938848151802133991344036696488204791984307057923179677630589032444985150800881889090713797496239571291907818169058929859395965304623825442220206712660451198754072531986630133689525911 q = 162585259972480477964240855936099163585362299488578311068842002571891718764319834825730036484383081273549236661473286892739224906812137330941622699836239606393084030874487072527724286268715004074797344316619876830720445250395986443767703356842297999006344406006724963545062388183647988548800359369190326996261 102 w = 18143710780782459577 m = 4522492601441914729446821257037 e = 15960663600754919507 b'DASCTF{T81I_tPPS_6r7g_xlPi_OO3M_6vyV_Rkba}' '''
#from Crypto.PublicKey import RSA from base64 import b64decode withopen('./pri.pem', 'r') as f: data = f.read()
n = 0x00b9ad332fb6b87d59b5b20b4ae880ba416d8724111f99a9ed498bcb365091d83dcc43fdff9b607df8a443bcadc79907c921e76b38003b5b0ece660437803195ebfab9a7e23fc0751228fdeefe5591827523d7b79ad04d85e4db5caa13f28a7e0124357d0685e00f14ccbb9679979923c2531ff487f9ba2500ade48995c315d913 e = 0x010001 d = 0x00974ebb2da0bb0afb3603970c3e17d8b044af22070a3750b05b849ddeef1d4a986182eed3832cc8bafc316eea36835042e96c0a85a23abc637e72c7f0ea787df06127fe9dc3d21b8dae8018bdffc345107d5271ddb6d5fbc01f8cbf73f44410d61e006208356f1c5b85515efc708b34b676e78f18d4d3b68f5765d10b701f0361 p = 0x00ea59434f560de2eaf4f21c22fb10691b79485e6290007dc28242bc63739fb95fa03e5ed807000d491f0ca43e50a91d43a6940f390c91757a3ba8226ce58112c9 q = 0x00cad4c29d017e30ddabd606805044d9ca3e6a3184fb4e1f332845555498c36b02e7b97e2eb09d85c919e30a493ce94ef9412261c3998c7344271b6e6e1b3dfefb c = 91817924748361493215143897386603397612753451291462468066632608541316135642691873237492166541761504834463859351830616117238028454453831120079998631107520871612398404926417683282285787231775479511469825932022611941912754602165499500350038397852503264709127650106856760043956604644700201911063515109074933378818
print(p * q == n) m = pow(c, d, n) import libnum flag = libnum.n2s(int(m)) print(flag) key = b'C0ngr4tulati0n5_y0u_fou^d_m3' flag1 = flag[:12].decode() flag = flag[12:] flag2 = '' for i inrange(len(key)): flag2 += chr(key[i] ^^ flag[i]) flag = flag1 + flag2 print(flag)