input_str = leak result = swap_bits(input_str) a=result
defcustom_add(input_str): input_list = list(input_str) length = len(input_list) for i inrange(length): input_list[i] = str((int(input_list[i]) + i + 1) % 10)
result = ''.join(input_list) return result
input_str = a result = custom_add(input_str) b=result print(b) #12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567891134567789112455679912235677900123557889112356679001245568890233457780113355689901335667991134457889122355788001345578891133566790113445688902335578800124456899022346779902344567991223557880013445689911245667801134456799122355788001344578891223467799112455678912235667900123556899022346779001245578801233467789112355779912234577990233556780113
关键在于
1 2 3 4 5 6 7 8 9
defcustom_add(input_str): input_list = list(input_str) length = len(input_list) for i inrange(length): input_list[i] = str((int(input_list[i]) + i + 1) % 10)
c = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567891134567789112455679912235677900123557889112356679001245568890233457780113355689901335667991134457889122355788001345578891133566790113445688902335578800124456899022346779902344567991223557880013445689911245667801134456799122355788001344578891223467799112455678912235667900123556899022346779001245578801233467789112355779912234577990233556780113"
c = list(c)
m = "" for i inrange(len(c)): idx = i+1 if (idx + 0) % 10 == int(c[i]): m += "0" if (idx + 1) % 10 == int(c[i]): m += "1" print(m) defswap_bits(input_str): input_list = list(input_str[2:]) length = len(input_list)
m = bytes_to_long(flag) p, q = get_happy_prime() n = p * q e = 65537 print(n) print(pow(m, e, n)) # 24852206647750545040640868093921252282805229864862413863025873203291042799096787789288461426555716785288286492530194901130042940279109598071958012303179823645151637759103558737126271435636657767272703908384802528366090871653024192321398785017073393201385586868836278447340624427705360349350604325533927890879 # 14767985399473111932544176852718061186100743117407141435994374261886396781040934632110608219482140465671269958180849886097491653105939368395716596413352563005027867546585191103214650790884720729601171517615620202183534021987618146862260558624458833387692782722514796407503120297235224234298891794056695442287
n = 24852206647750545040640868093921252282805229864862413863025873203291042799096787789288461426555716785288286492530194901130042940279109598071958012303179823645151637759103558737126271435636657767272703908384802528366090871653024192321398785017073393201385586868836278447340624427705360349350604325533927890879 c = 14767985399473111932544176852718061186100743117407141435994374261886396781040934632110608219482140465671269958180849886097491653105939368395716596413352563005027867546585191103214650790884720729601171517615620202183534021987618146862260558624458833387692782722514796407503120297235224234298891794056695442287
while1: p = gmpy2.next_prime(p) if n % p == 0: q = n // p d = gmpy2.invert(65537,(p-1)*(q-1)) m = pow(c,d,n) print(long_to_bytes(m)) break # XYCTF{3f22f4efe3bbbc71bbcc999a0a622a1a23303cdc}
import random from Crypto.Util.number import * from secrets import flag
defget_happy_prime(): while1: p = int("".join([random.choice("123") for _ inrange(512)])) q = int("".join([random.choice("567") for _ inrange(512)])) if isPrime(p) and isPrime(q): return (p,q)
m = bytes_to_long(flag) p ,q= get_happy_prime() n = p * q e = 65537 print(n) print(pow(m, e, n)) # 697906506747097082736076931509594586899561519277373830451275402914416296858960649459482027106166486723487162428522597262774248272216088755005277069446993003521270487750989061229071167729138628583207229945902389632065500739730301375338674342457656803764567184544685006193130563116558641331897204457729877920989968662546183628637193220770495938729301979912328865798266631957128761871326655572836258178871966196973138373358029531478246243442559418904559585334351259080578222274926069834941166567112522869638854253933559832822899069320370733424453856240903784235604251466010104012061821038897933884352804297256364409157501116832788696434711523621632436970698827611375698724661553712549209133526623456888111161142213830821361143023186927163314212097199831985368310770663850851571934739809387798422381702174820982531508641022827776262236373967579266271031713520262606203067411268482553539580686495739014567368858613520107678565628269250835478345171330669316220473129104495659093134763261751546990704365966783697780787341963138501 # 15338382608510229658123853967766869664415614805902686881375901510613913129713509783166104849307940522697222249215135610575923574950232430304703734941070902115225531542928076063911372434583653208797091845335372309055445058165793084767493022611384017236866283875644636448297709247897983820939676127932653341969905620998372184248499615002540300964465367892802586144532471541989379701587554152559013584302731232223608558157145208447726258296697270257713690438574144387052720564087444661641391723126013336422724892849257461024888113736420491400141226974046185174788335541496849927294459007162322360350169800422775333555264671556780282575579959795540922800428473974374953127083308485011357471204122489604452529259126463745279715109880260418631172459745078052014041370469737420965336996945150162758346789316041278073257508584646728913492088678995233817419320223417529965268756023259321213169345696631867084360523895872412636818528970356359147704910553852824463243486996533372269183746259112837981658272336703967402861994705714454
from Crypto.Util.number import * import itertools import gmpy2
n = 697906506747097082736076931509594586899561519277373830451275402914416296858960649459482027106166486723487162428522597262774248272216088755005277069446993003521270487750989061229071167729138628583207229945902389632065500739730301375338674342457656803764567184544685006193130563116558641331897204457729877920989968662546183628637193220770495938729301979912328865798266631957128761871326655572836258178871966196973138373358029531478246243442559418904559585334351259080578222274926069834941166567112522869638854253933559832822899069320370733424453856240903784235604251466010104012061821038897933884352804297256364409157501116832788696434711523621632436970698827611375698724661553712549209133526623456888111161142213830821361143023186927163314212097199831985368310770663850851571934739809387798422381702174820982531508641022827776262236373967579266271031713520262606203067411268482553539580686495739014567368858613520107678565628269250835478345171330669316220473129104495659093134763261751546990704365966783697780787341963138501 e = 65537 c = 153383826085102296581238539677668696644156148059026868813759015106139131297135097831661048493079405226972222492151356105759235749502324303047037349410709021152255315429280760639113724345836532087970918453353723090554450581657930847674930226113840172368662838756446364482977092478979838209396761279326533419699056209983721842484996150025403009644653678928025861445324715419893797015875541525590135843027312322236085581571452084477262582966972702577136904385741443870527205640874446616413917231260133364227248928492574610248881137364204914001412269740461851747883355414968499272944590071623223603501698004227753335552646715567802825755799597955409228004284739743749531270833084850113574712041224896044525292591264637452797151098802604186311724597450780520140413704697374209653369969451501627583467893160412780732575085846467289134920886789952338174193202234175299652687560232593212131693456966318670843605238958724126368185289703563591477049105538528244632434869965333722691837462591128379816582723367039674028619947057144546
deffindp(p,q): l = len(p) if l == 512and n % int(p) == 0: q = n // int(p) print(f"p = {p}") print(f"q = {q}") d = gmpy2.invert(e,(int(p)-1)*(int(q)-1)) m = pow(c,d,n) print(long_to_bytes(m)) else: table1 = ["1","2","3"] table2 = ["5","6","7"] for i in table1: for j in table2: pp = int(i + p) qq = int(j + q) if pp * qq % 10**l == n % 10**l: findp(i+p,j+q)
defget_happy_prime(): p = getPrime(n // 2) q = getPrime(n // 2) N = p * q if2 * q - p > gmpy2.iroot(N, 3)[0] and2 * q - p < int(N**gamma / 16): d = random.randint(int(N ** (delta - 0.001)), int(N**delta)) e = gmpy2.invert(d, (p - 1) * (q - 1)) return N, e
N, e = get_happy_prime() m = bytes_to_long(flag) print(N) print(e) print(pow(m, e, N)) # 262520792590557046276663232446026434827811478251833823563978322470880473096166170973396967071440576532100045206129176936699321058518888771220060450723297988984995658582283884954240129867538637146924315603797818982078845855992582229939200744016516540207525916858674450616913948112117516831530578235916528257187 # 202935305174706906986376186864051444100197589482194720650385604617995167023220940138899902073948844285283834058445151666398192104922822110762965750590312021079147170573038600118139119881722125346545331027913695559130179278058419339157557267195396326664433859683010193688491694572425231599139974726246205888139 # 66173406204647583141174847814283540389326401056490970592017577810775087017197392456634167609680060931553017712058528056769618111925816921272571306246667527546366331057781905353553621522209713637253380859235235590286779517552635935384231694255450099095196595516174794966487058693308544109311774074970769293357
第一感觉是出自某论文,关键词"RSA" "d < n^0.324",搜到名为Revisiting Wiener’s Attack – New Weak Keys in RSA的论文
n = 262520792590557046276663232446026434827811478251833823563978322470880473096166170973396967071440576532100045206129176936699321058518888771220060450723297988984995658582283884954240129867538637146924315603797818982078845855992582229939200744016516540207525916858674450616913948112117516831530578235916528257187 e = 202935305174706906986376186864051444100197589482194720650385604617995167023220940138899902073948844285283834058445151666398192104922822110762965750590312021079147170573038600118139119881722125346545331027913695559130179278058419339157557267195396326664433859683010193688491694572425231599139974726246205888139 c = 66173406204647583141174847814283540389326401056490970592017577810775087017197392456634167609680060931553017712058528056769618111925816921272571306246667527546366331057781905353553621522209713637253380859235235590286779517552635935384231694255450099095196595516174794966487058693308544109311774074970769293357
# n = 227356514376456085145407643699497785267575964192664414706015618659113920770516068763728136578026699605165351438105331282008556258187994169710089246109279146381472361264666736466411449942059568093916061632275622633234439324940363916123064654025553033995485190281219787597633737574334427577414563344330427377471759256645329 # e = 85356738187677927267094758044990579754357485762742350715347494115752841684037367619580505169859555149633498979366195155524089607956973186706608891521632808424477556097376663853312064312353402461172064273993869764933453316151177386412753448356073872108358709307048969215446586611896268736369229047317637983628682308907311 n_2 = sqrt(n)
temp = int(n_2 * 3 / sqrt(2)) + 1
phi = n - temp + 1 cf = continued_fraction(e / phi) for i inrange(1,1000): k = cf.numerator(i) d = cf.denominator(i) if (e*d - 1) % k == 0: print(d) m = pow(c,d,n) print(long_to_bytes(int(m))) # XYCTF{68f1880cdafd99fbf9a156946cb39dd86477886f1d115636e149e12c16f99af0}
开根号建议用sqrt,别用gmpy2.iroot
factor1
task.py
1 2 3 4 5 6 7 8 9 10 11 12 13
import gmpy2 import hashlib from Crypto.Util.number import *
p = getPrime(512) q = getPrime(512) d = getPrime(512) e = gmpy2.invert(d, (p**3 - 1) * (q**3 - 1)) flag = "XYCTF{" + hashlib.md5(str(p + q).encode()).hexdigest() + "}" print(e) print(p * q) # 172005065945326769176157335849432320425605083524943730546805772515111751580759726759492349719668775270727323745284785341119685198468883978645793770975366048506237371435027612758232099414404389043740306443065413069994232238075194102578269859784981454218948784071599231415554297361219709787507633404217550013282713899284609273532223781487419770338416653260109238572639243087280632577902857385265070736208291583497988891353312351322545840742380550393294960815728021248513046077985900158814037534487146730483099151396746751774427787635287611736111679074330407715700153025952858666841328055071403960165321273972935204988906850585454805923440635864200149694398767776539993952528995717480620593326867245714074205285828967234591508039849777840636255379730281105670496110061909219669860172557450779495125345533232776767292561378244884362014224844319802810586344516400297830227894063759083198761120293919537342405893653545157892446163 # 99075185389443078008327214328328747792385153883836599753096971412377366865826254033534293886034828804219037466246175526347014045811852531994537520303063113985486063022444972761276531422538694915030159420989401280012025249129111871649831185047820236417385693285461420040134313833571949090757635806658958193793
n = 99075185389443078008327214328328747792385153883836599753096971412377366865826254033534293886034828804219037466246175526347014045811852531994537520303063113985486063022444972761276531422538694915030159420989401280012025249129111871649831185047820236417385693285461420040134313833571949090757635806658958193793 e = 172005065945326769176157335849432320425605083524943730546805772515111751580759726759492349719668775270727323745284785341119685198468883978645793770975366048506237371435027612758232099414404389043740306443065413069994232238075194102578269859784981454218948784071599231415554297361219709787507633404217550013282713899284609273532223781487419770338416653260109238572639243087280632577902857385265070736208291583497988891353312351322545840742380550393294960815728021248513046077985900158814037534487146730483099151396746751774427787635287611736111679074330407715700153025952858666841328055071403960165321273972935204988906850585454805923440635864200149694398767776539993952528995717480620593326867245714074205285828967234591508039849777840636255379730281105670496110061909219669860172557450779495125345533232776767292561378244884362014224844319802810586344516400297830227894063759083198761120293919537342405893653545157892446163
# for i in trange(2,2000): # k = int(cf.numerator(i)) #取连分数的分子 # d = int(cf.denominator(i)) # if (e*d - 1)%k == 0: # print(f"k = {k}") # print(f"d = {d}")
k = 1494016303704162064457264904035459059241052443477469129020991127731711502975536480528822831565100775167693515363479900721259984367515846296462994230670260 d = 8447122254265361577759220083550460887840558233627984117576685838469227480934556534673167325385487344741530262745308367064419215281251764917289925433582347
# phi = (e * d - 1) // k # f1 = (p^3 - 1)*(q^3 - 1) == phi # f2 = p*q == n # res = solve([f1,f2],[p,q]) # print(res)
p = 9212046353930376594996890089494718736894378991991381248242532319628627449681664076081705664941905594411935750003102856235503684466394327681725704255564467 q = 10754959493573546439510276829300246769373124436128170955050379041986504869221750743052397622171703140881050431144683659643071578143360949942206693325622779 flag = "XYCTF{" + hashlib.md5(str(p + q).encode()).hexdigest() + "}" print(flag) # XYCTF{a83211a70e18145a59671c08ddc67ba4}
n = 913125842482770239379848062277162627509794409924607555622246822717218133091223291889541294440266178282194506242444509803611492259403578922020590849630191477864719052980160940803309686069818208833547621252544423652489179493083138385424424384165228024273745733240109761707533778691158938848158094054261174692601673435971526522219273943464877956131040249169850420336023942653021547841666224446678539579529590840999008107782784268926145671962239929431694391039559247 e = 494518390582436635999115147756676313570637682518235195828939117782099618734167908630788943568232122157772909140885391963441876427590731524706959546524212914108888799081844320513851526790475333924396837458796755678072486028072639014677580265244176441153444956871730684233063789931539669072735599696830757690822185323538738397827461580678488181113667710378657058297572328491762536595872579603698945272140918157163640403488075948987156585480146162739943419183496337465468187233821931312507662218106713861638334075899266373256620752680354704533272722692596941861606161634082613228896420520465402725359166156632884432690715903666803067996854084671477445131853993177110154928274312496230096270510089973592664248613332000290545537840595645944390047611474888693558676781309912289044962293014118087259307560444929227407113819165713213046898243995956550944640168932947118400215917515277554126694376415569909534496134700668701465649939 c = 4450931337369461482106945992542133557585962894030505065110870389112565329875502952762182372926117037373210509516570958483606566274369840551132381128665744266165792377925899683228751870742727716
for i inrange(1,1000): k = cf.numerator(i) d = cf.denominator(i) if (e*d - 1) % k == 0andint(d).bit_length() == 320: print(f"k = {k}") print(f"d = {d}") m = c ^^ d^2 print(long_to_bytes(int(m))) # XYCTF{I_love_to_read_the_crypto_paper_and_try_to_ak_them}
from Crypto.Util.number import * from gmpy2 import * from random import choice
flag = b'XYCTF{******}' e = '?' defgetBabyPrime(nbits): whileTrue: p = 1 while p.bit_length() <= nbits: p *= choice(sieve_base) if isPrime(p+1): return p+1
p = getBabyPrime(512) q = getBabyPrime(512) n = p*q gift1 = (pow(p,e,n)-pow(q,e,n)) % n gift2 = pow(p+q,e,n)
n = 39332423872740210783246069030855946244104982381157166843977599780233911183158560901377359925435092326653303964261550158658551518626014048783435245471536959844874036516931542444719549997971482644905523459407775392702211086149279473784796202020281909706723380472571862792003687423791576530085747716706475220532321 gift1 = 4549402444746338327349007235818187793950285105091726167573552412678416759694660166956782755631447271662108564084382098562999950228708300902201571583419116299932264478381197034402338481872937576172197202519770782458343606060544694608852844228400457232100904217062914047342663534138668490328400022651816597367310 gift2 = 111061215998959709920736448050860427855012026815376672067601244053580566359594802604251992986382187891022583247997994146019970445247509119719411310760491983876636264003942870756402328634092146799825005835867245563420135253048223898334460067523975023732153230791136870324302259127159852763634051238811969161011462 c = 16938927825234407267026017561045490265698491840814929432152839745035946118743714566623315033802681009017695526374397370343984360997903165842591414203197184946588470355728984912522040744691974819630118163976259246941579063687857994193309554129816268931672391946592680578681270693589911021465752454315629283033043 y = 1813650001270967709841306491297716908969425248888510985109381881270362755031385564927869313112540534780853966341044526856705589020295048473305762088786992446350060024881117741041260391405962817182674421715239197211274668450947666394594121764333794138308442124114744892164155894256326961605137479286082964520217 t = 65537
q = gmpy2.gcd(gift1-gift2,n) p = n // q
phi = (p-1)*(q-1) d = gmpy2.invert(t,phi) e = long_to_bytes(pow(y,d,n)) # XYCTF{e==4096}
e = 4096 # print(gmpy2.gcd(e,phi))
inv_p = gmpy2.invert(p, q) inv_q = gmpy2.invert(q, p) cs = [c] for i inrange(12): ps = [] for c2 in cs: r = pow(c2, (p + 1) // 4, p) s = pow(c2, (q + 1) // 4, q)
x = (r * inv_q * q + s * inv_p * p) % n y = (r * inv_q * q - s * inv_p * p) % n if x notin ps: ps.append(x) if n - x notin ps: ps.append(n - x) if y notin ps: ps.append(y) if n - y notin ps: ps.append(n - y) cs = ps
for m in cs: print(long_to_bytes(m)) # XYCTF{Rabin_is_so_biggggg!}
defcomplex_pow(c, exp, n): result = Complex(1, 0) while exp > 0: if exp & 1: result = result * c result.re = result.re % n result.im = result.im % n c = c * c c.re = c.re % n c.im = c.im % n exp >>= 1 return result
flag = flag.strip(b"XYCTF{").strip(b"}") p = 1127236854942215744482170859284245684922507818478439319428888584898927520579579027 g = Complex(3, 7) x = bytes_to_long(flag) print(complex_pow(g, x, p)) # 5699996596230726507553778181714315375600519769517892864468100565238657988087817 + 198037503897625840198829901785272602849546728822078622977599179234202360717671908i
利用次方和模长的关系,对于本题有 $$ (3+7i)^x \equiv c_{re} + c_{im}i \mod p $$ 我们可以把它转换成 $$ |3+7i|^x \equiv |c_{re}+c_{im}i| \mod p $$ 可以平方一些,即 $$ (|3+7i|^2)^x \equiv |c_{re} + c_{im}i|^2 \mod p $$
exp
1 2 3 4 5 6 7 8 9 10 11
p = 1127236854942215744482170859284245684922507818478439319428888584898927520579579027
g = 3^2 + 7^2 a = 5699996596230726507553778181714315375600519769517892864468100565238657988087817 b = 198037503897625840198829901785272602849546728822078622977599179234202360717671908 c = (a^2 + b^2) % p
x = discrete_log(mod(c,p),mod(g,p)) flag = b"XYCTF{" + bytes.fromhex(hex(int(x))[2:]) + b"}" print(flag) # XYCTF{___c0mp13x_d1p_15_3@5y_f0r_y0u___}
m = bytes_to_long(pad(flag)) p = getStrongPrime(512) q = getStrongPrime(512) n = p * q e = 3 print(pow(m, e, n)) print(n) # 120440199294949712392334113337541924034371176306546446428347114627162894108760435789068328282135879182130546564535108930827440004987170619301799710272329673259390065147556073101312748104743572369383346039000998822862286001416166288971531241789864076857299162050026949096919395896174243383291126202796610039053 # 143413213355903851638663645270518081058249439863120739973910994223793329606595495141951165221740599158773181585002460087410975579141155680671886930801733174300593785562287068287654547100320094291092508723488470015821072834947151827362715749438612812148855627557719115676595686347541785037035334177162406305243
c = 120440199294949712392334113337541924034371176306546446428347114627162894108760435789068328282135879182130546564535108930827440004987170619301799710272329673259390065147556073101312748104743572369383346039000998822862286001416166288971531241789864076857299162050026949096919395896174243383291126202796610039053 n = 143413213355903851638663645270518081058249439863120739973910994223793329606595495141951165221740599158773181585002460087410975579141155680671886930801733174300593785562287068287654547100320094291092508723488470015821072834947151827362715749438612812148855627557719115676595686347541785037035334177162406305243
p = getStrongPrime(1024) q = getStrongPrime(1024) n = p * q e = 2 m = bytes_to_long(pad(flag)) print(pow(m, e, n)) print(n) # 3335299537518434350008670067273514020883265809787658909900831303201069228111667477512288715627313377374377192065531931991830331266940281529429758933125645068623703704431432931062515459304407129764836169638068667723468109909932687335727824459807903558617156661138991973933280805156893120951135488168923425258119689993859896540097318197048436676318334502053269738046279338047497258783747007084564814994803144049365117574904704816542523015746396519693505167963245600047742456478545640334467678554748227823020862550712083249012329745708139070338928730226897923885785783461594034339595106377701306570280371612953393097739 # 26278624299187148406559772770865336226934633734285979741424867540828670397865189685966828527168795621543490979182417570078991930822041468539855006585233692884235331084907340302530060261742100702658312435180527335101284800616106884692498603300926488358017928867672861988448488439356448543527810620591324774111321619391264173779312033252573140028630441135269056099074531502841259940379636699304810677716177080486265721322966814104627525953974143476452058638927511594884002185219080847495835727300670028011001853179659250270200020884333850083063514830095064730932997593930711871108436386821290545084229347398808220810263
记len(flag) = l $$ flag = m_1\times 256^{128 + \frac{l}{2}} + hash \times 256^{\frac{l}{2}} + m_2 $$
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 []
from Crypto.Util.number import * import hashlib import itertools from tqdm import *
defattack(l,n,c): R.<m1,m2> = PolynomialRing(Zmod(n)) pad = bytes_to_long(hash(str(l))) if l % 2 == 1: f = (m1*2^(pad.bit_length() + 8*(l // 2)+8) + pad*2^(8*(l // 2)+8) + m2)^2 - c res = small_roots(f,bounds=(256^(l//2),256^(l//2+1)),m = 1,d=4) if res != []: x,y = res[0] m = x *2^(pad.bit_length() + 8*(l // 2)+8) + pad*2^(8*(l // 2)+8) + y msg = long_to_bytes(int(m)) flag = msg[:l // 2] + msg[-(l//2 + 1):] print(flag) print(f"len(flag) = {l}") else: f = (m1*2^(pad.bit_length() + 8*(l // 2)) + pad*2^(8*(l // 2)) + m2)^2 - c res = small_roots(f,bounds=(256^(l//2),256^(l//2)),m = 1,d=4) if res != []: x,y = res[0] m = x *2^(pad.bit_length() + 8*(l // 2)) + pad*2^(8*(l // 2)) + y msg = long_to_bytes(int(m)) flag = msg[:l // 2] + msg[-(l//2):] print(flag) print(f"len(flag) = {l}")
for l in trange(65,77): c = 3335299537518434350008670067273514020883265809787658909900831303201069228111667477512288715627313377374377192065531931991830331266940281529429758933125645068623703704431432931062515459304407129764836169638068667723468109909932687335727824459807903558617156661138991973933280805156893120951135488168923425258119689993859896540097318197048436676318334502053269738046279338047497258783747007084564814994803144049365117574904704816542523015746396519693505167963245600047742456478545640334467678554748227823020862550712083249012329745708139070338928730226897923885785783461594034339595106377701306570280371612953393097739 n = 26278624299187148406559772770865336226934633734285979741424867540828670397865189685966828527168795621543490979182417570078991930822041468539855006585233692884235331084907340302530060261742100702658312435180527335101284800616106884692498603300926488358017928867672861988448488439356448543527810620591324774111321619391264173779312033252573140028630441135269056099074531502841259940379636699304810677716177080486265721322966814104627525953974143476452058638927511594884002185219080847495835727300670028011001853179659250270200020884333850083063514830095064730932997593930711871108436386821290545084229347398808220810263 attack(l,n,c)
经测试,在二元copper中
m=1,d=3,可以有出长度为64的flag,相当于两个界都是$2^{256}$
m=1,d=4,可以有出长度为76的flag,相当于两个界都是$2^{304}$
重生之我要当oi爷 pro
task.py
1 2 3 4 5 6 7 8 9 10 11 12 13
deff(a, b, p): t = 0 for i inrange(len(b)): t += pow(a, i, p) * b[i] % p return t % p
p = 1041231053
a = open("flag.png", "rb").read() b = open("enc.txt", "w") for i inrange(len(a)): b.write(str(i) + str(f(i, a, p)) + "\n")
p = 1849790472911267366045392456893126092698743308291512220657006129900961168811898822553602045875909 L = Matrix(Zmod(p),[ [9,6,0], [0,0,1], [-36,-27,0] ])
A0 = vector(Zmod(p),[69,48,52]) A1 = A0 * L A2 = A1 * L An = vector(Zmod(p),[1431995965813617415860695748430644570118959991271395110995534704629241309597572003500157255135707, 1011565891130611736600822618382801465506651972373410962205810570075870804325974377971089090196019, 784497518859893244278116222363814433595961164446277297084989532659832474887622082585456138030246]) An1 = An * L An2 = An1 * L
A = Matrix(Zmod(p),[A0,A1,A2]) B = Matrix(Zmod(p),[An,An1,An2]) M = A.solve_right(B) # L^n
L_Jor, P = L.jordan_form(transformation=True)
T = L_Jor Tn = ~P * M * P # T^n
m = T[0][0] * Tn[0][1] // Tn[0][0] flag = long_to_bytes(int(m)) print(flag) # XYCTF{y0u_finally_f0und_t3h_s3cr3ts!!}
ks = [randint(0, rr**(i+key1-1)) for i inrange(16)] c1 = pow(sum(k*flag**i for i, k inenumerate(ks)), 127, n) c2 = pow(flag, 65537, n) ks = [pow(69, k+key, rr**(i+key1)) for i, k inenumerate(ks)]
from Crypto.Util.number import * from random import randint from tqdm import *
n = rr = ks = [...] c1 = c2 = final = []
#part1 dlog by p-adic for i in trange(16): R = Zp(rr, prec=i+3) x = (R(ks[i]).log() / R(69).log()).lift() final.append(x)
#part2 HGCD defHGCD(a, b): if2 * b.degree() <= a.degree() or a.degree() == 1: return1, 0, 0, 1 m = a.degree() // 2 a_top, a_bot = a.quo_rem(x^m) b_top, b_bot = b.quo_rem(x^m) R00, R01, R10, R11 = HGCD(a_top, b_top) c = R00 * a + R01 * b d = R10 * a + R11 * b q, e = c.quo_rem(d) d_top, d_bot = d.quo_rem(x^(m // 2)) e_top, e_bot = e.quo_rem(x^(m // 2)) S00, S01, S10, S11 = HGCD(d_top, e_top) RET00 = S01 * R00 + (S00 - q * S01) * R10 RET01 = S01 * R01 + (S00 - q * S01) * R11 RET10 = S11 * R00 + (S10 - q * S11) * R10 RET11 = S11 * R01 + (S10 - q * S11) * R11 return RET00, RET01, RET10, RET11 defGCD(a, b): print(a.degree(), b.degree()) q, r = a.quo_rem(b) if r == 0: return b R00, R01, R10, R11 = HGCD(a, b) c = R00 * a + R01 * b d = R10 * a + R11 * b if d == 0: return c.monic() q, r = c.quo_rem(d) if r == 0: return d return GCD(d, r)
key = 3168111950 PR.<x> = PolynomialRing(Zmod(n)) f1 = sum((k-key)*x**i for i, k inenumerate(final))^((1<<7)-1) - c1 f2 = x^((1<<16)+1) - c2 f2 = f2 % f1 res = GCD(f1,f2)
m = -res.monic().coefficients()[0] flag = long_to_bytes(int(m)) print(flag) # XYCTF{ba76e13f-269e-4481-baf0-4a50ad17f891}
defcomplex_pow(c, exp, n): result = Complex(1, 0) while exp > 0: if exp & 1: result = result * c result.re = result.re % n result.im = result.im % n c = c * c c.re = c.re % n c.im = c.im % n exp >>= 1 return result
m = bytes_to_long(flag) key = getRandomNBitInteger(m.bit_length()) c = m ^ key com = Complex(key, c) p = getPrime(512) q = getPrime(512) e = 9 enc = complex_pow(com, e, p * q) print(enc) print(Complex(p, q) * Complex(p, q)) # 66350931528185981323649477263900844564494528747802437244889229343520648562164950914433406604580038018765783183569276743239888668912948977370163046257917321742455772852779551569446155827368453262479370103326286297164105599131090881306108546341785251895116423206455175290083968296281375908109039893280371271943 + 65266730684129269806656018828265187384002656633231286337130178390517924611751697965395744944541329793503617856896706439809241745206839328124348693486741656130890593895436661857688522977866438805549144904296596887218275440542852887148071837153436265722614658566275517205322945316112048487893204059562830581004i # -28814875173103880290298835537218644402443395484370652510062722255203946330565951328874411874019897676900075613671629765922970689802650462822117767927082712245512492082864958877932682404829188622269636302484189627580600076246836248427780151681898051243380477561480415972565859837597822263289141887833338111120 + 235362412848885579543400940934854106052672292040465052424316433330114813432317923674803623227280862945857543620663672974955235166884830751834386990766053503640556408758413592161122945636548462064584183165189050320898315823173824074873376450569212651128285746330837777597290934043912373820690250920839961482862i
defcomplex_pow(c, exp, n): result = Complex(1, 0) while exp > 0: if exp & 1: result = result * c result.re = result.re % n result.im = result.im % n c = c * c c.re = c.re % n c.im = c.im % n exp >>= 1 return result
K = [] for i in res_ap: for j in res_aq: k = CRT_list([int(i[0]),int(j[0])],[p,q]) if k notin K: K.append(k)
enc = [] for i in res_bp: for j in res_bq: c = CRT_list([int(i[0]),int(j[0])],[p,q]) if c notin enc: enc.append(c)
for key in K: for c in enc: m = key ^^ c flag = long_to_bytes(int(m)) ifb"flag"in flag orb"XYCTF"in flag: print(flag) break # flag{Complex_is_so_fun_and_I_think_you_know_Sylvester!}
from Crypto.Util.number import * from hashlib import sha256
K = 17329427219955161804703200105884322768704934833367341 J = 1365855822212045061018261334821659180641576788523935479 p = 1365855822212045061018261334821659180641576788523935481 G = (1060114032187482137663886206406014543797784561116139791,752764811411303365258802649951280929945966659818544966) C = (695174082657148306737473938393010922439779304870471540,414626357054958506867453055549756701310099524292082869)
A = ((3 - J^2)*inverse(3*K^2,p)) % p B = ((2*J^3 - 9*J)*inverse(27*K^3,p)) % p
defto_weier(x,y): xW = (3*x+J) * inverse_mod(3*K, p) % p yW = y * inverse_mod(K, p) % p assert yW^2 % p == (xW^3+A*xW+B) % p return (xW,yW)
GG = to_weier(G[0],G[1]) CC = to_weier(C[0],C[1])
r1 = 544257272453066077953551360625531658485543339865638914 r2 = 277341277305912905111158613570595863670490108792657653 tG = (GG[0] + r1) % p tC = (CC[0] + r1) % p
r = 1098939827064891888175868587766723385826523557450954220 # R.<alpha> = PolynomialRing(Zmod(p)) # f = alpha^2 - r # print(f.roots()) alpha = 1003930426227501665054311651540310392036896175643136673 # alpha = 361925395984543395963949683281348788604680612880798808
g = ((GG[1] + alpha*tG) * inverse((GG[1]-alpha*tG),p)) % p y = ((CC[1] + alpha*tC) * inverse((CC[1]-alpha*tC),p)) % p
length = len(c) for i inrange(length): c[i] = c[i] << 120
A = [1] B = [0]
for i inrange(1,length-1): Ai = a*A[i-1] % m Bi = (a*B[i-1] + a*c[i]+b-c[i+1]) % m A.append(Ai) B.append(Bi)
A = A[1:] B = B[1:]
M = Matrix(length,length)
for i inrange(length-2): M[i,i] = m M[-1,i] = B[i] M[-2,i] = A[i] M[-1,-1] = 2^120 M[-2,-2] = 1 inv = gmpy2.invert(a,m) for i in M.LLL(): if i[-1] == 2^120: L1 = i[-2] seed1 = c[1] + L1 seed = inv * (seed1 - b) % m if ((a * seed + b) % m) >> 120 == c[1] >> 120: for _ inrange(30): seed = (a * seed + b) % m key = "" for _ inrange(40): seed = (a * seed % m + b) % m temp = seed >> 120 key += str(temp) msg = cipher ^^ int(key) flag = long_to_bytes(int(msg)) ifb"flag"in flag orb"XYCTF"in flag: print(flag) break # XYCTF{2h1_21_ZHi_5h0u_yU_zI_xI3_l@0}
F = [f1,f2,f3,f4,f5,f6] I = Ideal(F).groebner_basis() p = ZZ(I[3]) print(f"p = {p}")
PR.<a,b,Bx> = PolynomialRing(ZZ) F = [] for i inrange(1,10-1): f = (Bx-c2[i])^2*(c2[i-1]+c2[i+1]) - 2*(Bx^3+a*Bx+b+c2[i]^3+a*c2[i]+b) + 2*(Bx+c2[i])*(Bx-c2[i])^2 F.append(f) res = Ideal(F).groebner_basis() q = ZZ(res[3].univariate_polynomial()(0)) print(f"q = {q}")
r = n // p // q phi = (p-1)*(q-1)*(r-1) d = gmpy2.invert(e,phi) m = pow(c,d,n) print(long_to_bytes(int(m))) # flag{71fc0bc1-167a-43f0-ab0f-e0df3bea8ed3}