forked from RsaCtfTool/RsaCtfTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rsa_Auto.py
449 lines (428 loc) · 47.9 KB
/
Rsa_Auto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math,os
from decimal import *
from Crypto.PublicKey import RSA
from cursesmenu import *
from cursesmenu.items import *
import base64
from RsaCtfTool import *
import sys
from libnum import *
import gmpy
class Rsa_Auto():
def __init__(self):
pass
def _ShareModulus(self):
n = raw_input("请输入n:")
e1 = raw_input("请输入e1:")
c1 = raw_input("请输入c1:")
e2 = raw_input("请输入e2:")
c2 = raw_input("请输入c2:")
n = long(n)
e1 = long(e1)
e2 = long(e2)
c1 = long(c1)
c2 = long(c2)
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
s = egcd(e1, e2)
s1 = s[1]
s2 = s[2]
if s1 < 0:
s1 = - s1
c1 = modinv(c1, n)
elif s2 < 0:
s2 = - s2
c2 = modinv(c2, n)
m = (pow(c1, s1, n) * pow(c2, s2, n)) % n
print('{:x}'.format(m).decode('hex'))
raw_input("执行完毕,按任意键继续...")
def _e_is_65537(self):
def gcd(a, b):
if a < b:
a, b = b, a
while b != 0:
temp = a % b
a = b
b = temp
return a
the_p = 0
sessions = [
{
'n': 15332676608133432625865663467368919190850421373436872305238209486567746306752693183264062035640942650946758828528633231448998226188050742252550396688107155768402514304812870618950310974715588438082262348588705819097619132031775955087577747968993707499785025621378284551249401203165365487195742896418377659136293408431965728503802599736730660359459979028805108779949657406316632543793700807613053907758368854604813354833403677513823299142365314208498794841398346962238804417189013992218899491080330246745978774918990177368729345887251424126757866316961965483878635561579423904952486373719573475236627480658129994438113,
'e': 65537,
'c': 9966756770634564805207080238464398098497554088633805972648796894313669709745247366990369397633724490124779823794545535285145073953266673995245123140879654600247639058784528693656607116365939011445188653264524715175933655912457364554079091025504872690606720077006112010464987425352281034331735696096543034394336535006042954118071823331336288473434485267035366220066293572407588851108886395032725297946259551955435828919682432239387206962172592375408350481734674319052510567110292363010540720538646849102046350875461467038404700195985909071230603383702474652290250173348421615719110085512899553383626863443684486669405},
{
'n': 25410926500717115654275512995154860887554249055793477165321020914321378452889834515771353106618105785893904367926878989908608463323191121312910031698081057339005218436571814093932736408491929329079924007373742338779761588510818067577557611476845149828191734959563648309121470244133561975615563775643621432990194972193100318348291869405537806493699232939254716565069180900671557336768073969541747133886842330993127205896202102768011808928700677142526964447508063711360218894757799478377311772557178522106564040804244884470036065364346068125140557970765296262994997188128075660635189226926562629623806584461900164567529,
'e': 65537,
'c': 22371951190641601846958499365767437161740482339128459641600814765482625378664540842210206460452558705087834835657618130939426792724160263833222390474417995263521216412769741153689326761528557383921152644250056200200676015896053177857822666648866607120248818610120319265797913645437718546997996616815240528164798992577917339932916715877069580625072346157323449717734141140139205046585395386599166409444674353754882012134547131342107218599788271634005451682876752301938259459777608777294163783189249969185333622930884415716203968694303305103559984159416540876169508185172011938795450761855310225469696821486406761694359},
{
'n': 12239696479826555850597129927109919770091833609035388558468962118369586890134900361517982464393165680292077196991042167565195975428950246800641539046081533183761998702650561350988062145690386097289162514459679827022484332609714713692165867832484515781795647253415988531256799142790715353319778938102559743472780390436142137708101422903491964521660416603221562800772603874892386067425778365232052637519536818199905400819222339133682069241581706681944868057370900432115619225463624820882874150156763530362412807283114218149273746644874857008823831150403060427288092510268070187746470996527946022870595653082237295587897,
'e': 65537,
'c': 9722528460240943962136322404826078003388946337201452778351463162471297362543161290046212882603862561359959703824745017894635742978744169795573529378883236683156403066851475483700815273172052724279267429325050130758784223362194883939024170230577608970263137818807296524256295110029877517649300458997287037116717959648875154043669637931436030098527935251663094260211007898169975284315002242226368722250472604288032938739263554842859610776189962292394466957556568973289580585854576103092702146294529215048830935454907534925287709416230701444857736588760829453299282599056313705492399583914242198679408885001018788449267},
{
'n': 16568963625115149817406659108601857854691242755098874444083256114602586540153926029037458831508921325361480793676942073622488081260859285139986182099027171051518626225216914163727744687411738025237195281762506610410581566693998591497857229932692722243696840947009543068038022303913347454877567645968486756405154033458306629738838858527176917828882381517379117474531439928266911357978583761598636288781124334068782514153531953370610643074664373255672288942946554495224815602250694958136541498455442985378486729016257682762545050621455875313991976489340012737439855665053621571178170568483034953905966727203454742618257,
'e': 65537,
'c': 6218160328815307979336829238126237700981116945933214274808497827021720396007930526406201509244529039612859967491650623302511633865480575395364793364023896835558510627507444624862536575096738500064848215822214785444896005783325787610036342194424693650466484294629006816256053496732490728761896872455819337061269769365324988825657085238735126552993106639489290572517845816645695174950483831669067224593440794058772448615338305322442348657923242251803994552713000849492611363255359217482303917054068189069077420343008473503918241368078444540591906992655825727442248156483854608076008272625006262807673133441031230615037},
{
'n': 25083093660132910084370485150053883361604355866643213218859785008076644573183623472096733331684248133859890402961560753758388646869120748903363006537232617782347760832463874759709100497932543490706228380305027764503572395880073380775447498606755957865762433641157267154426618433094891829783110080529127746092018956576270651883826321867801688576863636016145300735643327433089262711681096041006560199788015169952625523352582034203637708063411918109248360206589990520179002555906276684356528284366146207037106616043295217486857152410371256013229923085634300651906751519766716847336646099804011810170550232618494187512361,
'e': 65537,
'c': 23988486392988892122647673770255585214657281833261187003147827364366217384967308229406244268441799527962120972621293862293863007903367814918791029047096304121373553213484597420708811900532017895376266628050490685325284568952433392918362725286375008571434114385176152439021137583179872249434271429950218407242133038615004951724285785663148309469451126659582895442571200322004779147728002803151330799584885100356471550408195966868798298869589413845330143510490763130922323763788209629915348439831188697998992590658712239646715460765973134469085994818615458977773181134391418004147022688745118762229235860819862828843907},
{
'n': 18060204755946948673092425540861870494212183654045165201404773747373368384560872412823550186316342665130196234806262709923574408531262437657769453542183244961213734083004286495373264855992777949771970704363672765651908827940800481247224986222077705102846082277749050052975217994658103146912037932993023758624287429014461956471853631960708220980646493586330745766311620529460879262010608047333156532069232122864142744405436210344624228443851499175105893471515421614623026390435572000514033194701767132552529193222225003749799105382501512030280815743179375481488234131689266390234393787496920852943827151352506552039657,
'e': 65537,
'c': 13246454157184599519619288091858012519836637647962574384289742050627651947870369305963550847136980287054399183351390955485593494702224957272532997214941284778560806368887634952025171279727809504248394766119104794834060439030484000221915070903348068861552452027218366802462804217208518911536128880068500775996839856562893405090550343486483766621228757797515666926153485726363576953340079936900616111772822906952059309320217235382149882984212853947351820383294723454852371347127203237402299215085141684496839150246179371524880141344709830347049751058719837224119830780062629897267764317721288976840036957120101112347506},
{
'n': 14157039136270939286083998215064324037051900590207494775859528748592499819780938000544538194358563665085153201971754392702602094570885804002737882977731237246329924156905912835315703391840706751663093288012201850245479250172920460425635253604803831820360567131520583909058479623050406124995625920354476939232561271022862966946802288812366578603585181571099283515891689343253430008013939537559660911673255006076190884711369935740992063410741799163748198408791325442711753608377102265379144593749838762789372562690604114686677332423008527985626462716263479624433804214083720205408084173484515543379201415409751069940521,
'e': 65537,
'c': 2462375947072145322473785567286110821998978327729039819931873088834571836487400893454076722018076734286029411212809135688889759299191080415157458612917622369257933472733771727715870237557940511719860362310638697900387401569165500523377457346964061505387580987562133278319443754005989142396070540396113721924407544425477881548293465430562852741941163719813060387575967328553568244532795008520235446046331976326268733969710395151898307605449143618211205472038853947463275079316191649171148447614584184555719716912497782410048889737117672703315727942278756722508106634149687416144352224221685820950189307871203512920681},
{
'n': 22707696378714966537152228462102512633752537801888441022944001762474357378830079832412416445881204818932836207630931126043746134122391017008682134473394311155471553606903798583262463930202573447724327418465373614994994531591412641592274013386929936167926500931405027623155808250324390412270509495456782620714841352177722814413984581495947520678360256309109512176982200541583313078568386903205832040126902976260101184384204053171782587714229964686597150765288454704281451033455632433588049013342348744464058061673220590991248218643411177527216800943702905206249378399131118767159864906587165904318009467395765392515401,
'e': 65537,
'c': 19000281806979152777599836861595053886467746845226548909869228929626525471604904093111449361928610639747459429976934078268218344923121344255646863262906926245063080989087820165411591417976418436170260579604464892689415579400805545989040025994948249844709986579880175509825437352151444432361090070516511852434969598628528796712795294711110700593240435187663709364321815168890985795458605284194802631074952238969818647925121044814804838252533888375754472512346766757468794883544483510087425701460988846318003165507086986834379545128390844383502186405783683503108818089215156354191196748316456726409288771035678350067893},
{
'n': 16038907921619647598234576197211537270206938161341246252697209209537811613342511878305726096934559362744831022406925637300559537925325668901570151805589869766164343240960539420866314761045794623854662541716026998026669731704952901512241918087875887201258197808606465141814428817275572319819200480736444149379677624584659701523157412024523447474792010909124682367393081431809316390747138420180856831760226412011895238821268884943238140806059846739031694244444553443887588365645576076841704342817266451094744991896523943326456981643310194854300698409838455225478412998466045296946940432503328046299692186105975608992329,
'e': 65537,
'c': 1751195617390969142391878927029637756379596539833975333398400723204357928455496958509661907885488725831221420863841094216307005627817038178750781638797466417002493896287033945378723165118238618746721325664581203917210196924899218051317714416888069915882238065853835881645587797752868924802292603531092053599675923848433095235280749875961903353582404153047843732387429696840772975709072567238726138826670946895431762984936674230364778990401605385191464655215131514254814133453621665092869972128411907320274845644489979522253803625488861982245344670594626872336853404534490561840780665846519183077025753798541761037932},
{
'n': 28585012190029156754079392763217925702277985447071330604510526715562571786638927925055738667617896634774239665223956980795001921162256083460036249881726998592634281455586060562523284911933364163423745508172096998672606661344595053330342953275851805840846979875290158067896828846643526777380270384818971502073554127284443931047623065253454002254885592844407626669341384188447149069044672719655226861206921144623725030249081825235006359727530056701501999179565005179477700560755413654425564058627159568891846486681383261551986127338095130790607486683452931879774380574319416874551871326375731490245653548603614429993497,
'e': 65537,
'c': 26607718758031674441310108935218388919604381887270331091167693146390123649606832480524490676697763202978589970359109587214846061986792669547721972765572991456045696984571212777964658515206973474094185140497015323328831578279199069525837611250340376196973055393736979898381426401989862363705419081411692333244459311744212155007499975213054125349108425364175229799877768661182867929867588766675576273460195368561330666354051626508024861376483327676343385538058765754028338457941328154401033457596337179225838134450430540859445747723847675356380715997735746088147500709855642474358944021230895899876097193068311738491980}]
for it in sessions:
for ot in sessions:
p = gcd(it['n'], ot['n'])
if p != it['n'] and p != ot['n'] and p != 1:
print(it['n'], ot['n'])
print('p:', p)
the_p = p
print('the_p',p)
the_p = long(the_p)
for it in sessions:
print(4444444,it)
priv_key = PrivateKey(long(the_p), long(it['n']/the_p), long(it['e']), long(it['n']))
cipher = n2s(it['c'])
unciphered = priv_key.decrypt(cipher)
print("[+] Clear text : %s" % unciphered)
raw_input("执行完毕,按任意键继续...")
def _e_is_2(self):
publickey = raw_input("请输入公钥的绝对路经:")
key_data = open(publickey, 'rb').read()
key = RSA.importKey(key_data)
print("[*] n: " + str(key.n))
print("[*] e: " + str(key.e))
x = raw_input("密文格式:1.数字 2. 文件\n")
c = 0
if x == '1':
c = raw_input("请输入密文数字:")
elif x == '2':
pathen = raw_input("请输入密文绝对路经:")
f = open(pathen, 'r').read()
c = s2n(f)
print("[*]请分解 n提供pq: " + str(key.n))
p = raw_input("请输入p:")
q = raw_input("请输入q:")
p = int(p)
q = int(q)
n = p * q
r = pow(c, (p + 1) / 4, p)
s = pow(c, (q + 1) / 4, q)
a = gmpy.invert(p, q)
b = gmpy.invert(q, p)
x = (a * p * s + b * q * r) % n
y = (a * p * s - b * q * r) % n
print(n2s(x % n))
print(n2s((-x) % n))
print(n2s(y % n))
print(n2s((-y) % n))
raw_input("执行完毕,按任意键继续...")
def _hashad(self):
from struct import pack, unpack
import zlib
import gmpy
def my_parse_number(number):
string = "%x" % number
# if len(string) != 64:
# return ""
erg = []
while string != '':
erg = erg + [chr(int(string[:2], 16))]
string = string[2:]
return ''.join(erg)
def extended_gcd(a, b):
x, y = 0, 1
lastx, lasty = 1, 0
while b:
a, (q, b) = b, divmod(a, b)
x, lastx = lastx - q * x, x
y, lasty = lasty - q * y, y
return (lastx, lasty, a)
def chinese_remainder_theorem(items):
N = 1
for a, n in items:
N *= n
result = 0
for a, n in items:
m = N / n
r, s, d = extended_gcd(n, m)
if d != 1:
N = N / n
continue
# raise "Input not pairwise co-prime"
result += a * s * m
return result % N, N
sessions = [
{'e':17,
'n': 25492165341402870943193342194243878583550091830588300179983190484677536677783878770091771426745020581968485223806403482688708690916599646206863308796421682192123291604607235755107364431209342465647517951497018019170871611821471577647494718032136027446239385000016582048814012025961200909864909126220898470337474181564969111740549293849528764155308076160637626891491670481790680054394999454113154731685789729310781453533838696397873008408145290785550608424077599753426457559997966726766077860415133627965054965186926286998932683118114065698789972581403018027054849591593302510627959868541308908688742433116746685957413,
'c': 6666419060534882063445608631701254352058920974421822069727027885698959797948070412066328188514278918986722361742912500893728424745043197562231410354633167915385978833339701529722766937863212423943888214894271923064950171580353934178748023608466389268996754295631668202047874774966518214639701079489902676101992190471567121772228753020078695924075081945558620706927179092603126041395606016633822502612954885417984184184737630487502072507997213412593690188577996870365218951232207636278621674542750965270226225337180320970384418820119666789283194398074172939976925377054214851752639573115850958399373724855553234690869},
{'e':17,
'n': 21127940129465859529449290710465142401626099761120448822896227957140612250511186166751154941578458258054285578274511450860570540828102070111370941358023009904308008795394643585864471986075987747216086746123427597776886622933600026324537947907678164739969468650427560672262831484437166952755098875024996456298734488381416586155715216067643259554486571154930556222002298362539331180176023831109425436177078457326927485711697356496728959762158514131974253928871967648747022232011642161107225163919743383709749413596786170516661129884422397760268997322898954161891328103774133077184028190158020665184081778231669390621447,
'c': 3025342965063970983290961997148038673244905555308289391392896371605967574831912573039170667272658109340534212720805676884032065407092138739865486740854024452063900964324730683963944994250537553102328024132087660954040726648939149676030355851576763910670469390583200661923104138267998714166483656478619852365437776250801008285867722681155855008497404084448836122553221951674607943082229713841396761939215145379881979676835537964953725808825354819362797503568722134701601966504482232887162578922823026182148541889898696868501626860530484834082878781257554947276873624962134091380186322272011345017295000538738940807566},
{'e':17,
'n': 22563350736629808264395772922819808510705484822245634475903533464108273973359621048788502042378085143059458230242646589967392945320090987067961491654357416853755261074531634683509015394347409817614889849143844247148479574777870355411207636748917532445668322051861338052906797819332259215278552079454184458015646487703430124222965707626399501332317111449272813630361908462924849723439803107908568327484445685641242797481816740336203447956828486867818411976997494338407256234593013250778655132122780432402623365698921928370817670245911505312288465537126236680323692662919137486755722237256218345361985595174086335811073,
'c': 20135971165126329834498721501784512951499984648652660104821647741072871489936836442504197592142991669369919035304425516258391511301425226375564892415339365148509645261565560913559856659645387805198871549146172257882053281268795760344994001608956789940307816388486468042693545041073838774812997395437209060397149458050765177205158052575861667488415782153713633691372314235926019032628836133660245109281683465499593548858659962576728675714563027957143405441096816687188373864658407934159173393698714390810577718268693096568336071207238450039773940104963444157704166286892082172461258702049072827677476706096904316460964},
{'e':17,
'n': 19697677554331221267369641184167188448335213481430897452143918418367154993523756864868509098839783053011355139590418269119504715550443490754762988015861768839038067134509092125205376509678043881070558825637905431885254727168679069330416776166369184375970521838169214107508952264240335498312849019213361951991190975616614804433716772411184993774461040690673753098373736933084616647869336141707774024488317582512216162403209135110515778522772939206567850540704356683976987508523230996026019768603629874558741038975853009975048175519392495212857366577161641687502674304846608725983145033337187267988999492371626182417551,
'c': 14300174742950957133230867352717457897986205104543265825218842008467109972830138620119291505539500355334723376879888409251809364632635010202950500050892997355318105884900306620239365641681252935777362167024611054220484107153146560621083009306565476636270883588514007454668850327310521718849400741849368048026759782889674614052640379573508042637071620721869972662416840328750772187247907576009995766111841008793044576343182936834713229276992314777759684829492003814595802694592792453101320053268786720734738162720052909591108493186651733642823133001888727983206566194875945734575411295063088173363528901413605439659186},
{'e':17,
'n': 22840174333461757597258965063503187552495604936013453813620970124752854692025931772152482715744058240571638175460349150353012889988723597821978579743499713766809084692254297539180932689148176677208822562008650864838388158004011554059006621226560748426334342736035675518932164604927602370423087532589029111052023317750367568915149473019058579728528211621618516222534404936592529521724927756742450435643292165615017136698859157488474841102297647488008004924900405006989945846311096537399030509947027554824768198503677139485403433484337602107191422279674605462427176871354240310242684337649527314815937448142118014471869,
'c': 7269879938285087105060326068602691458154834670489063661854932312114567012381462325708324462473335735747751985833951832304054546004436096189761282474552769255043106817537407079708725883409397856635437608590617899532129476884853580275762892523609169600551500977086971018619327642914168273376084417345670925345416864822282780628646416374712921155883117431305185117831110048721693951865267141962666069350614792416053836803149161735632213430960384988586996269807413280397202373128437527421378900814241234271529460443131737298476049472736692048979564047934967259654010923397531158463241672904787724046355796318156683211693},
{'e':17,
'n': 26181530231990596208912553982832841408036769882768643576772592303571004331051012292548525104394717484252349998434031623438917061404178468743247357634872314865905132523154238118481540371615056806756673945897771283881615017345596477766231957968854622033499073650939380455320632540323169565251694316300843433445667057555206872698949047971459662093473609973167299463621536822302820363096764742010575821082948519766535657152613824870739004432505303862201505469354785224205708777422229564239113870271947768783809029625932248575272642464554827073657622048405963388795704112995209429712517330431946725366254154641804680636843,
'c': 6596498503473090384156122948918457693225694527543771961484972133896304927525029433085731957072789891646290597692421323936231128201822432631159176346646447011338805190482179145913035869942714613486895588754112366313438192851954663748505238329830016450238817308208066971617432717047936473721490905750275563930145038394216747562079905090073621918898708464447477835510214346908545028865672847041949340101722558962541612836311085196500521238354347015877914823677634283521373575663416167402249149782097385550791425270622816978948552969790013217066937461634555711434991830871731594480554761087920979267051612013063691897091},
{'e':17,
'n': 18375116136153390105382127878395045588105857107246922094470216545244337357987681718168448429849698069981688291927806366721855289976121993055091218569849787514394507275481429728206429948836923251296672175100798033529862498578044854162615314211321712132747044363074981129056925717085204066189560133707352709019771792017200539059204024262019592851381317279368957556414371834469907989489823310736060223258994989400405121984031058976855896631117913090748375198718936570595983429517914008170697730495897896134771484309035561040862223201385431761693343426402858632921946359721016131084165400212811848077899966364178485645209,
'c': 14450411368079644952604705871870024872670263251365527266862703716230778731191117713744962010138824834514424908701300856449356388617430510950535877723758382120583446269291649779277478125202353436896123871466945681583403794725306243733343462009791759823657466902546992816090954580754519785729524996046358307197773786618439603452776474869740548426942373102466687002398105444228516302695737600836706505822006007382861266883167162032588362091247091029707841718663522787461434574458986841049123553142254769053444081497788062775004085437066317446657655214417638414799100287660747244854718117257942197065271633812453504802118},
{'e':17,
'n': 18856599160001833299560082802925753595735945621023660831294740454109973698430284916320395522883536507135735383517926050963512440162483065097256884040938259092582892259657340825971260278387406398529168309426241530551396056450450728728601248269612166083300938497235910244979946020059799495231539400114422748104072550004260736766137354572252872437140063474603268146956570787143010441293268321641092743010805639953103578977668248726500636191043930770036787317928372179939360510179438436665591755940224156131460271763912868322774604558314812111335691108887319827579162188169744014973478052491398688611046800951698773893393,
'c': 9537600333774561965347809713893729543607050845183365456822389928272539309054686047543168922742589173859871008938711574330675462024831693796734766356934966684018899382045308313458440052017835494802986355673813464215184924837019853399388984893499187144821922554214143314640377079505897680854314880035458832414257859211958802973880776732222536460326269825765449639225220135735729106020282618914111685220841901015367575108920943148006106714487046602965253293770388951872741849791157534395777330833976209975828865170184061768114280467192574451716204110866562270737220078456700440400847759035001437357717203367869014507886},
{'e':17,
'n': 27658060678038715780470429570597987144542213875178081185638364125349217264266787337266356239252353691015124430930507236433817624156361120645134956689683794554169169254645287613480048966030722812191823753459580311585866523664171185580520752591976764843551787247552790540802105791272457516072210641470817920157370947681970410336005860197552073763981521526496955541778866864446616347452950889748333309771690509856724643918258831575902389005661750464296924818808365029037591660424882588976607197196824985084365272217072807601787578262208488572448451271800547820717066767396857464594301327160705353075064322975430897551911,
'c': 11433488612991990768536086698965180146550356348457563234735402111134701115830423042016221831657484325065472609147436229496479358788735270448637824809543880271526735635196884978639585020518147152207002685868984199742884443523231593245377292570809368330956970290791633106067116466080014631110596564728982066569618319541351401820732547227122970369299780366876340403436785218211729531092484723580223801525992510782266856454394478372421830988205823368541860973674259795969870252832216828042174346473447490557323038031625277043161510854825069681462499200978561823301487118145650943076528233694749306585201212677836363102350},
{'e':17,
'n': 22946690762018202432990887189358840679847544298138130593179782494313283235656708793278294817370704179431587028797820098265221065047094111469387707851755805572030960917620048531944164128853858185996116085402185608208505862268300069081634690281361741089802822427014733500818400628218120217701065238981427409232531524810373504821405505784194458324112411450898914981925380408035843501140943840375415504531028291728110941191868468403264579853022847273710668909241386840084506302813524686512443804790681324357699954553042087787664048340322301999798211625015533291431249576149925507273846582715275085720997988716732564925247,
'c': 22114155655659890875804525321139229042160702983880183010190613534988342335473532979092956071940843014846115113869717993343361412775761899123003515398037548495421246487341345482541093940292149812612966738017954607042366694074210348507360409383994155996445275183175086308877643260816760841714256410898269814908129813108551478627448054932601176813323889809069498060147451905095827442175810001123067159848367040168783825044279052630935945891964134230385303127654483691228319146427451587693985548002268250053416426728753739573823133295951972853182187229046285977320129680613905801285437766686995325893960172376926445625122},
{'e':17,
'n': 24196427886175544349183151853220199349004334700026598710073455177858086301574503667298550711896472441115488666905450709605686892168468755356865839253905196330957826522203721667534054840325504585224175556167326100374628995111103938603447308160978852917872087343333848311844292367100647648063686677699135973596041567307784602998888789211145238333237613511049640509935260207585999606022736214748154785163367146044878537220691272581381812904389596150391959046627047758159235384420979630615278721284429066540058110867810807414175942667916940934372027878979861244401198531405584211810881235860988295519048177492135610751359,
'c': 22296229997452473240404864600165019763565238056149885081619043661295445875270023158334098350603026725837611396891344057244652143898149482532106315521575872089742565470821567522946361662445964027209388657746191967435348044945947483621482721538186361050633595041353278781238956486769506373730884495108039610798833232749139421109269053404110580969203157360738738757856465985684697862079893042321411866940484105462672034009048091117594209570962168395344960105313171255329582011213721766675322731331965794652525786585751148977132545042466983433106614477836969990881581169493890539450115893703987635774477858244270608426284},
{'e':17,
'n': 24360507764722702236982664735328841420311376027120399241078325497875531961885056309904114723678048613901842270612025644417215018351512175660269184968149109110209563435463873211914594529847294730496535258882010197028842516927468678843924092706088182566726725380495130934316582041447010080540555792802432876873205758297773353354773400601269072565235285553745651262991130015382601486699000167692446155692416347390954126585612460075052988874628884097103408004610717498543307677173210168954439627061348158046928463114067813191846253937179490050784167037265795606665046543869952157800016132040847485597002954075447344679099,
'c': 2091608690891505258645949208946395204695041893228619053197065370886625848151228416356718762070352835427065857428605476603762875777761221509564315249418937215791628816353819422446178413024109351330646491084678357651148045128554746141683169277567849074245394149157710582616155738686711539953583528181614701953172972027101545253676978964454897005216320952879719740377602813279137554307680715749682175447559417772077472349519490785782899751615833622828868651966927604858524345638792811839384317780948838203430577334783095834911402913673850996431719159112716370014509918436690080118494876223156361387987999829094801210890},
{'e':17,
'n': 20991361604317638769781823650579915779070848318189821239839863416933546421097820271887796260918963289144354691357223973152195364244222103627141268032857145421152363842224398828703305592221616019678102879125957325674007444034541160527072712208844228077449624475289619712846717432293230773279322231288635231839457560272849165524911202814516841260722542666841002263011069065414760944278124011800036869640840312097012211808948108758865722731922057521488153991477824974687229720412552981052793827211890557079895492457992364345889589127536307112174252490433421772585442393590767593690124636778449673348970938028847369589017,
'c': 1176089662945377859394485590647428302230902074038834532187683920441006896963622996580792552532208518328678256998366963208028743204946660302538358531574765644007432906847438011315702121691613005078019506672007476291213237939507650616659336489497035998458925997634519034425449883103272960672562772204663037400144823802245320606039938019714906919958257594477982151857672916895153818922550365638890064326384677879791487276278839159022778493427572161764299809764447703117493013786755368150005501909964140795978310287946809348754988288402304994584145696550845106776916203635572397815715801968943287729470010695704992513284},
{'e':17,
'n': 23536517244565831304957548822169291652207666754083340160306298208076886665065064843803465469409961704414510544566548897045751559184520942970858445270483458341528037499088946028024476760938334724064432942414803725235245083424464022066205458380170453189950900307248048617678876232312945996520402058770976469558803007764420552804210174247648614671097046399148333316029580244153775977135880775921663886951776404864678251750085264792097984094013284691977749495264267344834078278955171634785277609298558886471141768976455150174601039402280137908104589898954351484608783306378026577123527889651575952772820003380025546341093,
'c': 2787570158312133794000264568955228362505478580337508384369847495327630702109418180218855630800765419691714135680571469213835542379448821209732447589306506369895651911135089075762137873889141680195343215032414145506957429427948190720862478335515596644926205381293106417410261520242246013085300698437878525197927303400003852408888917470597700285690342597850461091636279688711415205788329646890595805415907084665612141657880165938739423616058100283432682214722969192114128403177592988697839575450529126584549296274365360257579336454484779207708578834457262472129009056129647722165160424055970728939468425924742795247404},
{'e':17,
'n': 22418240669078782395650685709211154715963189697089263104799144782938556049238670824036504767273510569815649054522706833230147726835488621722481390416868271627116267103118088021697808807092496788890564153300558617457948161343658932652189856049810225397121264480931341652674373238956664385611007077903271999938666531870525476577337484325119704138317723687164049779101614572637615019989722168840820998326371954897496744184510021746784303032310300111064058399012572181069622479162656626630666689745901678446100898000876596201969536772169761074060718581442841244466714693693744538078059538322489346225247976911833748299831,
'c': 12741916458848035965674277226994440579915129055105462698845221890770558867890065973150125863687106455321192524282688850919026924493363241183706112792662939894532011809709158105688708409813198696055548036573709488504352009446189240170056000380659861236761237694775468132858916502459186242385986053427650331045218524574666652583364331482577973877822911369427036861116411565934292335204172142173748941863013032301778764886328258476195697934305418334527182261876183838908107190767800857337092015153451318295610430683371731834190637342265695335154669307530426675001781782217407160875559782488264585128426108095378548497650},
{'e':17,
'n': 23980852037830754961843810384842109198680213599160124627608485316452067979221814287579031744661623974638029208789156178351485676131492416093947908129410491693261731404401473508295054358751129977958761373804386612048163148740071525591509234298601816819182717519183021983010112798883286143566969631969002952806398266165159329990930567027893909276078886841121244401773915309649928992691723658667402744292184927500507621943557420540056270730461740897601661594495028880708798531876866777269713984635553934853524715534003465120928659881608776745499441872048908380726942161563974610455635903901327036597201663594811207962219,
'c': 6500753041357449855790208700753809194331642408448330624975108109751859721113916040913210608530117147284032825775613945416295553599499663257233673509345038912634968979540222246220460441613513695959331196716737175735741187191935319073270722811433035516074289343637773607521789970734447289501012767670625043452129332667983332570596510412510389935486708833483704894688513193020406791505432203906745860054901025547265075193156070642716676601268698224891768784422675343094235950805128869705790800102590177489342374151286188584127528879203511712056047898651580309669143955726928687269246843562481439718815596302692298516533},
{'e':17,
'n': 20692935323456318857339636506121661409781600706918188791579569287363925379425076174758120076604134447812506062875425652061813752876711066070389233324351070782264123950482732919402628782987702034544299244918439112034234610691454353578647793100554890386226509128280993176780063155185886470116528236906519407444238424523493151082789442134440461004320839591569157239343299865825958454617581990948186838151424941117137818356712476559230872711658730923799079308488965817006179693410669320825141974869847267757553830878658559853061666130127729833856270147578804267991192625989184535593836836877723580138193263333297569871679,
'c': 3149880252209031344712912478150309339789690396685866282400905925814536568350616764434288908486968492252351255022161340923513529850192573780350157980373627989388396346845656970516353356298790654832486310492079962338672745285854665147406015543850517102001543987674241995267846019477920439984122318278402925428057397039652213642437095694526592410641576952281061504190238132151689288080423425867830015832927809704559212420233819682781217618789216812964682441589327621971863380979777355679805008775337364919278947618188598427198109821307637144473819840409743353235834034795849314936322245339378447572244022235758755318197},
{'e':17,
'n': 22182114562385985868993176463839749402849876738564142471647983947408274900941377521795379832791801082248237432130658027011388009638587979450937703029168222842849801985646044116463703409531938580410511097238939431284352109949200312466658018635489121157805030775386698514705824737070792739967925773549468095396944503293347398507980924747059180705269064441084577177316227162712249300900490014519213102070911105044792363935553422311683947941027846793608299170467483012199132849683112640658915359398437290872795783350944147546342693285520002760411554647284259473777888584007026980376463757296179071968120796742375210877789,
'c': 16279989825796156802547712583633798298339885806436476356497796047229021124610630851145390462136716298450342674217575569196543919171078736243934669612002081179432014315465408489156509075853163302037413161239227506465361288161349464334942343016752593577869091378915378775289174914088843177811858916838452126433042585745992994815140378356718198629815818784659700720051966660786554920296721138381405607252098516473395177368859057746002895683463397630717358844874557866924382768279446164993249061647243362602307728771126094369384362246840728274885512559106673543101063171808938427459691773785819336680808200730383909434687},
{'e':17,
'n': 21996468204721630460566169654781925102402634427772676287751800587544894952838038401189546149401344752771866376882226876072201426041697882026653772987648569053238451992877808811034545463363146057879646485465730317977739706776287970278094261290398668538232727000322458605289913900919015380904209692398479885177984131014170652915222062267448446642158394150657058846328033404309210836219241651882903083719822769947131283541299760283547938795574020478852839044803553093825730447126796668238131579735916546235889726257184058908852902241422169929720898025622336508382492878690496154797198800699611812166851455110635853297883,
'c': 18919626944919001746434424216626233185707755539125451453498872012547818089480281584466165481221804279035723497497262166426771134459922980759121024951390547522550510734782168869364945944106404378597344695227319394862905382593128694330512816879360904284025740295670026092938765777154761403682710878968080966485091090198109371223563090698724814081281842426885356314840392128767155541449360105460941384985904101420698734690361896580259169275867969219822351776569430617405034594385605780084981880407657528516176335362438505224655581522137130777263875034088903829280901936110414932437809315770012018024957989707333743935907}]
data = []
for session in sessions:
e = session['e']
n = session['n']
msg = session['c']
data = data + [(msg, n)]
print("-" * 80)
print("Please wait, performing CRT")
x, n = chinese_remainder_theorem(data)
e = session['e']
realnum = gmpy.mpz(x).root(e)[0].digits()
print(my_parse_number(int(realnum)))
raw_input("执行完毕,按任意键继续...")
def _brup_e(self):
publickey = raw_input("请输入公钥的绝对路经:")
key_data = open(publickey, 'rb').read()
key = RSA.importKey(key_data)
print("[*] n: " + str(key.n))
print("[*] e: " + str(key.e))
x = raw_input("密文格式:1.数字 2. 文件\n")
c = 0
if x == '1':
c = raw_input("请输入密文数字:")
elif x == '2':
pathen = raw_input("请输入密文绝对路经:")
f = open(pathen, 'r').read()
c = s2n(f)
i = 0
while i < 10:
a = gmpy.root(c, key.e)
print(n2s(a[0]))
c = a[0]
i += 1
raw_input("执行完毕,按任意键继续...")
def _Knowing_the_publickey(self):
"""
知道公钥
:return:
"""
publickey = raw_input("请输入公钥的绝对路经:")
key_data = open(publickey,'rb').read()
key = RSA.importKey(key_data)
print("[*] n: " + str(key.n))
print("[*] e: " + str(key.e))
print("请分解n输入p和q,大数使用sage,pq相近使用yafu,运行后执行factor(n)")
p = raw_input("请输入p:")
q = raw_input("请输入q:")
e = key.e
x = raw_input("密文格式:1.数字 2. 文件 3. base64\n")
if x == '1':
num = raw_input("请输入密文数字:")
print("解密命令")
cmd = "python RsaCtfTool.py -p {} -q {} -e {} --unciphernum {}".format(p,q,e,num)
print(cmd)
os.system(cmd)
raw_input("执行完毕,按任意键继续...")
elif x == '2':
pathnum = raw_input("请输入密文绝对路经:")
print("解密命令")
cmd = "python RsaCtfTool.py -p {} -q {} -e {} --uncipher {}".format(p,q,e,pathnum)
print(cmd)
# os.system(cmd)
raw_input("执行完毕,按任意键继续...")
elif x == '3':
numbase64 = raw_input("请输入密文base64:")
print("解密命令")
cmd = "python RsaCtfTool.py -p {} -q {} -e {} --uncipher {}".format(p,q,e,base64.b64decode(numbase64))
print(cmd)
os.system(cmd)
raw_input("执行完毕,按任意键继续...")
else:
print('没有这个选项....over\n')
raw_input("执行完毕,按任意键继续...")
def _Knowing_the_pqe(self):
"""
知道pqe
openssl rsautl -decrypt -in rsababy.en -inkey rsababy.key -out flag.txt
针对oaep情况
openssl rsautl -oaep -decrypt -in rsababy.en -inkey rsababy.key -out flag.txt
:return:
"""
p = raw_input("请输入p:")
q = raw_input("请输入q:")
e = raw_input("请输入e:")
x = raw_input("密文格式:1.数字 2. 文件 3. base64 4.生成私钥(解密oaep加密)\n")
if x == '1':
num = raw_input("请输入密文数字:")
print("解密命令")
cmd = "python RsaCtfTool.py -p {} -q {} -e {} --unciphernum {}".format(p,q,e,num)
print(cmd)
os.system(cmd)
raw_input("执行完毕,按任意键继续...")
elif x == '2':
pathnum = raw_input("请输入密文绝对路经:")
print("解密命令")
cmd = "python RsaCtfTool.py -p {} -q {} -e {} --uncipher {}".format(p,q,e,pathnum)
print(cmd)
# os.system(cmd)
raw_input("执行完毕,按任意键继续...")
elif x == '3':
numbase64 = raw_input("请输入密文base64:")
print("解密命令")
cmd = "python RsaCtfTool.py -p {} -q {} -e {} --uncipher {}".format(p,q,e,base64.b64decode(numbase64))
print(cmd)
os.system(cmd)
raw_input("执行完毕,按任意键继续...")
elif x == '4':
pathpri = raw_input("请输入秘钥输出路经:")
print("解密命令")
cmd = "python RsaCtfTool.py -p {} -q {} -e {} --private > {}".format(p,q,e,pathpri)
print(cmd)
os.system(cmd)
pathen = raw_input("请输入密文的绝对路经使用openssl解密oaep格式加密(输入x退出):")
cmd = "openssl rsautl -oaep -decrypt -in {} -inkey {}".format(pathen, pathpri )
print(cmd)
os.system(cmd)
raw_input("执行完毕,按任意键继续...")
else:
print('没有这个选项....over\n')
raw_input("执行完毕,按任意键继续...")
def _Knowing_the_private_key(self):
"""
知道私钥,直接解密
openssl rsautl -decrypt -in rsababy.en -inkey rsababy.key -out flag.txt
:return:
"""
keypath = raw_input("请输入秘钥绝对路经:")
encpath = raw_input("请输入密文绝对路经:")
print("解密命令")
cmd = "openssl rsautl -decrypt -in {} -inkey {}".format(encpath, keypath)
print(cmd)
os.system(cmd)
raw_input("执行完毕,按任意键继续...")
def run(self):
x = raw_input("""
------rsa工具-----
1.我有秘钥
2.我有公钥
3.用pqe解密或生成私钥(解密oaep)
4.e比较小(3,5开方)
5.hashad攻击(1组me多组nc)其中e比较小,低加密指数广播攻击
6.e=2 robin RSA
7.hashad攻击(1组me多组nc)其中e比较大,e=65537 求公因数,公因数即为p
8. 1组m n 2个或者多个e c ,共模攻击
------请选择-----
""")
if x == '0':
self._rsa_ctf_tools()
elif x == '1':
self._Knowing_the_private_key()
elif x == '2':
self._Knowing_the_publickey()
elif x == '3':
self._Knowing_the_pqe()
elif x == '4':
self._brup_e()
elif x == '5':
self._hashad()
elif x == '6':
self._e_is_2()
elif x == '7':
self._e_is_65537()
elif x == '8':
self._ShareModulus()
else:
print('没有这个选项....over\n')
raw_input("执行完毕,按任意键继续...")
if __name__ == '__main__':
r = Rsa_Auto()
r.run()
# f = open('/Users/passion/Downloads/RSA2_Robin/pubkey.pem', 'r')
# c = f.read()
# c = s2n(c)
# print(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(n2s(x % n))
# print(n2s((-x) % n))
# print(n2s(y % n))
# print(n2s((-y) % n))