import libnum import gmpy2 defegcd(a, b):#求a*e1+b*e2中的a,b if a == 0: return b, 0, 1 else: g, y, x = egcd(b % a, a) return g, x - (b // a) * y, y #g=1 y=a x=b
import gmpy2 import libnum e = 65537 n=248254007851526241177721526698901802985832766176221609612258877371620580060433101538328030305219918697643619814200930679612109885533801335348445023751670478437073055544724280684733298051599167660303645183146161497485358633681492129668802402065797789905550489547645118787266601929429724133167768465309665906113
dp=905074498052346904643025132879518330691925174573054004621877253318682675055421970943552016695528560364834446303196939207056642927148093290374440210503657 c=140423670976252696807533673586209400575664282100684119784203527124521188996403826597436883766041879067494280957410201958935737360380801845453829293997433414188838725751796261702622028587211560353362847191060306578510511380965162133472698713063592621028959167072781482562673683090590521214218071160287665180751\ for x inrange(1, e): if(e*dp%x==1): p=(e*dp-1)//x+1 if(n%p!=0): continue q=n//p phi=(p-1)*(q-1) d=gmpy2.invert(e, phi) m=pow(c, d, n) print(hex(m)) print(libnum.n2s(int(m)))
结果:
五、 已知n、c、e 求m。(n很大,e很小)
5.1 原理
1、由于
e很小,n很大, 有可能小于n,此时 c=m^e
2、当
时,可以对k进行爆破
5.2 例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
from gmpy2 import iroot import libnum e = 0x3 c = 2776571135646565181849912433877522437622755332262910824866791711 n=85793694792655420934945863688968944466300304898903354212780512650924132933351787673979641944071634528676901506049360194331553838080226562532784448832916022442020751986591703547743056267118831445759258041047213294368605599719242059474324548598203039032847591828382166845797857139844445858881218318006747115157
#利用中国剩余定理求解同余方程,aList:余数,mList:模数 defCRT(cList, nList): M = 1 for i in nList: M = M * i #计算M = n1*n2*n3 #print(M) x = 0 for i inrange(len(nList)): Mi = M // nList[i] #计算Mi Mi_inverse = gmpy2.invert(Mi, nList[i]) #计算Mi的逆元 x += cList[i] * Mi * Mi_inverse #构造x各项 x = x % M return x
if __name__ == "__main__": #========== n c ========== n1 = "331310324212000030020214312244232222400142410423413104441140203003243002104333214202031202212403400220031202142322434104143104244241214204444443323000244130122022422310201104411044030113302323014101331214303223312402430402404413033243132101010422240133122211400434023222214231402403403200012221023341333340042343122302113410210110221233241303024431330001303404020104442443120130000334110042432010203401440404010003442001223042211442001413004" c1 = "310020004234033304244200421414413320341301002123030311202340222410301423440312412440240244110200112141140201224032402232131204213012303204422003300004011434102141321223311243242010014140422411342304322201241112402132203101131221223004022003120002110230023341143201404311340311134230140231412201333333142402423134333211302102413111111424430032440123340034044314223400401224111323000242234420441240411021023100222003123214343030122032301042243" n2 = "302240000040421410144422133334143140011011044322223144412002220243001141141114123223331331304421113021231204322233120121444434210041232214144413244434424302311222143224402302432102242132244032010020113224011121043232143221203424243134044314022212024343100042342002432331144300214212414033414120004344211330224020301223033334324244031204240122301242232011303211220044222411134403012132420311110302442344021122101224411230002203344140143044114" c2 = "112200203404013430330214124004404423210041321043000303233141423344144222343401042200334033203124030011440014210112103234440312134032123400444344144233020130110134042102220302002413321102022414130443041144240310121020100310104334204234412411424420321211112232031121330310333414423433343322024400121200333330432223421433344122023012440013041401423202210124024431040013414313121123433424113113414422043330422002314144111134142044333404112240344" n3 = "332200324410041111434222123043121331442103233332422341041340412034230003314420311333101344231212130200312041044324431141033004333110021013020140020011222012300020041342040004002220210223122111314112124333211132230332124022423141214031303144444134403024420111423244424030030003340213032121303213343020401304243330001314023030121034113334404440421242240113103203013341231330004332040302440011324004130324034323430143102401440130242321424020323" c3 = "10013444120141130322433204124002242224332334011124210012440241402342100410331131441303242011002101323040403311120421304422222200324402244243322422444414043342130111111330022213203030324422101133032212042042243101434342203204121042113212104212423330331134311311114143200011240002111312122234340003403312040401043021433112031334324322123304112340014030132021432101130211241134422413442312013042141212003102211300321404043012124332013240431242" cList = [int(c1,5), int(c2,5), int(c3,5)] nList = [int(n1,5), int(n2,5), int(n3,5)] m_e = CRT(cList, nList) #计算m^e #print(m_e) for e inrange(1, 10): #遍历e求解 m, f = gmpy2.iroot(m_e, e) #m_e开e次根 print("加密指数e = %d:"%e) m = hex(m)[2:] iflen(m)%2 == 1: m = m + '0'#binascii.unhexlify()参数长度必须为偶数,因此做一下处理 flag = binascii.unhexlify(m) print(flag)
defhack_RSA(e,n): ''' Finds d knowing (e,n) applying the Wiener continued fraction attack ''' frac = ContinuedFractions.rational_to_contfrac(e, n) convergents = ContinuedFractions.convergents_from_contfrac(frac) for (k,d) in convergents: #check if d is actually the key if k!=0and (e*d-1)%k == 0: phi = (e*d-1)//k s = n - phi + 1 # check if the equation x^2 - s*x + n = 0 # has integer roots discr = s*s - 4*n if(discr>=0): t = Arithmetic.is_perfect_square(discr) if t!=-1and (s+t)%2==0: print("Hacked!") return d
# TEST functions
deftest_hack_RSA(): print("Testing Wiener Attack") times = 5 while(times>0): e,n,d = RSAvulnerableKeyGenerator.generateKeys(1024) print("(e,n) is (", e, ", ", n, ")") print("d = ", d) hacked_d = hack_RSA(e, n) if d == hacked_d: print("Hack WORKED!") else: print("Hack FAILED") print("d = ", d, ", hacked_d = ", hacked_d) print("-------------------------") times -= 1 defRSA(): # e = 543692319895782434793586873362429927694979810701836714789970907812484502410531778466160541800747280593649956771388714635910591027174563094783670038038010184716677689452322851994224499684261265932205144517234930255520680863639225944193081925826378155392210125821339725503707170148367775432197885080200905199759978521133059068268880934032358791127722994561887633750878103807550657534488433148655178897962564751738161286704558463757099712005140968975623690058829135 # n = 836627566032090527121140632018409744681773229395209292887236112065366141357802504651617810307617423900626216577416313395633967979093729729146808472187283672097414226162248255028374822667730942095319401316780150886857701380015637144123656111055773881542557503200322153966380830297951374202391216434278247679934469711771381749572937777892991364186158273504206025260342916835148914378411684678800808038832601224951586507845486535271925600310647409016210737881912119 e = 51999725233581619348238930320668315462087635295211755849675812266270026439521805156908952855288255992098479180003264827305694330542325533165867427898010879823017054891520626992724274019277478717788189662456052796449734904215067032681345261878977193341769514961038309763898052908572726913209883965288047452751 n = 68816697240190744603903822351423855593899797203703723038363240057913366227564780805815565183450516726498872118491739132110437976570592602837245705802946829337567674506561850972973663435358068441037127926802688722648016352967768929007662772115485020718202683004813042834036078650571763978066558718285783045969
d=hack_RSA(e,n) print("d=",d)
if __name__ == "__main__": #test_is_perfect_square() #print("-------------------------") RSA()