def modulus_inutilis(): n = 17258212916191948536348548470938004244269544560039009244721959293554822498047075403658429865201816363311805874117705688359853941515579440852166618074161313773416434156467811969628473425365608002907061241714688204565170146117869742910273064909154666642642308154422770994836108669814632309362483307560217924183202838588431342622551598499747369771295105890359290073146330677383341121242366368309126850094371525078749496850520075015636716490087482193603562501577348571256210991732071282478547626856068209192987351212490642903450263288650415552403935705444809043563866466823492258216747445926536608548665086042098252335883 e = 3 ct = 243251053617903760309941844835411292373350655973075480264001352919865180151222189820473358411037759381328642957324889519192337152355302808400638052620580409813222660643570085177957 gmpy2.get_context().precision = 1000 plaintext = int(gmpy2.cbrt(ct)) return trans(plaintext)
def initConstants(cls): from math import sqrt def nextPrime(n=2): while True: for factor in range(2,int(sqrt(n))+1): if n % factor == 0: break else: yield n n += 1 # Find the first nRounds primes npgen = nextPrime() primes = [next(npgen) for x in range(cls.nRounds)] fb_mul = 2 ** cls.wordBits def getFractionalBits(n): return int((n - int(n)) * fb_mul) if cls.use_gmp: from gmpy2 import context,set_context,sqrt,cbrt set_context(context(precision=75)) else: cbrt = lambda n: pow(n, 1 / 3) # First wordBits bits of the fractional parts of the square roots of the first 8 primes H = (getFractionalBits(sqrt(n)) for n in primes[:8]) # First wordBits bits of the fractional parts of the cube roots of the first nRounds primes K = (getFractionalBits(cbrt(n)) for n in primes) cls.H_init = tuple(H) cls.K = tuple(K)
def addPrefixToSignature(self, signatureSuffix): prefix = "\x00\x01" prefix += "\xFF"*8 while True: testPrefix = prefix + os.urandom((self.keysize/8) - (len(prefix))) signatureCandidate = toBytes(int(gmpy2.cbrt(toInt(testPrefix))))[:-len(signatureSuffix)] + signatureSuffix toCheck = toBytes(toInt(signatureCandidate) ** 3) if "\x00" not in toCheck[:-len(signatureSuffix)]: return signatureCandidate
def recover_msg(N1, N2, N3, C1, C2, C3): m = 42 # your code starts here: to calculate the original message - m # Note 'm' should be an integer N = N1 * N2 * N3 #y1 = N / N1 #y2 = N / N2 #y3 = N / N3 s = [1, 0] r = [(N / N1), N1] i = 2 temp = (N / N1) % N1 while (temp > 0): temp = r[i - 2] % r[i - 1] r.append(temp) s.append(s[i - 2] - (r[i - 2] / r[i - 1]) * s[i - 1]) i += 1 z1 = s[i - 2] s = [1, 0] r = [(N / N2), N2] i = 2 temp = (N / N2) % N2 while (temp > 0): temp = r[i - 2] % r[i - 1] r.append(temp) s.append(s[i - 2] - (r[i - 2] / r[i - 1]) * s[i - 1]) i += 1 z2 = s[i - 2] s = [1, 0] r = [(N / N3), N3] i = 2 temp = (N / N3) % N3 while (temp > 0): temp = r[i - 2] % r[i - 1] r.append(r[i - 2] % r[i - 1]) s.append(s[i - 2] - (r[i - 2] / r[i - 1]) * s[i - 1]) i += 1 z3 = s[i - 2] if z1 < 0: z1 += N1 if z2 < 0: z2 += N2 if z3 < 0: z3 += N3 x = (z1 * C1 * (N / N1) + z2 * C2 * (N / N2) + z3 * C3 * (N / N3)) % N gmpy2.set_context(gmpy2.context()) gmpy2.get_context().precision = 1000000 m = int(gmpy2.cbrt(x)) # your code ends here # convert the int to message string msg = hex(m).rstrip('L')[2:].decode('hex') return msg
def initConstants(cls): from math import sqrt def nextPrime(n=2): while True: for factor in range(2, int(sqrt(n)) + 1): if n % factor == 0: break else: yield n n += 1 # Find the first nRounds primes npgen = nextPrime() primes = [next(npgen) for x in range(cls.nRounds)] fb_mul = 2**cls.wordBits def getFractionalBits(n): return int((n - int(n)) * fb_mul) if cls.use_gmp: from gmpy2 import context, set_context, sqrt, cbrt # context() parameters are platform-dependent! set_context(context(precision=75, round=1)) # OK for gmp 6.1.2 / gmpy 2.1.0 else: cbrt = lambda n: pow(n, 1 / 3) # First wordBits bits of the fractional parts of the square roots of the first 8 primes H = (getFractionalBits(sqrt(n)) for n in primes[:8]) # First wordBits bits of the fractional parts of the cube roots of the first nRounds primes K = (getFractionalBits(cbrt(n)) for n in primes) cls.H_init = tuple(H) cls.K = tuple(K)
import gmpy2 from Crypto.Util.number import long_to_bytes e = 3 c = 15478048932253023588842854432571029804744949209594765981036255304813254166907810390192307350179797882093083784426352342087386691689161026226569013804504365566204100805862352164561719654280948792015789195399733700259059935680481573899984998394415788262265875692091207614378805150701529546742392550951341185298005693491963903543935069284550225309898331197615201102487312122192298599020216776805409980803971858120342903012970709061841713605643921523217733499022158425449427449899738610289476607420350484142468536513735888550288469210058284022654492024363192602734200593501660208945967931790414578623472262181672206606709 n = 21034814455172467787319632067588541051616978031477984909593707891829600195022041640200088624987623056713604514239406145871910044808006741636513624835862657042742260288941962019533183418661144639940608960169440421588092324928046033370735375447302576018460809597788053566456538713152022888984084306297869362373871810139948930387868426850576062496427583397660227337178607544043400076287217521751017970956067448273578322298078706011759257235310210160153287198740097954054080553667336498134630979908988858940173520975701311654172499116958019179004876438417238730801165613806576140914402525031242813240005791376093215124477 gmpy2.get_context().precision = 10000 # print(gmpy2.cbrt(c)) # (m * m * m) % n = c # (m % n * m % n * m % n) % n = c # m^3 * 1 = x * n + c # c + x * n = m^3 # bruteforce check? for i in range(10000000): temp = n * i + c if int(gmpy2.cbrt(temp))**3 == (c + i * n): print("Found ", i) break x = 1831 print(1831 * n + c, long_to_bytes(int(gmpy2.cbrt(1831 * n + c)))) # print(long_to_bytes(int(gmpy2.cbrt(c - (x * n))))) # CTF{34sy_RS4_1s_e4sy_us3}
cipher = 2780321436921227845269766067805604547641764672251687438825498122989499386967784164108893743279610287605669769995594639683212592165536863280639528420328182048065518360606262307313806591343147104009274770408926901136562839153074067955850912830877064811031354484452546219065027914838811744269912371819665118277221 n = 23571113171923293137414347535961677173798389971011031071091131271311371391491511571631671731791811911931971992112232272292332392412512572632692712772812832933073113133173313373473493533593673733793833893974014094194214314334394434494574614634674794874914995035095215235415475575635695715775875935996016076136176196316416436476536596616736776836917017097197277337397437517577617697737877978098118218238278298398538578598638778818838879079119199299379419479539679719779839919971009101310191431936117404941729571877755575331917062752829306305198341421305376800954281557410379953262534149212590443063350628712530148541217933209759909975139820841212346188350112608680453894647472456216566674289561525527394398888860917887112180144144965154878409149321280697460295807024856510864232914981820173542223592901476958693572703687098161888680486757805443187028074386001621827485207065876653623459779938558845775617779542038109532989486603799040658192890612331485359615639748042902366550066934348195272617921683 Low Public Exponent Attack """ import sys try: import gmpy2 except ImportError: print("Install gmpy2 first to run this program") sys.exit() e = 3 cipher = 2780321436921227845269766067805604547641764672251687438825498122989499386967784164108893743279610287605669769995594639683212592165536863280639528420328182048065518360606262307313806591343147104009274770408926901136562839153074067955850912830877064811031354484452546219065027914838811744269912371819665118277221 n = 23571113171923293137414347535961677173798389971011031071091131271311371391491511571631671731791811911931971992112232272292332392412512572632692712772812832933073113133173313373473493533593673733793833893974014094194214314334394434494574614634674794874914995035095215235415475575635695715775875935996016076136176196316416436476536596616736776836917017097197277337397437517577617697737877978098118218238278298398538578598638778818838879079119199299379419479539679719779839919971009101310191431936117404941729571877755575331917062752829306305198341421305376800954281557410379953262534149212590443063350628712530148541217933209759909975139820841212346188350112608680453894647472456216566674289561525527394398888860917887112180144144965154878409149321280697460295807024856510864232914981820173542223592901476958693572703687098161888680486757805443187028074386001621827485207065876653623459779938558845775617779542038109532989486603799040658192890612331485359615639748042902366550066934348195272617921683 n = hex(n) import gmpy2 with gmpy2.local_context(gmpy2.context(), precision=800) as ctx: ctx.precision += 800 root = gmpy2.cbrt(cipher) try: print(str('%x' % +int(root)).decode('hex')) except AttributeError: print(bytes.fromhex(str('%x' % +int(root))).decode('utf-8')) # answer!! # dsc{t0-m355-w1th-m4th-t4k35-4-l0t-0f-sp1n3}
import gmpy2 cipher = 2205316413931134031046440767620541984801091216351222789180582564557328762455422721368029531360076729972211412236072921577317264715424950823091382203435489460522094689149595951010342662368347987862878338851038892082799389023900415351164773 with gmpy2.local_context(gmpy2.context(), precision=100000) as ctx: m = gmpy2.cbrt(cipher) try: print(str('%x' % + int(m)).decode('hex')) except AttributeError: print(bytes.fromhex(str('%x' % + int(m))).decode('utf-8'))
# 1/e % n def modInverse(e, n): return egcd(e, n)[0] % n (p, q) = rfact(n, e, d) print "p", hex(p) print "q", hex(q) print "expo1", hex(d % (p - 1)) print "expo2", hex(d % (q - 1)) print "coeff", hex(modInverse(q, p)) orig = int(open("secret").read().encode("hex"), 16) c = orig while True: m = gmpy2.cbrt(c) if pow(int(m), 3, n) == orig: print "pwned" out = "" for k in xrange(212, -1, -1): blah = 27**k val = int(m / blah) m = m - val * blah if val == 0: out += " " else: out += chr(0x60 + val) print out[::-1] break c += n
def endless_emails(): modulo = [ 14528915758150659907677315938876872514853653132820394367681510019000469589767908107293777996420037715293478868775354645306536953789897501630398061779084810058931494642860729799059325051840331449914529594113593835549493208246333437945551639983056810855435396444978249093419290651847764073607607794045076386643023306458718171574989185213684263628336385268818202054811378810216623440644076846464902798568705083282619513191855087399010760232112434412274701034094429954231366422968991322244343038458681255035356984900384509158858007713047428143658924970374944616430311056440919114824023838380098825914755712289724493770021, 20463913454649855046677206889944639231694511458416906994298079596685813354570085475890888433776403011296145408951323816323011550738170573801417972453504044678801608709931200059967157605416809387753258251914788761202456830940944486915292626560515250805017229876565916349963923702612584484875113691057716315466239062005206014542088484387389725058070917118549621598629964819596412564094627030747720659155558690124005400257685883230881015636066183743516494701900125788836869358634031031172536767950943858472257519195392986989232477630794600444813136409000056443035171453870906346401936687214432176829528484662373633624123, 19402640770593345339726386104915705450969517850985511418263141255686982818547710008822417349818201858549321868878490314025136645036980129976820137486252202687238348587398336652955435182090722844668488842986318211649569593089444781595159045372322540131250208258093613844753021272389255069398553523848975530563989367082896404719544411946864594527708058887475595056033713361893808330341623804367785721774271084389159493974946320359512776328984487126583015777989991635428744050868653379191842998345721260216953918203248167079072442948732000084754225272238189439501737066178901505257566388862947536332343196537495085729147, 12005639978012754274325188681720834222130605634919280945697102906256738419912110187245315232437501890545637047506165123606573171374281507075652554737014979927883759915891863646221205835211640845714836927373844277878562666545230876640830141637371729405545509920889968046268135809999117856968692236742804637929866632908329522087977077849045608566911654234541526643235586433065170392920102840518192803854740398478305598092197183671292154743153130012885747243219372709669879863098708318993844005566984491622761795349455404952285937152423145150066181043576492305166964448141091092142224906843816547235826717179687198833961, 17795451956221451086587651307408104001363221003775928432650752466563818944480119932209305765249625841644339021308118433529490162294175590972336954199870002456682453215153111182451526643055812311071588382409549045943806869173323058059908678022558101041630272658592291327387549001621625757585079662873501990182250368909302040015518454068699267914137675644695523752851229148887052774845777699287718342916530122031495267122700912518207571821367123013164125109174399486158717604851125244356586369921144640969262427220828940652994276084225196272504355264547588369516271460361233556643313911651916709471353368924621122725823, 25252721057733555082592677470459355315816761410478159901637469821096129654501579313856822193168570733800370301193041607236223065376987811309968760580864569059669890823406084313841678888031103461972888346942160731039637326224716901940943571445217827960353637825523862324133203094843228068077462983941899571736153227764822122334838436875488289162659100652956252427378476004164698656662333892963348126931771536472674447932268282205545229907715893139346941832367885319597198474180888087658441880346681594927881517150425610145518942545293750127300041942766820911120196262215703079164895767115681864075574707999253396530263, 19833203629283018227011925157509157967003736370320129764863076831617271290326613531892600790037451229326924414757856123643351635022817441101879725227161178559229328259469472961665857650693413215087493448372860837806619850188734619829580286541292997729705909899738951228555834773273676515143550091710004139734080727392121405772911510746025807070635102249154615454505080376920778703360178295901552323611120184737429513669167641846902598281621408629883487079110172218735807477275590367110861255756289520114719860000347219161944020067099398239199863252349401303744451903546571864062825485984573414652422054433066179558897 ] ciphertext = [ 6965891612987861726975066977377253961837139691220763821370036576350605576485706330714192837336331493653283305241193883593410988132245791554283874785871849223291134571366093850082919285063130119121338290718389659761443563666214229749009468327825320914097376664888912663806925746474243439550004354390822079954583102082178617110721589392875875474288168921403550415531707419931040583019529612270482482718035497554779733578411057633524971870399893851589345476307695799567919550426417015815455141863703835142223300228230547255523815097431420381177861163863791690147876158039619438793849367921927840731088518955045807722225, 5109363605089618816120178319361171115590171352048506021650539639521356666986308721062843132905170261025772850941702085683855336653472949146012700116070022531926476625467538166881085235022484711752960666438445574269179358850309578627747024264968893862296953506803423930414569834210215223172069261612934281834174103316403670168299182121939323001232617718327977313659290755318972603958579000300780685344728301503641583806648227416781898538367971983562236770576174308965929275267929379934367736694110684569576575266348020800723535121638175505282145714117112442582416208209171027273743686645470434557028336357172288865172, 5603386396458228314230975500760833991383866638504216400766044200173576179323437058101562931430558738148852367292802918725271632845889728711316688681080762762324367273332764959495900563756768440309595248691744845766607436966468714038018108912467618638117493367675937079141350328486149333053000366933205635396038539236203203489974033629281145427277222568989469994178084357460160310598260365030056631222346691527861696116334946201074529417984624304973747653407317290664224507485684421999527164122395674469650155851869651072847303136621932989550786722041915603539800197077294166881952724017065404825258494318993054344153, 1522280741383024774933280198410525846833410931417064479278161088248621390305797210285777845359812715909342595804742710152832168365433905718629465545306028275498667935929180318276445229415104842407145880223983428713335709038026249381363564625791656631137936935477777236936508600353416079028339774876425198789629900265348122040413865209592074731028757972968635601695468594123523892918747882221891834598896483393711851510479989203644477972694520237262271530260496342247355761992646827057846109181410462131875377404309983072358313960427035348425800940661373272947647516867525052504539561289941374722179778872627956360577, 8752507806125480063647081749506966428026005464325535765874589376572431101816084498482064083887400646438977437273700004934257274516197148448425455243811009944321764771392044345410680448204581679548854193081394891841223548418812679441816502910830861271884276608891963388657558218620911858230760629700918375750796354647493524576614017731938584618983084762612414591830024113057983483156974095503392359946722756364412399187910604029583464521617256125933111786441852765229820406911991809039519015434793656710199153380699319611499255869045311421603167606551250174746275803467549814529124250122560661739949229005127507540805, 23399624135645767243362438536844425089018405258626828336566973656156553220156563508607371562416462491581383453279478716239823054532476006642583363934314982675152824147243749715830794488268846671670287617324522740126594148159945137948643597981681529145611463534109482209520448640622103718682323158039797577387254265854218727476928164074249568031493984825273382959147078839665114417896463735635546290504843957780546550577300001452747760982468547756427137284830133305010038339400230477403836856663883956463830571934657200851598986174177386323915542033293658596818231793744261192870485152396793393026198817787033127061749, 15239683995712538665992887055453717247160229941400011601942125542239446512492703769284448009141905335544729440961349343533346436084176947090230267995060908954209742736573986319254695570265339469489948102562072983996668361864286444602534666284339466797477805372109723178841788198177337648499899079471221924276590042183382182326518312979109378616306364363630519677884849945606288881683625944365927809405420540525867173639222696027472336981838588256771671910217553150588878434061862840893045763456457939944572192848992333115479951110622066173007227047527992906364658618631373790704267650950755276227747600169403361509144 ] # coprime check for i in range(len(modulo)): for j in range(i + 1, len(modulo)): assert (gmpy2.gcd(modulo[i], modulo[j]) == 1) # checking to make sure it works # ciphertext = [529, 414, 558] # modulo = [629, 2173, 1159] product = 1 for i in modulo: product *= i gmpy2.get_context().precision = 10000 ret = (crt(modulo, ciphertext)[0]) # run a check to make sure the crt value is correct for i in range(7): assert ((ret + product) % modulo[i] == ciphertext[i]) assert (int(ret) == ret) # Turns out not all messages are the same for i in range(len(modulo)): for j in range(i + 1, len(modulo)): for k in range(j + 1, len(modulo)): temp1 = [modulo[i], modulo[j], modulo[k]] temp2 = [ciphertext[i], ciphertext[j], ciphertext[k]] ret = crt(temp1, temp2)[0] temp3 = gmpy2.cbrt(ret) if (temp3 == int(temp3)): return trans(int(temp3)) # print(ret % product == ret) # print(gmpy2.cbrt(ret)) # orig = ret # ret = gmpy2.iroot(ret, 3)[0] # print(ret) # print(orig) # print(6163435258792081155570563724535482791203179091979152504421757686683886493041873412493545277924714159737287201858788975497745808400937668458605428592558762083014850528313290613930252878249664198812745222654769820800241714978826271054236532198430467610687313659504279838226086861781305095949833574849716879096699647639958527921958637834064006172266890540479928345057273368544415806236989595476517313435658885845707049493257058926418496568638144697200407609515013103968647293188517382989530930549157220335692939060237456534893474572240265398889167814394704328134737878272985108974227486747707167006223245092132605468821480666851989946884699570651793971720879741935255673412957496812959560447653617121882766833325630820418452175677609504859541364979563245379911740546513300811333775982093598441414943432903846748986044773051271273074178187927616383656534025093239974329202223312844777650132177288938999947223314403450951104308875899846070329618172976449337886028742891717458284020666066406018267577671532200481210396120408114235438891394741637492599984773722590015110968648852335612801270812495940724242254856590121053924868070702836783742218196309136657025599313169326518028561002568919672558441696873251945314360866858517591470667708009596072897901621906067954652464219937450386826886270166857948082249114796422275205761431557543779017703807244769877494961517975032755320293361788375328727084392074072910374720592266764026634565924091861726 ** 3) # print(orig - 6163435258792081155570563724535482791203179091979152504421757686683886493041873412493545277924714159737287201858788975497745808400937668458605428592558762083014850528313290613930252878249664198812745222654769820800241714978826271054236532198430467610687313659504279838226086861781305095949833574849716879096699647639958527921958637834064006172266890540479928345057273368544415806236989595476517313435658885845707049493257058926418496568638144697200407609515013103968647293188517382989530930549157220335692939060237456534893474572240265398889167814394704328134737878272985108974227486747707167006223245092132605468821480666851989946884699570651793971720879741935255673412957496812959560447653617121882766833325630820418452175677609504859541364979563245379911740546513300811333775982093598441414943432903846748986044773051271273074178187927616383656534025093239974329202223312844777650132177288938999947223314403450951104308875899846070329618172976449337886028742891717458284020666066406018267577671532200481210396120408114235438891394741637492599984773722590015110968648852335612801270812495940724242254856590121053924868070702836783742218196309136657025599313169326518028561002568919672558441696873251945314360866858517591470667708009596072897901621906067954652464219937450386826886270166857948082249114796422275205761431557543779017703807244769877494961517975032755320293361788375328727084392074072910374720592266764026634565924091861726 ** 3) # print(hex(ret)) # print(bytes.fromhex('0' + hex(ret)[2:])) # Idea 1: I thought maybe there was a cube root error # ret = int(gmpy2.cbrt(ret)) - 200 # # print(ret) # for i in range(100 * 2): # try: # # print(trans(ret + i)) # print(bytes.fromhex(hex(ret)[2:] + '0')) # except: # continue # Idea 2: I thoughtmaybe one of the algs was implemented incorrectly # ret2 = solve_crt(ciphertext, modulo) # assert(ret == ret2) # Idea 3: I tried to take the cube root of the ret value and brute forced k*product + residue # factor = 1 # factor = 70000000 # ret += (product * factor) # while (factor < 10000000000): # ret += product # if factor % 10000000 == 0: # print(factor) # if gmpy2.is_power(ret): # print("factor is: ", factor) # factor +=1 return None
#!/usr/bin/env python3 import gmpy2 from gmpy2 import mpfr from Cryptodome.Util.number import long_to_bytes gmpy2.get_context().precision = 1000 c = 2205316413931134031074603746928247799030155221252519872650082343781881947286623459260358458095368337105247516735006016223547924074432814737081052371203373104854490121754016011241903971190586239974732476290129461147622505210058893325312869 print(long_to_bytes(int(gmpy2.cbrt(c))).decode('utf-8'))
import binascii import gmpy2 from rsa import RSA rsa_0 = RSA() n_0 = rsa_0.get_public_key()[1] rsa_1 = RSA() n_1 = rsa_1.get_public_key()[1] rsa_2 = RSA() n_2 = rsa_2.get_public_key()[1] message = "secret" # we use crt to find message number = int(binascii.hexlify(message.encode()), 16) print(number) c_0 = rsa_0.encrypt(number) c_1 = rsa_1.encrypt(number) c_2 = rsa_2.encrypt(number) result = 0 result += c_0 * (n_1 * n_2) * pow(n_1 * n_2, -1, n_0) result += c_1 * (n_0 * n_2) * pow(n_0 * n_2, -1, n_1) result += c_2 * (n_0 * n_1) * pow(n_0 * n_1, -1, n_2) decrypted = result % (n_0 * n_1 * n_2) decrypted = int(gmpy2.cbrt(decrypted)) print(decrypted) print(binascii.unhexlify(hex(decrypted)[2:]).decode())
#!/usr/bin/python3 from Crypto.Util.number import inverse import gmpy2 # https://gmpy2.readthedocs.io/en/latest/mpfr.html n = 17260541145579198891286838820507756585494408484294770862002805660577661138753926064444981930310528890773266098356882517290033235056654103412024620204547445159627671127518965960486480229617902782023368077819854820837387791591683428592246121552228695417314295153721144499366280389254552597040315734269703314601367296670296596449184388246232676724558126845148454433428619114310396032130452938580427564701080866025573039407415733982384144637567337164211240182263026767455207192185903776322079215198832973810587735067694801737731617941390472537291532113711292822595269219795255224521641891049508289784761579371125213474439 e = 3 ct = 1112413624683819960899152482895461211039349964898672381675850025556800617245120168928400758297834676330400246617472191750627367991315450127361583383350639760738254818244740474313061192563860605923503717 with gmpy2.local_context(gmpy2.context(), precision=800) as ctx: ctx.precision += 800 root = gmpy2.cbrt(ct) try: print(str("%x" % +int(root)).decode("hex")) except AttributeError: print(bytes.fromhex(str("%x" % +int(root))).decode())