from Crypto.Util.number import * from gmpy2 import * from Crypto.PublicKey import RSA from base64 import * from factordb.factordb import FactorDB public_key=RSA.importKey(open("./public.pem",'rb').read())
E=public_key.e N=public_key.n print(E) print(N)
f=FactorDB(N) f.connect() p,q=f.get_factor_list() c=[] for i inrange(4): c.append(bytes_to_long(b64decode((open("flag.enc{}".format(i+1),'rb').read())))) for i in c: print(long_to_bytes(pow(i,invert(E,(p-1)*(q-1)),N))) """b'\x02\x88b4Kt\xe4\x1cI\xcf\x8fw/"\x00666c61677b495345432d49' b'\x02\x1b\xf3\x18\xaf\xe1\xe3\xc7\x19\xa0\x05\xac\xe0N\x007235574d5f473441666276' b'\x025\x89;\xb0\xbd\x96\tU@\xf2\x0b\xe7+\x00785f6d534d5f556766387a' b'\x02\xfcu\xcb\n\xcf\x97\x0f\x9e\xbbC\x11\xd7\xdbw\xd9\x0052416f4d6b594350787d'""" a=0x666c61677b495345432d497235574d5f473441666276785f6d534d5f556766387a52416f4d6b594350787d print(long_to_bytes(a)) #b'flag{ISEC-Ir5WM_G4Afbvx_mSM_Ugf8zRAoMkYCPx}' #NSSCTF{ISEC-Ir5WM_G4Afbvx_mSM_Ugf8zRAoMkYCPx}
from Crypto.Util.number import * from gmpy2 import * from factordb.factordb import FactorDB n = 3454083680130687060405946528826790951695785465926614724373 f=FactorDB(n) f.connect() p,q,r=f.get_factor_list() e = 3 c = 1347530713288996422676156069761604101177635382955634367208
from Crypto.Util.number import * from flag import *
n1 = 103835296409081751860770535514746586815395898427260334325680313648369132661057840680823295512236948953370895568419721331170834557812541468309298819497267746892814583806423027167382825479157951365823085639078738847647634406841331307035593810712914545347201619004253602692127370265833092082543067153606828049061 n2 = 115383198584677147487556014336448310721853841168758012445634182814180314480501828927160071015197089456042472185850893847370481817325868824076245290735749717384769661698895000176441497242371873981353689607711146852891551491168528799814311992471449640014501858763495472267168224015665906627382490565507927272073 e = 65537 m = bytes_to_long(flag) c = pow(m, e, n1) c = pow(c, e, n2)
print("c = %d" % c)
# output # c = 60406168302768860804211220055708551816238816061772464557956985699400782163597251861675967909246187833328847989530950308053492202064477410641014045601986036822451416365957817685047102703301347664879870026582087365822433436251615243854347490600004857861059245403674349457345319269266645006969222744554974358264
"""from Crypto.Util.number import * from flag import *
n1 = 103835296409081751860770535514746586815395898427260334325680313648369132661057840680823295512236948953370895568419721331170834557812541468309298819497267746892814583806423027167382825479157951365823085639078738847647634406841331307035593810712914545347201619004253602692127370265833092082543067153606828049061 n2 = 115383198584677147487556014336448310721853841168758012445634182814180314480501828927160071015197089456042472185850893847370481817325868824076245290735749717384769661698895000176441497242371873981353689607711146852891551491168528799814311992471449640014501858763495472267168224015665906627382490565507927272073 e = 65537 m = bytes_to_long(flag) c = pow(m, e, n1) c = pow(c, e, n2)
print("c = %d" % c)
# output # c = 60406168302768860804211220055708551816238816061772464557956985699400782163597251861675967909246187833328847989530950308053492202064477410641014045601986036822451416365957817685047102703301347664879870026582087365822433436251615243854347490600004857861059245403674349457345319269266645006969222744554974358264 """ from Crypto.Util.number import * from gmpy2 import * n1 = 103835296409081751860770535514746586815395898427260334325680313648369132661057840680823295512236948953370895568419721331170834557812541468309298819497267746892814583806423027167382825479157951365823085639078738847647634406841331307035593810712914545347201619004253602692127370265833092082543067153606828049061 n2 = 115383198584677147487556014336448310721853841168758012445634182814180314480501828927160071015197089456042472185850893847370481817325868824076245290735749717384769661698895000176441497242371873981353689607711146852891551491168528799814311992471449640014501858763495472267168224015665906627382490565507927272073 e = 65537 c = 60406168302768860804211220055708551816238816061772464557956985699400782163597251861675967909246187833328847989530950308053492202064477410641014045601986036822451416365957817685047102703301347664879870026582087365822433436251615243854347490600004857861059245403674349457345319269266645006969222744554974358264 p=gcd(n1,n2)
if p != 1and isPrime(p): c1=pow(c,invert(e,(p-1)*(n2//p-1)),n2) print(long_to_bytes(pow(c1,invert(e,(p-1)*(n1//p-1)),n1))) #b'SangFor{qSccmm1WrgvIg2Uq_cZhmqNfEGTz2GV8}' #NSSCTF{qSccmm1WrgvIg2Uq_cZhmqNfEGTz2GV8}
from Crypto.Util.number import * from gmpy2 import * n1=15228664629164509105936278301396170708905691970126305196584505186788860519598413718493859625462561931380632032431490419378905593909771649295663481782473029836321132574188559245931660756414915507930357509270674460219615256962333464689419869130366867401404262606367700782040693275068101244535880649261286041921882470460606034302142183971677715439862839410834231609821777031530457674591868138859358815039755085358568037032478394036448363183057305077227769673701227083943898736796552550712057417053897722979700329662099072959306298177351997084389916443815546425080826441671985030755256185725913397986385179516049927425591 n2=28182418532443955655250943929828439725377604572088962537896240628709829618999901367131159759359513146864646169253348651905865895468151210748207509325666501438590382812326109260537618829438786609626137074778638549998280533912080708785604673270460635181275360847313985764185991865570533815651261638439461846512012164531330949433517277559149828806588070421852157781670188281908625986974579194819272643409859915715455134433970119584552350648013116998668938513347083566970423327936691885137812528912263666957628197241313496232397910546498542303925205356813548741679943691886217742767778075067797422624969714343428365022749 n3=18355811159408154065817199279776805621878757240392366715869421799780946779485225342662736231980532326015283372375030686507311099745671828649419794838611580909610100636296701054995302819692794479292794716441442731393027118795245239019609474743841061251498233337758043553376098591254587406941205804917663153256036922860462415387926973551020540123742773938055950168965005226319984869124543783579240130888344231027912143592472823564266887957101575622993773291455143915263715932280728961208233983782906070719786115187115449430196335973764600533097718947377609348244073036523422892353195107093782201003551217830556519184839 e1=65537 e2=27751 e3=65537 c1=5368342382489380107251269030258282008067103595899117880173297169710980852124379736420135829984131832023988667774795223808420069001078159756328642298736759964890517323144475742861501409284299556459601222657540302786301791897975932176538612601162552795835603779910738886150925504885639254302406755008796950704938463132687940418772021406619622090999564746948113296328739593309200238996686945891130656599419832796482095787039339269564880847130379179831744694000940207887150388411084465949903406848727641093033681144598595895383689139227400553234701993087147186292040330589331703587405822925483701667354935313494938769206 c2=21521672635651854919517759696514027081496995002884626306313384597771682621826437868933822942195279941318573525337109548152966094293276717095298929811895186384560362917891928656637913236676702009205642367801075592458101830488916914437754803979953027152373619293870115731171449223105986403604973873007338969000153480949617700626516389419935352576014084068271819009465242491467427642787306345049280205827574043586767133396458785487959251540831856187380154825027964867977651727983254127239427622549059938701125498520279503972702883327594442747467858234391945790597844344295786118320620376681461727686876948563884520137741 c3=13940747781246179701167820858098775936269078279837839169409057305686612176371099274767269714494905207551971162649902129137425806839867713157472497469542260664882313041602553845621113546259276402534229231780532278276697961222319054833980226978574905974878218905613341365260453461080117407529132948986104191917111000811731784483944945364091757083949827612260904757837644538366763161154611658652020868326985526984718638276184626634240096213703958275241215175054246685206226179114590838833694648062135027841593419815101363262701960507235056752424778384286627997500871204804629047307688466887868894491042058198480775705486
p1=gcd(n1,n2) print(long_to_bytes(pow(c1,invert(e1,p1-1),p1))) print(long_to_bytes(pow(c2,invert(e2,p1-1),p1))) #b"hahaha, you've got the flag didn't you !the front part is :flag{G00d_w4" #b"hahaha, you've got the flag didn't you !the middle part is :y_tO_cR" p3=gcd(n3,n2) print(long_to_bytes(pow(c3,invert(e3,p3-1),p3))) #b"hahaha, you've got the flag didn't you !the last part is :4ck_RS4}" #flag{G00d_w4y_tO_cR4ck_RS4} #NSSCTF{G00d_w4y_tO_cR4ck_RS4}
cf = continued_fraction(n2 / n1) convers = cf.convergents() for pkd in convers: k,r3=pkd.as_integer_ratio() if gcd(k, e) != 1: continue r = inverse_mod(-k, e) phi = crt([r, 0], [e, r3]) md = e*r3 // gcd(e, r3) st = phi + (n1//md) * md #预处理一下
for i inrange(-100, 100): if gcd(e, st+i*md) != 1: continue d = inverse_mod(e, st + i*md) m = long_to_bytes(power_mod(enc1, d, n1)) ifb'flag'in m: print(phi) print(m) break #b'flag{C0ngra4u1ation!!!_You_R3a1ly_Kn0w_Lat2ic3}' #NSSCTF{C0ngra4u1ation!!!_You_R3a1ly_Kn0w_Lat2ic3}
#sage from Crypto.Util.number import * from gmpy2 import * pk = (1150398070565459492080597718626032792435556703413923483458704675295997646493249759818468321328556510074044954676615760446708253531839417036997811506222349194302791943489195718713797322878586379546657275419261647635859989280700191441312691274285176619391539387875252135478424580680264554294179123254566796890998243909286508189826458854346825493157697201495100628216832191035903848391447704849808577310612723700318670466035077202673373956324725108350230357879374234418393233, 1242678737076048096780023147702514112272319497423818488193557934695583793070332178723043194823444815153743889740338870676093799728875725651036060313223096288606947708155579060628807516053981975820338028456770109640111153719903207363617099371353910243497871090334898522942934052035102902892149792570965804205461900841595290667647854346905445201396273291648968142608158533514391348407631818144116768794595226974831093526512117505486679153727123796834305088741279455621586989) c1, c2 = (361624030197288323178211941746074961985876772079713896964822566468795093475887773853629454653096485450671233584616088768705417987527877166166213574572987732852155320225332020636386698169212072312758052524652761304795529199864805108000796457423822443871436659548626629448170698048984709740274043050729249408577243328282313593461300703078854044587993248807613713896590402657788194264718603549894361488507629356532718775278399264279359256975688280723740017979438505001819438, 33322989148902718763644384246610630825314206644879155585369541624158380990667828419255828083639294898100922608833810585530801931417726134558845725168047585271855248605561256531342703212030641555260907310067120102069499927711242804407691706542428236208695153618955781372741765233319988193384708525251620506966304554054884590718068210659709406626033891748214407992041364462525367373648910810036622684929049996166651416565651803952838857960054689875755131784246099270581394) e = 0x10001 n1=pk[0] n2=pk[1] cf = continued_fraction(n1 / n2) convers = cf.convergents() for pkd in convers: q1,q2=pkd.as_integer_ratio() if q1==0or q2==0: continue if n1 % q1 == 0and n2 % q2 == 0and is_prime(q1) and is_prime(q2): if iroot(n1//q1, 4)[1]: p1=iroot(n1//q1, 4)[0] p2=iroot(n2//q2, 4)[0] d1=invert(e,(p1**4-p1**3)*(q1-1)) d2=invert(e,(p2**4-p2**3)*(q2-1)) m1=pow(c1,d1,n1) m2=pow(c2,d2,n2) print(long_to_bytes(int(m1))+long_to_bytes(int(m2))) break #b'flag{8ef333ac-21a7-11ec-80f1-00155d83f114}' #NSSCTF{8ef333ac-21a7-11ec-80f1-00155d83f114}
[NCTF 2019]easyRSA
题目
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from flag import flag
e = 0x1337 p = 199138677823743837339927520157607820029746574557746549094921488292877226509198315016018919385259781238148402833316033634968163276198999279327827901879426429664674358844084491830543271625147280950273934405879341438429171453002453838897458102128836690385604150324972907981960626767679153125735677417397078196059 q = 112213695905472142415221444515326532320352429478341683352811183503269676555434601229013679319423878238944956830244386653674413411658696751173844443394608246716053086226910581400528167848306119179879115809778793093611381764939789057524575349501163689452810148280625226541609383166347879832134495444706697124741 n = p * q
assert(flag.startswith('NCTF')) m = int.from_bytes(flag.encode(), 'big') assert(m.bit_length() > 1337)
c = pow(m, e, n) print(c) # 10562302690541901187975815594605242014385201583329309191736952454310803387032252007244962585846519762051885640856082157060593829013572592812958261432327975138581784360302599265408134332094134880789013207382277849503344042487389850373487656200657856862096900860792273206447552132458430989534820256156021128891296387414689693952047302604774923411425863612316726417214819110981605912408620996068520823370069362751149060142640529571400977787330956486849449005402750224992048562898004309319577192693315658275912449198365737965570035264841782399978307388920681068646219895287752359564029778568376881425070363592696751183359
from Crypto.Util.number import * from gmpy2 import * import random import math
c=10562302690541901187975815594605242014385201583329309191736952454310803387032252007244962585846519762051885640856082157060593829013572592812958261432327975138581784360302599265408134332094134880789013207382277849503344042487389850373487656200657856862096900860792273206447552132458430989534820256156021128891296387414689693952047302604774923411425863612316726417214819110981605912408620996068520823370069362751149060142640529571400977787330956486849449005402750224992048562898004309319577192693315658275912449198365737965570035264841782399978307388920681068646219895287752359564029778568376881425070363592696751183359 e = 0x1337 p = 199138677823743837339927520157607820029746574557746549094921488292877226509198315016018919385259781238148402833316033634968163276198999279327827901879426429664674358844084491830543271625147280950273934405879341438429171453002453838897458102128836690385604150324972907981960626767679153125735677417397078196059 q = 112213695905472142415221444515326532320352429478341683352811183503269676555434601229013679319423878238944956830244386653674413411658696751173844443394608246716053086226910581400528167848306119179879115809778793093611381764939789057524575349501163689452810148280625226541609383166347879832134495444706697124741 n=p*q
defonemod(e, q): p = random.randint(1, q-1) while(powmod(p, (q-1)//e, q) == 1): # (r,s)=1 p = random.randint(1, q) return p
defAMM_rth(o, r, q): # r|(q-1 assert((q-1) % r == 0) p = onemod(r, q)
t = 0 s = q-1 while(s % r == 0): s = s//r t += 1 k = 1 while((s*k+1) % r != 0): k += 1 alp = (s*k+1)//r
a = powmod(p, r**(t-1)*s, q) b = powmod(o, r*a-1, q) c = powmod(p, s, q) h = 1
for i inrange(1, t-1): d = powmod(int(b), r**(t-1-i), q) if d == 1: j = 0 else: j = (-math.log(d, a)) % r b = (b*(c**(r*j))) % q h = (h*c**j) % q c = (c*r) % q result = (powmod(o, alp, q)*h) return result
defALL_Solution(m, q, rt, cq, e): mp = [] for pr in rt: r = (pr*m) % q # assert(pow(r, e, q) == cq) mp.append(r) return mp
defcalc(mp, mq, e, p, q): i = 1 j = 1 t1 = invert(q, p) t2 = invert(p, q) for mp1 in mp: for mq1 in mq: j += 1 if j % 100000 == 0: print(j) ans = (mp1*t1*q+mq1*t2*p) % (p*q) if check(ans): return return
defALL_ROOT2(r, q): # use function set() and .add() ensure that the generated elements are not repeated li = set() while(len(li) < r): p = powmod(random.randint(1, q-1), (q-1)//r, q) li.add(p) return li
cp = c % p cq = c % q
mp = AMM_rth(cp, e, p) # AMM算法得到一个解 mq = AMM_rth(cq, e, q)
defonemod(e, q): p = random.randint(1, q-1) while(powmod(p, (q-1)//e, q) == 1): # (r,s)=1 p = random.randint(1, q) return p
defAMM_rth(o, r, q): # r|(q-1 assert((q-1) % r == 0) p = onemod(r, q)
t = 0 s = q-1 while(s % r == 0): s = s//r t += 1 k = 1 while((s*k+1) % r != 0): k += 1 alp = (s*k+1)//r
a = powmod(p, r**(t-1)*s, q) b = powmod(o, r*a-1, q) c = powmod(p, s, q) h = 1
for i inrange(1, t-1): d = powmod(int(b), r**(t-1-i), q) if d == 1: j = 0 else: j = (-math.log(d, a)) % r b = (b*(c**(r*j))) % q h = (h*c**j) % q c = (c*r) % q result = (powmod(o, alp, q)*h) return result
defALL_Solution(m, q, rt, cq, e): mp = [] for pr in rt: r = (pr*m) % q # assert(pow(r, e, q) == cq) mp.append(r) return mp
defcalc(mp, mq, e, p, q): i = 1 j = 1 t1 = invert(q, p) t2 = invert(p, q) for mp1 in mp: for mq1 in mq: j += 1 if j % 100000 == 0: print(j) ans = (mp1*t1*q+mq1*t2*p) % (p*q) if check(ans): return return
defALL_ROOT2(r, q): # use function set() and .add() ensure that the generated elements are not repeated li = set() while(len(li) < r): p = powmod(random.randint(1, q-1), (q-1)//r, q) li.add(p) return li
cp = c % p cq = c % q
mp = AMM_rth(cp, e, p) # AMM算法得到一个解 mq = AMM_rth(cq, e, q)
a=calc(amp, amq, e, p, q) # 俩俩CRT #2900000 #b'NSSCTF{2he_amm_13_r12lly_hard_f0r_me}yhe92871899hihiohh97709ujojl;lhdiwoqu903YE98Y299HDY8W9EYRW8!$$%!$!$FSR@#$@%FSEGDRYERYHRWER@$%^$^DGTW%$^&GRWR@$%@FASFSFQFSTGW#TWGARWQ$@%WGVDSGADQR@%TGVDSFASDATWEGHWE%@$GSDVSFQATY$^#^%@$!RAFSDGDRTW'
[长安杯 2021]checkin
题目
1 2 3 4 5 6 7 8 9 10 11 12 13
from Crypto.Util.number import * from secret import flag p = getPrime(1024) q = getPrime(16) n = p*q m = bytes_to_long(flag) for i inrange(1,p-q): m = m*i%n e = 1049 print(pow(2,e,n)) print(pow(m,e,n)) #4513855932190587780512692251070948513905472536079140708186519998265613363916408288602023081671609336332823271976169443708346965729874135535872958782973382975364993581165018591335971709648749814573285241290480406050308656233944927823668976933579733318618949138978777831374262042028072274386196484449175052332019377 #3303523331971096467930886326777599963627226774247658707743111351666869650815726173155008595010291772118253071226982001526457616278548388482820628617705073304972902604395335278436888382882457685710065067829657299760804647364231959804889954665450340608878490911738748836150745677968305248021749608323124958372559270
from Crypto.Util.number import * c2=4513855932190587780512692251070948513905472536079140708186519998265613363916408288602023081671609336332823271976169443708346965729874135535872958782973382975364993581165018591335971709648749814573285241290480406050308656233944927823668976933579733318618949138978777831374262042028072274386196484449175052332019377 c1=3303523331971096467930886326777599963627226774247658707743111351666869650815726173155008595010291772118253071226982001526457616278548388482820628617705073304972902604395335278436888382882457685710065067829657299760804647364231959804889954665450340608878490911738748836150745677968305248021749608323124958372559270 e=1049 kn=2**1049-c2 """for i in range(1<<15,1<<16): if isPrime(i) and kn%i==0: q=i print(q) print(kn//q) break #34211 #176187289150514462046172630174700088525221325866312783639426750020851677271339945780081598171833449310803326466412109196489585032131454474980855474525268996498728189708770015967359856986040467410546755834813513311041394212977692161618099683277808044775858602607121850124014360450965062461065243304016148002474085 """ q=34211 p=170229264879724117919007372149468684565431232721075153274808454126426741324966131188484635914814926870341378228417496808202497615585946352638507704855332363766887139815236730403246238633855524068161116748612090155595549964229654262432946553891601975628848891407847198187453488358420350203927771308228162321231
assertpow(2,e,p*q)==c2
m0=pow(c1,invert(e,(p-1)*(q-1)),p*q) for i inrange(p-q,p): m0=(m0*i)%p print(long_to_bytes((int(m0)*invert(-1,p))%p)) #b"flag{7h3_73rr1b13_7h1ng_15_7h47_7h3_p457_c4n'7_b3_70rn_0u7_by_175_r0075}" #NSSCTF{7h3_73rr1b13_7h1ng_15_7h47_7h3_p457_c4n'7_b3_70rn_0u7_by_175_r0075}
from gmpy2 import * from Crypto.Util.number import *
c = 182812482972168423884795132699225934365072979206288632257180603530046820174392675977209758378734399146216742345585898385168866887000708558119959898992294085847474548306743585711154035585848291290988967352517174312220756638881837930962458861193652684492265539096477345065113556380573776423787885892688197584678128636231428194711357642971544417113415626331810909274966752557628893585198569815939514862013512237657828262360291726912615575646318630641527418369988268899879152029186728850816178597399494254385226049249357897840618728804680238123954207656671747782543031545429711152272581734051959578453680011676521727918037340906791388178004979453256050227967701258768070039292546964652071924183467364467145178290753361477912582242961929982420950384199259355122986865808523351306098081481072454093823090 n = 438980397031315392229453908048509540832246041631432878509579665664182747463100230160823865621798053164989325086085003940181731721089701380743698761443812523024144817205902380903062054138730658451286904347536210833160924917347633148983052015550354913154312162901555870494273903714349869746793861874257201085777893961715468950661641778512110325457371446203379767458862059193946434683324578530163650541637261158037041205642428802942295011562277084687025213626698849526240663754073508102229066475773893638716845176469070938803298515155140240970836387785401085919369741520890271902332951669953411373633688944162470994856654604872287103746922041844065053274059990595496159866206551119361036237431289830985174384522423364811997241255005514248198447925396378192915553898993758660041223393168707380580012437 e = 0x10001
pqh = n // pow(10, 600) pql = n % pow(10, 200) pq = pqh*pow(10, 200) + pql
n -= pow(10, 400)*pq + pq
from z3 import * h=Int('h') l=Int('l') s=Solver() s.add(h*l==pq) s.add(pow(10, 200)*h*h+pow(10, 200)*l*l==n) s.check() s=s.model() h=s[s.decls()[0]].as_long() l=s[s.decls()[1]].as_long() p = h*pow(10, 200) + l q = l*pow(10, 200) + h
from gmpy2 import * from Crypto.Util.number import * from itertools import product from string import digits from factordb.factordb import FactorDB n = 177269125756508652546242326065138402971542751112423326033880862868822164234452280738170245589798474033047460920552550018968571267978283756742722231922451193 c = 47718022601324543399078395957095083753201631332808949406927091589044837556469300807728484035581447960954603540348152501053100067139486887367207461593404096 e = 65537
''' c = 86415476382906786465939442398992406348852252355326334785583474583480585659663836032856765037225261433532613020730103955916772373674295180495452293421279237222544308971840820110279355118064931506637793547489441433938707518241461449059717326341918746156620038847745542794560335988858156929013492541794032580255 e = 65537 n = 166337085427556441543394334802135957169988266794453522153008810336368247289697353242192853337017363111987395194428553050681210209730724596529629525357502302165396675392000087988956194589350195512264427901330860811469484473725396354231555692283910488095918243519370430703255279433498479943391876108577325840381 x = 2509898544460604898497702985357222191225421344430742181152035006910161802193623236888758239071502008180363546424715261788 '''
c = 86415476382906786465939442398992406348852252355326334785583474583480585659663836032856765037225261433532613020730103955916772373674295180495452293421279237222544308971840820110279355118064931506637793547489441433938707518241461449059717326341918746156620038847745542794560335988858156929013492541794032580255 e = 65537 n = 166337085427556441543394334802135957169988266794453522153008810336368247289697353242192853337017363111987395194428553050681210209730724596529629525357502302165396675392000087988956194589350195512264427901330860811469484473725396354231555692283910488095918243519370430703255279433498479943391876108577325840381 x = 2509898544460604898497702985357222191225421344430742181152035006910161802193623236888758239071502008180363546424715261788
plist = [0] qlist = [0]
mask = 0 for i inrange(400): tmpp, tmpq = [],[] for p,q in [(0,0),(0,1),(1,0),(1,1)]: if p^^q == ((x>>mask)&1): for j inrange(len(plist)): if ((p<<mask) + plist[j]) * ((q<<mask) + qlist[j]) % (2<<mask) == n % (2<<mask): tmpp.append((p<<mask) + plist[j]) tmpq.append((q<<mask) + qlist[j]) plist,qlist = tmpp, tmpq mask += 1
for pbar in plist: PR = PolynomialRing(Zmod(n), 'x') x = PR('x') ZmodN = Zmod(n) f = x*ZmodN(2**400) + pbar f = f.monic() r = f.small_roots(X=2**112, beta=0.4) if r: p = int(pbar+r[0]* 2**400) if n%p == 0: break
q = n // p d = inverse_mod(e, (p-1)*(q-1)) print(long_to_bytes(power_mod(c, d, n))) #b'flag{59sm5gzl-8wbm-v2lj-w7m2-4op2lyr8wjz8}' #NSSCTF{59sm5gzl-8wbm-v2lj-w7m2-4op2lyr8wjz8}
c3=59213696442373765895948702611659756779813897653022080905635545636905434038306468935283962686059037461940227618715695875589055593696352594630107082714757036815875497138523738695066811985036315624927897081153190329636864005133757096991035607918106529151451834369442313673849563635248465014289409374291381429646 n3=113432930155033263769270712825121761080813952100666693606866355917116416984149165507231925180593860836255402950358327422447359200689537217528547623691586008952619063846801829802637448874451228957635707553980210685985215887107300416969549087293746310593988908287181025770739538992559714587375763131132963783147 high_p=7117286695925472918001071846973900342640107770214858928188419765628151478620236042882657992902 unknown_bits=200 high_p=high_p<<unknown_bits R.<x> = PolynomialRing(Zmod(n3)) f = high_p + x x = f.small_roots(X = 2^unknown_bits,beta = 0.4) p=int(high_p+x[0]) msg3=long_to_bytes(int(pow(c3,invert(e3,(p-1)*(n3//p-1)),n3)))
text=msg1+msg2+msg3 print(text) print(md5(text).hexdigest()) #b" \nO wild West Wind, thou breath of Autumn's being,\nThou, from whose unseen presence the leaves dead\nAre driven, like ghosts from an enchanter fleeing,\nYellow, and black, and pale, and hectic red,\nPestilence-stricken multitudes: O thou,\nWho chariotest to their dark wintry bed\n" #3943e8843a19149497956901e5d98639 #NSSCTF{3943e8843a19149497956901e5d98639}
import gmpy2 from Crypto.Util.number import * import random from secret import flag
m1 = flag[0:12] m2 = flag[12:24] m3 = flag[24:]
defencrypt1(m): while1: e = random.randint(100, 1000) p = getPrime(1024) q = getPrime(1024) phi_n = (p - 1) * (q - 1) t = gmpy2.gcd(e, phi_n) if gmpy2.invert(e // t, phi_n) and t != 1: break n = p * q c = pow(m, e, n) print({'c': format(c, 'x'), 'p': format(p, 'x'), 'q': format(q, 'x'), 'e': format(e, 'x')})
defencrypt2(m): p = getPrime(1024) q = getPrime(1024) n = p * q e = 65537 c = gmpy2.powmod(m, e, n) print({'c': format(c, 'x'), 'p': format((p >> 60) << 60, 'x'), 'e': format(e, 'x'), 'n': format(n, 'x')})
defencrypt3(m): p = getPrime(1024) q = getPrime(1024) n = p * q e = 65537 M = 2022 * m * 1011 * p c = pow(M, e, n) print({'c': format(c, 'x'), 'n': format(n, 'x'),'e':format(e, 'x')})
if __name__ == '__main__': encrypt1(bytes_to_long(m1)) encrypt2(bytes_to_long(m2)) encrypt3(bytes_to_long(m3))
from flag import flag defgen(): e = 3 whileTrue: try: p = getPrime(512) q = getPrime(512) n = p*q phi = (p-1)*(q-1) d = inverse(e,phi) return p,q,d,n,e except: continue return p,q,d,n,e = gen() r = getPrime(512) m = bytes_to_long(flag+os.urandom(32)) M = m%r c = pow(m,e,n) print("r = %d"%r) print("M = %d"%M) print("n = %d"%n) print("e = %d"%e) print("c = %d"%c) ''' r = 7996728164495259362822258548434922741290100998149465194487628664864256950051236186227986990712837371289585870678059397413537714250530572338774305952904473 M = 4159518144549137412048572485195536187606187833861349516326031843059872501654790226936115271091120509781872925030241137272462161485445491493686121954785558 n = 131552964273731742744001439326470035414270864348139594004117959631286500198956302913377947920677525319260242121507196043323292374736595943942956194902814842206268870941485429339132421676367167621812260482624743821671183297023718573293452354284932348802548838847981916748951828826237112194142035380559020560287 e = 3 c = 46794664006708417132147941918719938365671485176293172014575392203162005813544444720181151046818648417346292288656741056411780813044749520725718927535262618317679844671500204720286218754536643881483749892207516758305694529993542296670281548111692443639662220578293714396224325591697834572209746048616144307282 '''
#sage from gmpy2 import * from Crypto.Util.number import * r = 7996728164495259362822258548434922741290100998149465194487628664864256950051236186227986990712837371289585870678059397413537714250530572338774305952904473 M = 4159518144549137412048572485195536187606187833861349516326031843059872501654790226936115271091120509781872925030241137272462161485445491493686121954785558 n = 131552964273731742744001439326470035414270864348139594004117959631286500198956302913377947920677525319260242121507196043323292374736595943942956194902814842206268870941485429339132421676367167621812260482624743821671183297023718573293452354284932348802548838847981916748951828826237112194142035380559020560287 e = 3 c = 46794664006708417132147941918719938365671485176293172014575392203162005813544444720181151046818648417346292288656741056411780813044749520725718927535262618317679844671500204720286218754536643881483749892207516758305694529993542296670281548111692443639662220578293714396224325591697834572209746048616144307282
PR.<x> = PolynomialRing(Zmod(n)) f = (M+x*r)^e-c f = f.monic() x = f.small_roots() if x: print(x) m=int(M+x[0]*r) print(long_to_bytes(m)) #[810968823598060539864535] #b'flag{53a2e494-964d-4506-a2c4-c34b9475dedd}W\xf1X6\xacP\x9bc~9\xfd\x0f\x96\xbf\x92\xb9+\xe5\xebPJ\x17\xc4\xb2\xe8\xad\x01\n\xf2j\xae\x15' #NSSCTF{53a2e494-964d-4506-a2c4-c34b9475dedd}
from pwn import * from Crypto.Util.number import * import hashlib p=remote("node4.anna.nssctf.cn",28421) print(p.recvline()) p.recvuntil(b'hexdigest()=') sha=(p.recvline().decode()).strip() print(sha.encode()) p.recvuntil(b"skr[0:5].encode('hex')=") pre_skr=(p.recvline().decode()).strip() print(pre_skr.encode()) print(p.recvuntil(b"skr.encode('hex')=")) temp="0123456789abcdef" fl=0 for i in temp: for j in temp: for k in temp: for l in temp: for m in temp: for n in temp: skr=int(pre_skr+i+j+k+l+m+n,16) skr=long_to_bytes(skr) ifstr(hashlib.sha256(skr).hexdigest()) == sha: print(skr.hex()) p.sendline(str(skr.hex()).encode()) fl=1 break if fl: break if fl: break if fl: break if fl: break if fl: break p.interactive()
第二步
m的高位攻击
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#sage from Crypto.Util.number import long_to_bytes
R.<x> = PolynomialRing(Zmod(n)) m = m_high + x f = m^e - c x = f.small_roots(X = 2^unknown_bits,beta = 0.4) if x: m = m_high + x[0] print(long_to_bytes(int(m)).hex()) #4e53534354467b35643366363566366163303966613064303162383838626336643335616234347d
第三步
p高位攻击
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#sage from Crypto.Util.number import * from gmpy2 import * n=23412744196435681035016886220643400350970797545501310013304272571425348967576484268142400640552976798425045608274973517284025831498467236341911890721102753724816097043557304340791745423642403094886324667586890743441181879275819905523857289371181975206153946416492759491324890990771260876302417738635652747674766470906230972589393979314947350143584541009374774851236081699644040090877016127125997472439169352089823014941009458298353952133084702487229994974065020835757669357061533853531708873378901690969236981895123027779256185848480947387458247076558510061941953938352870386545064169169200268143031918794825270298631 e=65537 c=9398872900480694514053766636015808474916722324451384224067138680826665766000870773107410167898882406270257015221891892085001459713583625341362117117871802743137635415088841771727308157301268300094318585426138274238719361978001009950762135079902183427948096280119330118607820781873262983642485251440958841901313115939514261290900440121234463088422361021316299407329969835177481638764778578537907012916110360538977789500099230565812930941982075719936200613719978050626787999136833805067940187341360786050332115552930420848777154904971863171308444262963588775160727678158622484428491558915905860791139989357158989913050 high_p=136232996027545975004563119617198627048065138315684770279122430141624103589614249447456579821863618044683199820893874027883593009896002870850519475109346672955929930383822711173403482003043608577119957883570354552011936964395584718847162648784196596548570670949314203035203058272377061576666254342055958413312 unknown_bits=128 R.<x> = PolynomialRing(Zmod(n)) f = high_p + x x = f.small_roots(X = 2^unknown_bits,beta = 0.4) if x: p=int(high_p+x[0]) q=n//p phi=(p-1)*(q-1) d=invert(e,phi) print(long_to_bytes(int(pow(c,d,n))).hex()) #4e53534354467b32393537633039333964393165633765633331306462356630616130303637627d
#sage from Crypto.Util.number import * import gmpy2 defgetFullP(low_p, n): R.<x> = PolynomialRing(Zmod(n), implementation='NTL') p = x * 2 ^ bit + low_p root = (p - n).monic().small_roots(X=2 ^ 128, beta=0.4) if root: return p(root[0]) returnNone
defphase4(low_d,n,c,e): maybe_p = [] for k inrange(1, 4): p = var('p') p0 = solve_mod([3 * p * low_d == p + k * (n * p - p ^ 2 - n + p)], 2 ^ bit) maybe_p += [int(x[0]) for x in p0] # print(maybe_p)
for x in maybe_p: P = getFullP(x, n) if P: break
P = int(P) Q = n // P
assert P * Q == n print("P = ",P) print("Q = ",Q)
d = inverse_mod(3, (P - 1) * (Q - 1)) m = pow(c,d,n) print(long_to_bytes(int(m)).hex())
low_d = 9973904680412152570138003136466059609611257944928836300634423080561298749075958309067426603113525684100417494741123226214436236190592552869975926519409611 bit = low_d.nbits() n = 114977856718774899746007273258142027119221291891045313370664580956945773892016230637680441795343679791416907107771431518072494779758596070931073345264743927090857036312767418019303974408803896285561003981790474474879254339898650912349447554900342592699419248042230565484734565275290033575309907770959979249441 e = 3 c = 279116358436840580443869403482689523417926943370283952754967172903129298280404573580346506496192282092683784627132866212050801891387341516024550585731142831756181412863049450540988286305848774162564019517973339661098414905264661903203165067354388671494408807585235845183100544469908026469
""" Setting debug to true will display more informations about the lattice, the bounds, the vectors... """ debug = True
""" Setting strict to true will stop the algorithm (and return (-1, -1)) if we don't have a correct upperbound on the determinant. Note that this doesn't necesseraly mean that no solutions will be found since the theoretical upperbound is usualy far away from actual results. That is why you should probably use `strict = False` """ strict = False
""" This is experimental, but has provided remarkable results so far. It tries to reduce the lattice as much as it can while keeping its efficiency. I see no reason not to use this option, but if things don't work, you should try disabling it """ helpful_only = True dimension_min = 7# stop removing if lattice reaches that dimension
# display stats on helpful vectors defhelpful_vectors(BB, modulus): nothelpful = 0 for ii inrange(BB.dimensions()[0]): if BB[ii,ii] >= modulus: nothelpful += 1
print(nothelpful, "/", BB.dimensions()[0], " vectors are not helpful")
# display matrix picture with 0 and X defmatrix_overview(BB, bound): for ii inrange(BB.dimensions()[0]): a = ('%02d ' % ii) for jj inrange(BB.dimensions()[1]): a += '0'if BB[ii,jj] == 0else'X' if BB.dimensions()[0] < 60: a += ' ' if BB[ii, ii] >= bound: a += '~' print(a)
# tries to remove unhelpful vectors # we start at current = n-1 (last vector) defremove_unhelpful(BB, monomials, bound, current): # end of our recursive function if current == -1or BB.dimensions()[0] <= dimension_min: return BB
# we start by checking from the end for ii inrange(current, -1, -1): # if it is unhelpful: if BB[ii, ii] >= bound: affected_vectors = 0 affected_vector_index = 0 # let's check if it affects other vectors for jj inrange(ii + 1, BB.dimensions()[0]): # if another vector is affected: # we increase the count if BB[jj, ii] != 0: affected_vectors += 1 affected_vector_index = jj
# level:0 # if no other vectors end up affected # we remove it if affected_vectors == 0: print("* removing unhelpful vector", ii) BB = BB.delete_columns([ii]) BB = BB.delete_rows([ii]) monomials.pop(ii) BB = remove_unhelpful(BB, monomials, bound, ii-1) return BB
# level:1 # if just one was affected we check # if it is affecting someone else elif affected_vectors == 1: affected_deeper = True for kk inrange(affected_vector_index + 1, BB.dimensions()[0]): # if it is affecting even one vector # we give up on this one if BB[kk, affected_vector_index] != 0: affected_deeper = False # remove both it if no other vector was affected and # this helpful vector is not helpful enough # compared to our unhelpful one if affected_deeper andabs(bound - BB[affected_vector_index, affected_vector_index]) < abs(bound - BB[ii, ii]): print("* removing unhelpful vectors", ii, "and", affected_vector_index) BB = BB.delete_columns([affected_vector_index, ii]) BB = BB.delete_rows([affected_vector_index, ii]) monomials.pop(affected_vector_index) monomials.pop(ii) BB = remove_unhelpful(BB, monomials, bound, ii-1) return BB # nothing happened return BB
""" Returns: * 0,0 if it fails * -1,-1 if `strict=true`, and determinant doesn't bound * x0,y0 the solutions of `pol` """ defboneh_durfee(pol, modulus, mm, tt, XX, YY): """ Boneh and Durfee revisited by Herrmann and May finds a solution if: * d < N^delta * |x| < e^delta * |y| < e^0.5 whenever delta < 1 - sqrt(2)/2 ~ 0.292 """
# x-shifts gg = [] for kk inrange(mm + 1): for ii inrange(mm - kk + 1): xshift = x^ii * modulus^(mm - kk) * polZ(u, x, y)^kk gg.append(xshift) gg.sort()
# x-shifts list of monomials monomials = [] for polynomial in gg: for monomial in polynomial.monomials(): if monomial notin monomials: monomials.append(monomial) monomials.sort()
# y-shifts (selected by Herrman and May) for jj inrange(1, tt + 1): for kk inrange(mm//tt * jj, mm + 1): yshift = y^jj * polZ(u, x, y)^kk * modulus^(mm - kk) yshift = Q(yshift).lift() gg.append(yshift) # substitution
# y-shifts list of monomials for jj inrange(1, tt + 1): for kk inrange(mm//tt * jj, mm + 1): monomials.append(u^kk * y^jj)
# construct lattice B nn = len(monomials) BB = Matrix(ZZ, nn) for ii inrange(nn): BB[ii, 0] = gg[ii](0, 0, 0) for jj inrange(1, ii + 1): if monomials[jj] in gg[ii].monomials(): BB[ii, jj] = gg[ii].monomial_coefficient(monomials[jj]) * monomials[jj](UU,XX,YY)
# Prototype to reduce the lattice if helpful_only: # automatically remove BB = remove_unhelpful(BB, monomials, modulus^mm, nn-1) # reset dimension nn = BB.dimensions()[0] if nn == 0: print("failure") return0,0
# check if vectors are helpful if debug: helpful_vectors(BB, modulus^mm)
# check if determinant is correctly bounded det = BB.det() bound = modulus^(mm*nn) if det >= bound: print("We do not have det < bound. Solutions might not be found.") print("Try with highers m and t.") if debug: diff = (log(det) - log(bound)) / log(2) if strict: return -1, -1 else: print("det(L) < e^(m*n) (good! If a solution exists < N^delta, it will be found)")
# display the lattice basis if debug: matrix_overview(BB, modulus^mm)
# LLL if debug: print("optimizing basis of the lattice via LLL, this can take a long time")
BB = BB.LLL()
if debug: print("LLL is done!")
# transform vector i & j -> polynomials 1 & 2 if debug: print("looking for independent vectors in the lattice") found_polynomials = False
for pol1_idx inrange(nn - 1): for pol2_idx inrange(pol1_idx + 1, nn): # for i and j, create the two polynomials PR.<w,z> = PolynomialRing(ZZ) pol1 = pol2 = 0 for jj inrange(nn): pol1 += monomials[jj](w*z+1,w,z) * BB[pol1_idx, jj] / monomials[jj](UU,XX,YY) pol2 += monomials[jj](w*z+1,w,z) * BB[pol2_idx, jj] / monomials[jj](UU,XX,YY)
# are these good polynomials? if rr.is_zero() or rr.monomials() == [1]: continue else: print("found them, using vectors", pol1_idx, "and", pol2_idx) found_polynomials = True break if found_polynomials: break
ifnot found_polynomials: print("no independant vectors could be found. This should very rarely happen...") return0, 0
rr = rr(q, q)
# solutions soly = rr.roots()
iflen(soly) == 0: print("Your prediction (delta) is too small") return0, 0
soly = soly[0][0] ss = pol1(q, soly) solx = ss.roots()[0][0]
# return solx, soly
defattack(N, e, factor_bit_length, factors, delta=0.25, m=1): x, y = ZZ["x", "y"].gens() A = N + 1 f = x * (A + y) + 1 X = int(RR(e) ** delta) Y = int(N^((factors-1)/factors)) t = int((1 - 2 * delta) * m) x0, y0 = boneh_durfee(f, e, m, t, X, Y) z = int(f(x0, y0)) if z % e == 0: phi = N +int(y0) + 1 return phi
returnNone
n = 11807668285253476335993112649125478311738138284032009521013160232275155591439056104050712447640428132619606976587476242139778581733169915977009541156271158342164308850183882293808973497182420010031579067660977663894542027297739371427053469215030263817360875451422844176774669484378609912250701562139454836788798283646984193408478412076837926365908454580066517380897820350918189254782926664771936070107395821284187885892036352109035393564580351743381904585469190706683218284347728580797326563996730184944085618477815023279614804702457729169640438408479963581615968900059881783129713048355206267321472950141238916990289 e = 5281486869323390978888423235641733430226678655686523392509714694751699050972120246916705000659875362083798615103093121939019773338011435050637066804667485505484551148317473841662442079996914658971301530910391768225029937660420184269526291001532582856000498690281601708552422507554114650938378386138884675333079129219306840932269967053765809850586002685750596186872500960539502358573116582308035530522367769717985694159855985288721479249298872468268929957454831663164937357314947859985362524031645431354657428639172589105754006273997415340441064909574647647141996659760272695449996972251425797398960494913207372272501 print(attack(n, e, 512, 2, 0.270,10)) #11807668285253476335993112649125478311738138284032009521013160232275155591439056104050712447640428132619606976587476242139778581733169915977009541156271158342164308850183882293808973497182420010031579067660977663894542027297739371427053469215030263817360875451422844176774669484378609912250701562139454836788580954283961789345480162099667001543891543639039785702888420923141077344358421051350061532584618771565302075962527648516570054467566143564854760959148274897390714017585220094430947903853829077089810614861107089070658414503772365171320735613805448738756377532942888788137666255740398790486415370724509249593956
from random import randrange from gmpy2 import gcd from Crypto.Util.number import isPrime from Crypto.Cipher import AES import hashlib def factorize_multi_prime(N, phi): prime_factors = set() factors = [N] while len(factors) > 0: # Element to factorize. N = factors[0]
w = randrange(2, N - 1) i = 1 while phi % (2**i) == 0: sqrt_1 = pow(w, phi // (2**i), N) if sqrt_1 > 1 and sqrt_1 != N - 1: # We can remove the element to factorize now, because we have a factorization. factors = factors[1:]
p = gcd(N, sqrt_1 + 1) q = N // p
if isPrime(p): prime_factors.add(p) elif p > 1: factors.append(p)
if isPrime(q): prime_factors.add(q) elif q > 1: factors.append(q)
_sage_const_1 = Integer(1); _sage_const_0 = Integer(0); _sage_const_1p = RealNumber('1.'); _sage_const_1000 = Integer(1000); _sage_const_7p = RealNumber('7.'); _sage_const_8 = Integer(8); _sage_const_2 = Integer(2) __author__ = 'x3nny' x_calc = lambda x, n: ((n - x) // x + _sage_const_1, (n - x) % x) defx_change(e_list): e_list = [(i, e_list[i]) for i inrange(len(e_list))] e_list = sorted(e_list, key=lambda x: x[_sage_const_1]) sum = max_i = _sage_const_0 indexs = [] exponents = [] for index, exp in e_list: sum += _sage_const_1 / exp indexs.append(index) if exp > max_i: max_i = exp ifsum > _sage_const_1p: break for i inrange(len(indexs)): exponents.append(x_calc(e_list[i][_sage_const_1], max_i)) return indexs, exponents
defmodinv(a, m): return gmpy2.invert(gmpy2.mpz(a), gmpy2.mpz(m)) defCRT(n, a): N = reduce(lambda x, y: x * y, n) result = 0 for n_i, a_i inzip(n, a): p = N / n_i s = modinv(p, n_i) result += a_i * s * p return result % N
N = reduce(lambda x, y: x * y, [n_list[i] for i in indexs])
T=[] n=[] tj=[] for i inrange(len(indexs)): n.append(n_list[indexs[i]]) tj.append([0for i inrange(len(indexs))]) tj[i][i]=1 for i inrange(len(indexs)): T.append(CRT(n,tj[i]))
P.<x> = PolynomialRing(Zmod(N)) g=0 for i inrange(len(indexs)): g+=((a_list[indexs[i]] * x + b_list[indexs[i]]) ** e_list[indexs[i]] - c_list[indexs[i]])*T[i] g = g.monic() m=g.small_roots(X=2**bits, beta=beta) if m: return m[_sage_const_0] returnNone
e = [] n = [] c = [] for i inrange(14): e.append(eval('e%d' % (i+1))) n.append(eval('N%d' % (i+1))) c.append(eval('Invitation%d' % (i+1))) flag=b'GWTH{'+b'x'*49 flag=bytes_to_long(flag) bits=flag.bit_length() b = [(i+1)*(3**bits) for i inrange(14)] a = [1for i inrange(14)] m = smupe(c, n, a, b, e_list=e) print(long_to_bytes(iroot(int(m), 2)[0]))
from pwn import * from hashlib import md5 from Crypto.Util.number import * from gmpy2 import * from sage.allimport * p = remote("node4.anna.nssctf.cn", 28932)
p.recvuntil(b'md5(str + ')
last = p.recv(4).decode() p.recvuntil(b'[0:5] == ') known_md5 = p.recv(5).decode()
temp = "0123456789qwertyuiopasdfghjklzxcvbnm"
defcalc_md5(): for i in temp: for j in temp: for k in temp: for l in temp: for m in temp: a = (i + j + k + l + m + last).encode() ifstr(md5(a).hexdigest())[0:5] == known_md5: return i + j + k + l + m
all_md5 = calc_md5() p.sendlineafter(b'Give me xxxxx: ', all_md5.encode())
from Crypto.Util.number import * import gmpy2 from flag import flag
defencrypt(plaintext): p = getStrongPrime(3072) m = bytes_to_long(plaintext) r = getRandomNBitInteger(1024) whileTrue: f = getRandomNBitInteger(1024) g = getStrongPrime(768) h = gmpy2.invert(f, p) * g % p c = (r * h + m * f) % p return (h, p, c)
""" h = 3967900409518491437091166715380802161532841159072519563471354336400750930009970177101953304861954502146570721506995224520631716261108071684882841102381144720177664434981608584075201907891964214604246219441325377602163957172642582158192223452845671007585556951922415200415538060247456213608112360361636912703380306386439846269645696750929811607783895294670639202472465920599542568227657152922843001792754116981992696203788298740550812661583820191877594185184758074771316815650833195023325150218113883046328740408517222933980589974912467363367727038230703152354450353199257411964288022409128890352346036423792759938468964462267528727695183747947515480432786669353434638860350849296620606820894819933050645748656981993408399675189724419997805599649975500093890450393421897803267909569938850674774386012819838940544502656293639875120854745249463561940935651895728242282430164407574626178693654713011323376912585958110558532953333 p = 4407206782832544188667944201727813617189883940490534227436068867901196311508151544316989531306678865408607390128649278629254128753967046691736522108356971272311308455619879297358588727267184200777923695048248757115057072357087881336680504033511958280710547178971268670442650871890760916203109226852889599638484429889898210426540567794020013920566784973281560628666918122674783539653720295629054898529900882965691587718212291373734218555167591690910246380516121338139063419587750344469214004539520017140593342859857394308703001939640899189432836134392830208318268131639318655382175643272565186884496188876341460968563623529229713790076050095498053846983536874648190033735162809614805624209827336432223553914651838063614534617044557310972056169869738746432924853953258079006936103497626054364115282007843847693813896856977882285910369660539092462408790126385881581833165309032853389777355480169212478669139225609058338565029211 c = 4052491539376955553220568757544621659293304958837707160681090710624505862889512520190589879197831394720145909992216099963759496125523078969015706069688556356682711471641851937470179182960755800968587551608595725470945584970094036299764623894583379909329996337429067328575804567222496890803396234507278490116354758303807070775249711087938549824010697869930856205244006491475201993228121418890520174179969294094963249013786611889790711801269524919695653453576043288934196952437164829830756439734795068980207758771052483500272264363028346668629397497794792110170275173209377114274164087320163340547019935562316429227119346802124620682293405375798340275679831750482339301440428527223801872439611461272229275824994734898078664180541096159146759378804836952981089673755590353588900522455968721971944276318473421193690310601002295637581030417570868955379815661133148339565983621730401675643094909263098778572081973142223744746526672 """
#sage from Crypto.Util.number import * from gmpy2 import * h = 3967900409518491437091166715380802161532841159072519563471354336400750930009970177101953304861954502146570721506995224520631716261108071684882841102381144720177664434981608584075201907891964214604246219441325377602163957172642582158192223452845671007585556951922415200415538060247456213608112360361636912703380306386439846269645696750929811607783895294670639202472465920599542568227657152922843001792754116981992696203788298740550812661583820191877594185184758074771316815650833195023325150218113883046328740408517222933980589974912467363367727038230703152354450353199257411964288022409128890352346036423792759938468964462267528727695183747947515480432786669353434638860350849296620606820894819933050645748656981993408399675189724419997805599649975500093890450393421897803267909569938850674774386012819838940544502656293639875120854745249463561940935651895728242282430164407574626178693654713011323376912585958110558532953333 p = 4407206782832544188667944201727813617189883940490534227436068867901196311508151544316989531306678865408607390128649278629254128753967046691736522108356971272311308455619879297358588727267184200777923695048248757115057072357087881336680504033511958280710547178971268670442650871890760916203109226852889599638484429889898210426540567794020013920566784973281560628666918122674783539653720295629054898529900882965691587718212291373734218555167591690910246380516121338139063419587750344469214004539520017140593342859857394308703001939640899189432836134392830208318268131639318655382175643272565186884496188876341460968563623529229713790076050095498053846983536874648190033735162809614805624209827336432223553914651838063614534617044557310972056169869738746432924853953258079006936103497626054364115282007843847693813896856977882285910369660539092462408790126385881581833165309032853389777355480169212478669139225609058338565029211 c = 4052491539376955553220568757544621659293304958837707160681090710624505862889512520190589879197831394720145909992216099963759496125523078969015706069688556356682711471641851937470179182960755800968587551608595725470945584970094036299764623894583379909329996337429067328575804567222496890803396234507278490116354758303807070775249711087938549824010697869930856205244006491475201993228121418890520174179969294094963249013786611889790711801269524919695653453576043288934196952437164829830756439734795068980207758771052483500272264363028346668629397497794792110170275173209377114274164087320163340547019935562316429227119346802124620682293405375798340275679831750482339301440428527223801872439611461272229275824994734898078664180541096159146759378804836952981089673755590353588900522455968721971944276318473421193690310601002295637581030417570868955379815661133148339565983621730401675643094909263098778572081973142223744746526672
L = matrix(ZZ, [[1, h],[0, p]]) v = L.LLL()[0] f, g = map(abs, v) m = ((f*c%p)*invert(f*f,g))%g print(long_to_bytes(m)) #b'Sangfor{pfa2s1f65ads4fwev1s2d3v1cxxavqes}' #NSSCTF{pfa2s1f65ads4fwev1s2d3v1cxxavqes}
flag = open('flag.txt', 'rb').read() m = bytes_to_long(flag) p = getPrime(512) q = getPrime(512) e = 65537 n = p * q c = pow(m, e, n)
s = getPrime(256) M = getPrime(5048) hint = (p - 297498275426) * inverse(s, M) % M t = open("message.txt", "w") t.write(f"e = {e}\nc = {c}\nn = {n}\nM = {M}\nhint = {hint}\n") t.close() """e = 65537 c = 11223534598141520071392544441952727165225232358333005778273904279807651365082135278999006409297342081157139972503703772556228315654837441044781410960887536342197257046095815516053582104516752168718754752274252871063410625756822861003235434929734796245933907621657696650609132419469456238860601166224944487116 n = 99499509473364452726944770421623721217675378717234178828554602484867641740497277374806036356486848621495917213623425604565104435195783450029879177770305417469850119739921527698744700219067563802483159458398895082044997907953256062342593605652927874232404778144112740505724215742062859322375891810785229735653 M = 28858066896535909755146975040720031655813099454455588895434479778600245612915775220883088811806723015938061791264869678085304248608125313205719043320256733514389739252845381708094678596099621503299764646358765107958130065721737938646850422959290465490270263553423913213684958592738500488797707239673645370968467090153285601432966586133693641854092761919184904521240074718850103356119966387029699913571443658384564840234765103070736676067458391659605655580766436276719610283460962533141261830775028138998594269732067550977245136631815804641115446066102981044849495663224005844657686979516481904043008222498344271373989609634617315702887646444506965035406154183377067490922195507071571055579654138590566650703038341939225657159668601565182939447340585110418258653618384852356058444795156595720943362884361136229430356254095673818462046182310826133487611183265532844700265640889105864909560170846171486510513240630480729194415061752698286990999064594811803482429976978688266632277914610443963726561921790718480343488391563503774868490108659902216386976683532579945706490286814310031310144410303859633785939399012605326754445715302492704458881700872467560968264583996658711892595658439058034434031646411995511168849724976850557976639662545139917517675296224197763447929417263845949813741362574641118781293171167041592771305352186419565096347024619027397784780864922205105185970180629777320680707022011697404359388540366320053501502698747763307336114482530784826238326983596966436776918503653153420281803168537703048371580451 hint = 24302462761412867722556483860201357169283131384498485449193507018526006760633350601593235386242712333885826513399701577522498685938541691414316724804357523659514319083860507720945068584985970098437482386854188516742033184163273293005356970701527614010961471490166306765208284126815267752826036846338185010168551115391901008731195800723630612524215610302192763771954146943262822909368086155537366851998954401585888789660061750804720858175620022924944428882337005545535959410243692854073069775794945154943244522898330286785483043492678802461244624116832548150221211726044545331789932659966539042635768789637635754297830131948383991027466194455817875377950516103513735000718642093769229006510961952865719649517629939801014585849419818774317178973918720330390674833583065434312010539617630210110724391629534996688713945139529416075521015600392479980677759342058040778532467875961508475990300178277703011765698425360329342396347848373844031930655143343217447877587074485794273364964346235973542157189093330870952677683308479410235841331914353677363106473914986073397716367455628483060709281215783434084559550690248426391913205234184130354155776334292729262232484610747771114078013979494659835579574006801652858265173309736540235377076956677464263798132149783780830729103485354096234062135454873557941791812722418582207577124971978987895472250326100927372068822672582017222521124179752698654114839303426099426224351872025466618402675104161895600513776962289703455252021742990686505176582638132300246212598903123706906104217087 """
from Crypto.Util.number import * from gmpy2 import * e = 65537 c = 11223534598141520071392544441952727165225232358333005778273904279807651365082135278999006409297342081157139972503703772556228315654837441044781410960887536342197257046095815516053582104516752168718754752274252871063410625756822861003235434929734796245933907621657696650609132419469456238860601166224944487116 n = 99499509473364452726944770421623721217675378717234178828554602484867641740497277374806036356486848621495917213623425604565104435195783450029879177770305417469850119739921527698744700219067563802483159458398895082044997907953256062342593605652927874232404778144112740505724215742062859322375891810785229735653 M = 28858066896535909755146975040720031655813099454455588895434479778600245612915775220883088811806723015938061791264869678085304248608125313205719043320256733514389739252845381708094678596099621503299764646358765107958130065721737938646850422959290465490270263553423913213684958592738500488797707239673645370968467090153285601432966586133693641854092761919184904521240074718850103356119966387029699913571443658384564840234765103070736676067458391659605655580766436276719610283460962533141261830775028138998594269732067550977245136631815804641115446066102981044849495663224005844657686979516481904043008222498344271373989609634617315702887646444506965035406154183377067490922195507071571055579654138590566650703038341939225657159668601565182939447340585110418258653618384852356058444795156595720943362884361136229430356254095673818462046182310826133487611183265532844700265640889105864909560170846171486510513240630480729194415061752698286990999064594811803482429976978688266632277914610443963726561921790718480343488391563503774868490108659902216386976683532579945706490286814310031310144410303859633785939399012605326754445715302492704458881700872467560968264583996658711892595658439058034434031646411995511168849724976850557976639662545139917517675296224197763447929417263845949813741362574641118781293171167041592771305352186419565096347024619027397784780864922205105185970180629777320680707022011697404359388540366320053501502698747763307336114482530784826238326983596966436776918503653153420281803168537703048371580451 hint = 24302462761412867722556483860201357169283131384498485449193507018526006760633350601593235386242712333885826513399701577522498685938541691414316724804357523659514319083860507720945068584985970098437482386854188516742033184163273293005356970701527614010961471490166306765208284126815267752826036846338185010168551115391901008731195800723630612524215610302192763771954146943262822909368086155537366851998954401585888789660061750804720858175620022924944428882337005545535959410243692854073069775794945154943244522898330286785483043492678802461244624116832548150221211726044545331789932659966539042635768789637635754297830131948383991027466194455817875377950516103513735000718642093769229006510961952865719649517629939801014585849419818774317178973918720330390674833583065434312010539617630210110724391629534996688713945139529416075521015600392479980677759342058040778532467875961508475990300178277703011765698425360329342396347848373844031930655143343217447877587074485794273364964346235973542157189093330870952677683308479410235841331914353677363106473914986073397716367455628483060709281215783434084559550690248426391913205234184130354155776334292729262232484610747771114078013979494659835579574006801652858265173309736540235377076956677464263798132149783780830729103485354096234062135454873557941791812722418582207577124971978987895472250326100927372068822672582017222521124179752698654114839303426099426224351872025466618402675104161895600513776962289703455252021742990686505176582638132300246212598903123706906104217087
L = matrix(ZZ, [[1, hint],[0, M]]) v = L.LLL() v = v[0] s, p = map(abs, v) p=p+297498275426 print(long_to_bytes(int(pow(c,invert(e,(p-1)*(n//p-1)),n)))) #b'flag{388bb794-ccda-f02e-79c6-8e44659c2481}' #NSSCTF{388bb794-ccda-f02e-79c6-8e44659c2481}
from Crypto.Util.number import * from flag import flag import gmpy2
defgen_prime(nbits, gamma): g = getPrime(int(nbits * gamma)) alpha = 0.5 - gamma whileTrue: a = getRandomNBitInteger(int(alpha * nbits)) p = 2 * g * a + 1 if isPrime(p): b = getRandomNBitInteger(int(alpha * nbits)) q = 2 * g * b + 1 h = 2 * g * a * b + a + b whilenot isPrime(q) or isPrime(h) or gmpy2.gcd(a, b) != 1: b = getRandomNBitInteger(int(alpha * nbits)) q = 2 * g * b + 1 return p, q
defencrypt(nbits, gamma): p, q = gen_prime(nbits, gamma) n = p * q e = getPrime(16) while gmpy2.gcd(e, gmpy2.lcm(p-1,q-1)) != 1: e = getPrime(16) m = bytes_to_long(flag) c = pow(m, e, n) return n, e, c
n, e, c = encrypt(1024, 0.48) print'n =', n print'e =', e print'c =', c
# n = 84236796025318186855187782611491334781897277899439717384242559751095347166978304126358295609924321812851255222430530001043539925782811895605398187299748256080526691975084042025794113521587064616352833904856626744098904922117855866813505228134381046907659080078950018430266048447119221001098505107823645953039 # e = 58337 # c = 13646200911032594651110040891135783560995665642049282201695300382255436792102048169200570930229947213493204600006876822744757042959653203573780257603577712302687497959686258542388622714078571068849217323703865310256200818493894194213812410547780002879351619924848073893321472704218227047519748394961963394668
from Crypto.Util.number import * from gmpy2 import invert
f = lambda x,n: (pow(x, n - 1, n) + 3) % n defphllard_rho(n): i = 1 whileTrue: a = getRandomRange(2, n) b = f(a, n) j = 1 whileTrue: p = GCD(abs(a - b), n) if p == n: break elif p > 1: return (p, n // p) else: a = f(a, n) b = f(f(b, n), n) j += 1 i += 1
n = 84236796025318186855187782611491334781897277899439717384242559751095347166978304126358295609924321812851255222430530001043539925782811895605398187299748256080526691975084042025794113521587064616352833904856626744098904922117855866813505228134381046907659080078950018430266048447119221001098505107823645953039 c = 13646200911032594651110040891135783560995665642049282201695300382255436792102048169200570930229947213493204600006876822744757042959653203573780257603577712302687497959686258542388622714078571068849217323703865310256200818493894194213812410547780002879351619924848073893321472704218227047519748394961963394668 e = 58337
p,q = phllard_rho(n) d = invert(e,(p-1)*(q-1)) print(long_to_bytes(pow(c,d,n))) #b'SangFor{0a8c2220-4c1b-32c8-e8c1-adf92ec7678b}' #NSSCTF{0a8c2220-4c1b-32c8-e8c1-adf92ec7678b}
N, e = (1108103848370322618250236235096737547381026108763302516499816051432801216813681568375319595638932562835292256776016949573972732881586209527824393027428125964599378845347154409633878436868422905300799413838645686430352484534761305185938956589612889463246508935994301443576781452904666072122465831585156151, 65537) c = 254705401581808316199469430068831357413481187288921393400711004895837418302514065107811330660948313420965140464021505716810909691650540609799307500282957438243553742714371028405100267860418626513481187170770328765524251710154676478766892336610743824131087888798846367363259860051983889314134196889300426
from Crypto.Util.number import * from gmpy2 import *
N=1108103848370322618250236235096737547381026108763302516499816051432801216813681568375319595638932562835292256776016949573972732881586209527824393027428125964599378845347154409633878436868422905300799413838645686430352484534761305185938956589612889463246508935994301443576781452904666072122465831585156151 e=65537 c = 254705401581808316199469430068831357413481187288921393400711004895837418302514065107811330660948313420965140464021505716810909691650540609799307500282957438243553742714371028405100267860418626513481187170770328765524251710154676478766892336610743824131087888798846367363259860051983889314134196889300426
f = lambda x,n: (pow(x, n - 1, n) + 3) % n defphllard_rho(n): i = 1 whileTrue: a = getRandomRange(2, n) b = f(a, n) j = 1 whileTrue: p = GCD(abs(a - b), n) if p == n: break elif p > 1: return (p, n // p) else: a = f(a, n) b = f(f(b, n), n) j += 1 i += 1 p,q=phllard_rho(N) print(long_to_bytes(int(pow(c,invert(e,(p-1)*(q-1)),N)))) #b"TSGCTF{Roll_Safe:_You_c4n't_be_exploited_1f_you_are_a_minimali5t_enough_and_y0u_don't_have_any_s3crets_in_your_mind}" #NSSCTF{Roll_Safe:_You_c4n't_be_exploited_1f_you_are_a_minimali5t_enough_and_y0u_don't_have_any_s3crets_in_your_mind}