p = int(raw_input("Enter a p: ")) q = int(raw_input("Enter a q: ")) e = int(raw_input("Enter a e: ")) t = (p-1)*(q-1) i=0 whileTrue : if (1-t*i)%e == 0: break i-=1 print i print'ok:' + '%d' % ((1-t*i)/e)
脚本写得老粗糙
python模块解决
1 2 3 4 5
import gmpy2 p = int(raw_input("Enter a p: ")) q = int(raw_input("Enter a q: ")) e = int(raw_input("Enter a e: ")) gmpy2.invert(e, (p-1)*(q-1)) `
#!/usr/bin/env python # -*- coding: utf-8 -*- import libnum import gmpy2 f = open('flag.enc','r') c = f.read() c = libnum.s2n(c) p = 275127860351348928173285174381581152299 q = 319576316814478949870590164193048041239 n = p*q r = pow(c,(p+1)/4,p) s = pow(c,(q+1)/4,q) a = gmpy2.invert(p,q) b = gmpy2.invert(q,p) x =(a*p*s+b*q*r)%n y =(a*p*s-b*q*r)%n print libnum.n2s(x%n) print libnum.n2s((-x)%n) print libnum.n2s(y%n) print libnum.n2s((-y)%n)
#!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'ByStudent' from libnum import s2n,n2s from gmpy2 import iroot n = 0xB0BEE5E3E9E5A7E8D00B493355C618FC8C7D7D03B82E409951C182F398DEE3104580E7BA70D383AE5311475656E8A964D380CB157F48C951ADFA65DB0B122CA40E42FA709189B719A4F0D746E2F6069BAF11CEBD650F14B93C977352FD13B1EEA6D6E1DA775502ABFF89D3A8B3615FD0DB49B88A976BC20568489284E181F6F11E270891C8EF80017BAD238E363039A458470F1749101BC29949D3A4F4038D463938851579C7525A69984F15B5667F34209B70EB261136947FA123E549DFFF00601883AFD936FE411E006E4E93D1A00B0FEA541BBFC8C5186CB6220503A94B2413110D640C77EA54BA3220FC8F4CC6CE77151E29B3E06578C478BD1BEBE04589EF9A197F6F806DB8B3ECD826CAD24F5324CCDEC6E8FEAD2C2150068602C8DCDC59402CCAC9424B790048CCDD9327068095EFA010B7F196C74BA8C37B128F9E1411751633F78B7B9E56F71F77A1B4DAAD3FC54B5E7EF935D9A72FB176759765522B4BBC02E314D5C06B64D5054B7B096C601236E6CCF45B5E611C805D335DBAB0C35D226CC208D8CE4736BA39A0354426FAE006C7FE52D5267DCFB9C3884F51FDDFDF4A9794BCFE0E1557113749E6C8EF421DBA263AFF68739CE00ED80FD0022EF92D3488F76DEB62BDEF7BEA6026F22A1D25AA2A92D124414A8021FE0C174B9803E6BB5FAD75E186A946A17280770F1243F4387446CCCEB2222A965CC30B3929 e = 3 f = open('flag.enc','rb') c= f.read() c = s2n(c) f.close() i = 0 while1: res = iroot(c+i*n,3) if(res[1] == True): print res break print"i="+str(i) i = i+1 #i=118719487 m = 440721643740967258786371951429849843897639673893942371730874939742481383302887786063966117819631425015196093856646526738786745933078032806737504580146717737115929461581126895844008044713461807791172016433647699394456368658396746134702627548155069403689581548233891848149612485605022294307233116137509171389596747894529765156771462793389236431942344003532140158865426896855377113878133478689191912682550117563858186 print n2s(m) # 结果 # Didn't you know RSA padding is really important? Now you see a non-padding message is so dangerous. And you should notice this in future.Fl4g: PCTF{Sm4ll_3xpon3nt_i5_W3ak}
$$ X^4 -(x_1 + x_2 + x_3 + x_4)X^3 + (x_1 x_2 + x_1 x_3 + x_1 x_4 + x_2 x_3 + x_2 x_4 + x_3 x_4)X^2 - (x_2 x_3 x_4 + x_1 x_3 x_4 + x_1 x_2 x_4 + x_1 x_2 x_3)X + x_1 x_2 x_3 x_4 = 0 $$ import math import gmpy2 gmpy2.get_context().precision=5000 from polycubicroot import * def Carpenter(p, q,r, s): p = gmpy2.mpfr(p) q = gmpy2.mpfr(q) r = gmpy2.mpfr(r) s = gmpy2.mpfr(s) “”” Solves for all roots of the quartic polynomial P(x) = x^4 + px^3 + qx^2 + rx + s. “”” #print “@@@ inside Carpenter”, p,q,r,s pby4 = p/4.0 C = ((6 * pby4) - 3p)pby4 + q D = (((-4pby4) + 3p)pby4 - 2q)pby4 + r E = (((pby4 - p) pby4 + q)pby4 - r)pby4 + s #print “C, D, E=”,C, D, E root = None for zero in polyCubicRoots(2C, (C**2 - 4E), -D2): #print “zero = “, zero if type(zero)== type(gmpy2.mpfr(1.0)) and zero > 0.0: root = zero #print “found a positive root.” break if root == None: return None sqroot = gmpy2.sqrt(root) Q1 = -root/4.0 - C/2.0 - D/2.0 / sqroot Q2 = -root/4.0 - C/2.0 + D/2.0 / sqroot #print “Q1,Q2=”, Q1, Q2 sqy2 = sqroot/2.0 if Q1 >= 0.0: sqQ1 = gmpy2.sqrt(Q1) z1 = sqy2 + sqQ1 -pby4 z2 = sqy2 - sqQ1 -pby4 else: sqQ1 = gmpy2.sqrt(-Q1) z1 = (sqy2-pby4, sqQ1) z2 = (sqy2-pby4, - sqQ1) if Q2 >= 0.0: sqQ2 = gmpy2.sqrt(Q2) z3 = -sqy2 - sqQ2 -pby4 z4 = -sqy2 + sqQ2 -pby4 else: sqQ2 = gmpy2.sqrt(-Q2) z3 = (-sqy2-pby4, sqQ2) z4 = (-sqy2-pby4, -sqQ2) return (z1, z2,z3, z4) 得到一个 4 元组解和循环的左侧和右侧的任何 4 根,看看是否我们得到的除数$s$,应完全$a b c d$。如果我们得到一个然后基本上解决问题。 剩下的唯一问题是,我们不知道多少零点的每个$p$, $q$ ,$r$有。让我们试着从 3 零 0 零的所有组合︰ p= 0b10001110000110111100100111111000100111 q= 0b1110110010101000100001010011110000010100100010111011100111100011010001010101 r= 0b1010111100101001100011011101101000000110011010100010001010001100010000111001010011110111011000110010000110010001 s= 0b11000010011110000010000010000101010000011101100011101110110110011111000000001011001101111100111001111110001100100101101010011011011101110111001110101 A=[] B=[] C=[] D=[s] for i in range(0,3): A.append(p * (2**i)) B.append(q * (2i)) C.append(r * (2**i)) for i in A: print “====================” for j in B: for k in C: for l in D: (x1, x2, x3, x4) = Carpenter(-i,j,-k,l)
since sometimes the solution will consist of complex numbers, we’ll just disregard those with a try-except
try: #approximate value of x1 aprox = int(x1.floor()) for step in range(0,10000): #try to the right if s % (aprox + step) == 0: print aprox + step #and to the left if s % (aprox - step) == 0: print aprox - step except: pass
N = 136463296143190893248608270448493439350683115492680876168052084053504347848230208722375209906076869052896867483336405778205506777456239889195254751404720423599579856449118314029926341504368322006603240157122006887052352260584975471045726061549619820648607756554188235927302932154082823424745427535138517580059079182622982686168676727690710935334448342460007367100561867135456783067679052102951400317012276135721843672557488711786097028300122487504246207343408690682175135816894247936690735246673985277504237071409859401185555290479154689954442396078547166388871978321863215123445269948324347122830270551854130365741961149443091305418900821360286967705810624380568422193310901491451664233271486835428015602712828527839237760712498204208114606471362410690870710499020885304151928570632315330437235330924071236528549152143092328833484016372976817809299430339752106123272706667690980774118545338761390919046832338928913059106351087015273177895380149217373395580948428521961929052291618261409715145038026920685297885235683434107218249548518659574493203836870070093674704878498465104238178877833471640168999551981025958271865584457582649595664499635489167898398826429084686279399959537724187268210760718340639939389766681703213695409551029265784749153180083564347733676434492122290166926822855552175712346350104533901079605669192289178418165584907326210349152609265563881542151049862642882025360604692084264752182798892702414638180781403398216616666472863758371330058299406233359567750413748257953598843651612399709945608339073191578573877985362619301768016485017815171486743934037935902150228210340093187387036580763941637989511155597643722984057627086517479392172108031663245553269