河南省第五届金盾信安杯

金盾杯

我看看谁还不会RSA

题目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import gmpy2
import secret

m = secret.flag
p=8666789885346075954502743436174521501697
q=2449101960789395782044494299423558347143
n = p*q
phin = (p-1)*(q-1)
e=37777
d = gmpy2.invert(e,phin)
c = pow(m,d,n)

print(c)
# c = 8232151627233115772131180151146951323147507324390914513031444555762539986162650

题解

题目用d加密,那么解密需要使用e

1
2
3
4
5
6
7
8
9
10
11
12
13
from Crypto.Util.number import *
import gmpy2

p = 8666789885346075954502743436174521501697
q = 2449101960789395782044494299423558347143
e = 37777
c = 8232151627233115772131180151146951323147507324390914513031444555762539986162650
n = p * q
phi = (p - 1) * (q - 1)
print(gmpy2.gcd(e, phi))
d = gmpy2.invert(e, phi)
m = pow(c, e, n)
print(long_to_bytes(int(m)))

hakiehs

题目

题解

解压文件后,更改后缀名为zip,然后在里面找信息,找到一个图片,类似图形编码,在

百度识图找对应的密码表

Font

题目

题解

百度查到,这个图片相关的英语句子

然后,字符与字母进行替换

上面三十五个,中间英文也是三十五个,一一替换,后面的符号翻译后就是flag

babyrsa

题目

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
#! /usr/bin/env python
from libnum import *
from Crypto.Util.number import getPrime, bytes_to_long, long_to_bytes
from secret import p1, q1, p2, q2, e, d, flag, N
from random import randint

def round1(m):
e = 10
N = open('round1.txt', 'r').read()
N = eval(N)
c = []
for n in N:
c.append(pow(m, e, n))
FILE = open('round1.txt', 'a+')
FILE.write('#' * 100 + '\n')
FILE.write(str(c)+'\n')
FILE.close()
return c, N

def round2(m, n):
e1 = 65537
e2 = 84731
c1 = pow(m, e1, n)
c2 = pow(m, e2, n)
c = [c1, c2]
open('round2.txt', 'w').write(str(c) + '\n')


def round3(m, n):
assert p1 * q1 == n
rand = randint(1, n - 1)
test_enc = pow(rand, e, n)
assert pow(test_enc, d, n) == rand
key = e * d
new_e = 0x10001
c = pow(m, new_e, n)
f = open('round3.txt', 'w')
f.write(str(key) + '\n')
f.write(str(c) + '\n')
f.close()

def round4(m, n):
p, q = p2, q2
e = 65537
c = pow(m, e, n)
f = open('round4.txt', 'w')
f.write(str(c) + '\n')
f.write(str(pow(p + q, 2019, n)) + '\n')
f.write(str(pow(p + 2019, q, n)) + '\n')
f.close()

n1, n2, n3, n4 = N
print 'n1 =', n1
# n1 = 11177704342647691670070109831808378482821379302566268464943167206480241387551242120970561253786504223799891058773501288553907233379501460184102021531940602831324488818584481982166938165369946708273616214479706319082928159418133324715707808391767322038274030554057692976849636562898606740933056248870247255267420120488136713203337167713048960270135675134979523980774070773134748108563438318851158022122836766753248988837577239024669497444315476147384635493684855045085484618938995056978709390818568630471778630936743950045419782341697342117812959693992029294299582179507853091511833090186389981102903683178276414062363

round1(n2)
round2(n3, n2)
round3(n4, n3)
round4(bytes_to_long(flag), n4)

题解

这道题就是套了很多个RSA的考点

Round1是低加密广播指数攻击

Round2是共模攻击

Round3相当于已知k*phi,直接求d就可以

Round4原题,数学推导CTF-RSA 数论解题记一:2019-AceBear-babyRSA_ctf crypto babyrsa p = getprime(512)-CSDN博客

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
from Crypto.Util.number import *
import gmpy2

e = 10
N = [
18431707175796628027225210155764866137060589315850638917972353197871917045314474893373874146167401473640496246918625073265314997076179084530975296256697950503808356831143505633589397070754757130347166337448241649490343632413194818305304282134652935734618351907444876747782412498406589594074346525589905804627592138765992193075504467772493903710487629594988418988816461730614064472246359730211736860698588935743474739456810313578221344149212967580031412892687641217848936661879291425099878596289403526996814326634014199800848745187073801631429637963652883750120648966862626291870121641208420678002017364509249530764747,
14145693897141879525840664438283759821358469464703980311150286197774061655269643593339816338763581126036204650731015715608716649019120247894671819635572639702297628616505328154665608694761993104253433191662257428651230564621033703840374238283098946482496825723935809252603374188301371026031934804191647040480124274766742473995602296423800386148472679984304299323087549910890044334019073640081344033591196347814849923627262807145570123585406584120926847557042642632328080411777972450857396733507594125111544313786964421300628357394817603934472199493084525674623520392865603979693428355417558604577825474441676237227989,
18328366459806542970414782482522644251249973175412184918375187368337857388595074542418558924363449997442308642240518237481809307628057494582393372506022132819010410242686193550894774354189598629653504850289550400201580184032079513787238377699965750487292287032818746740503301354763211450701594032787858045479916391020898664567510418276797066083978957230786055406099955808855106919520134292791463990872298332368374281605285909673736503878906456860129802112339080032529021421969681477406950090927077476710012056152567501270798287810734377603580314062576510971518789736851256880848125729522562004210454550396947353002701,
13892173543265481156393614877743252091356523279451308452465139372725756834054298367030770852538169250461439607972921489028017751247776732769299792365279897040092034862285973205241117128445355230064598554222229058728934495880925342044908394815983059524515035206034579476641220560684300201174679913290770019257152099999763276092315187522984804635406861398895193116724034542845277267529141307790094608880447818537827089313922581190239356889374653905348537823660164130577055741013309127682348014185126678973112236569713425273275851542251529280114647378998420423599173718363406954180691079491158009860097177651883840800913,
25931944327275440730121825142491688732627507217913274133911000058167400322827696441827216482747141105489071698339457914271959956530795163550510723817793534813405331152070267239674275367230158331538774113796440325313992662361896765191331324662661827820496779628497733947801048583293586303580427016629670770252325328322089089912620710817509613807258913428398368326489530988878767135277493930774374325520096314937969266749192390063918924563915355875877816069337019504543128036385832955481211426939237202661603847686261981779526202370645412102947380979822328218557522653212917811230672495894220355353293435974934373825033,
13391542785399588708552829351210087956883641515125074042315304426334156657809872464461572502519014425431002866370861764361547253594934246724197023038133352663375284808974862879628200451461121894930472443487305185448881168513908728268542974475122520261418449080070157249580715244954627776261095936527253658761584956735671709315908555724747673781562228441847181305817223487014998214582335865933252790301366210664498155005571480420602603872976422643029682851844778031186807453058822054122320713715531727950649638750053718553039173610812548836568077645531707638550227096040110178331680717826940857097667991518031143405763,
10661018216746797695429667637804127203646725591681531214833699319837662949962722180602886697745758643953793990211981205526249582255994944299648299338598281473820029194803720384507115456043923903572886866927376455821345630106061030933153596945142351989098427709979282970446919206844507317523824809023155570327758095044614918388469588423746283935026797928483619047653091058043551159094124906196869937781345500455791985391903225882391469645663039353147572357898752838567832277312831438100629652618942965076463460084761833327185156379936150490183724468369454672313401905127421894766632740502368701642304689675925913800689,
20182633671997423422044190079209748731081408253005472727551564302784434145531222554600250888633641995570056959927861819078132615720740826975764374044107623720274192644754703361346647256210234707921461637319620855947170722674904425111045253430882932275941597333590835687005738986496138711730523217263273332300211339450726197960894778412937640818291034516829493327106212351149422090761822762735298949423202597284105028287801170515124918642457618515457660308083304635495040247724410756402281626521868165640635825503602594580410998529905168924520831619837972805243173317414675537009221104531444141335545635717092578343067,
13205196037947478850072678723637223374897520216040790960690604286507245756016494229672162090858195986055621775812019911502613487238508741496207514842396894486677755906669059694383608173990783920788992415796711063868616100011307206665572940725739455694717000698475210290616112460519476241741168526434204917335279743948425211005603913709320381020138044523013150451791065934176219432979780086465120351288759122845721145032265981521404177225738585608603778529034325326146476086361192076333726188249655092325997508830257829218302337471014540319258518871704369481869652919101970587648039911862443949292645073871689207120159,
18993679737848730442620443987506536429982010735750938039274000248259118061145378721879970359848926346175920410935274458679237508661470243965270242631798851514473739876613630171641814275071989422018403731080655190186568816359202637834416458445102373726212841484304653389813806819452320807858071660593900910808698908406798285354102321940949300732791199455246799294078277813353547519034405982868709751692213745873376021404586308318591527103628122106362208713303329550731153425976273750441439271390869384451913308714632104445685584508607411462213533554937064183339589227513547797298018163848249580661121338058267313438241]
c = [
18332360039785946489321870623815269985753746158861459150569598112014402896695181901760686159881359456029490851754791392860104524800698857851952048683087711170274564693473644387153066393126816874622568307143529174073426086900774808746527964157176644984208659254096137050812931970992963233059990402090219422639363503881374971180404614476175675341517804036257442868048387793775246628305167105526586268766979682025640583249927992233468838422582062990706232086875077218810181708510683136152781696872203009563078097281775498444747684178769200719166182820275361128347949846122345571729839331857265429515892958246893763588643,
11320469425625659447944572672127104215924996220806654104883406382573509992320627058076981450404770334617473696182574053311147414248595364490965507221536172348690663970121735140054038616565820713850943064083071079831065513458181200456474893698167658844784724020900641750099951242207355641902457408351489728809588982042368890495634661062508209569444860977808087159324411068716855660647565063854857834723569104981600005392387048537814058087753400275424200341649135186137643263410035894467733560501422964695791695100915763100944412664299664289566736865107978354482790197248991552108582815463899651285254118580658948423704,
11250461399779386326913254073468527449093994834046842007901847136889337199480293373517374696536983278119461022990030083561430132446915667543920052296369699640921553581218689123949605328654268066782846311438517743633894970974550817911234848999347198760120475383681239003104168684990135849266001783187956087741775409007737476991393227127711304354627515809662561647265947164511788316939535204416890967655939349404610967109007683998760981978392466562454674646060487585363558433477905620632224653631270609337500891974297436907278509824927975096946480769374708464448052320789795062628955485940141170301616730743809753451218,
9728070783603422726790305026269233180757448679371808091182627048326085978955982588027533752914237870241367507707682092599165777567832516566188998840865137473291816659679169720439829971887882435760256781338789855158273985670571203585843887377188618472721727412137187110899045674910779466902707910186196077005516348800627362695879964661706264721984164736879958672682479923187190220217725854493348343386772374023913918724505336020343727606925236248498831375012818689285902746744306634757224535589609553799424042986336264064852476133916682607297220717435616272704152778122659678630827735116570324546139388895776126233100,
10372377022739769752286893640496238286248878950239284012271672008133814958665240857715105732699889602983495683351218048834892283345332690787977246100395007583712050617142313552470546152083234794533924290018834916153198841398756152136542084574566033934256444131217525766449231068328554248036611267859336763317066902740351891502023864384592167685511187034430377718693145475936300887478999164816006725047516538190460176675713365356748683642952451598856582418600610697375811816567759097465251828643298609488409363407831718924618956584689966152747758252062752707093973079344401857582600328510100439016311624262855432345129,
12746074990094988125290302488022306969531651031585129639702544157968249941681932232200204608967409549361468416495900564133729466510225427792311033264805771929395691274427946157300261037704606620841054802306082626189979627089691644906883595442806640876312429444489674406733868287518051873496086688123043649863298650254793160396801774601344053904784337828973688458190429633625347881519825808256210517295781026342849104820930674536787650119565527421762524786905726229302883538428945110125556462188732673515356819977180220071094699285551905652034307558767250207637176198102692690816265677229250716139924530151140588675341,
8636516881848405202706323147002885007353780059160017779048921719307880858323096266990754982776133944540298995017561874950810330836700522199626161468498051458953701537278784467881407814358514446853328829597075966562463804297504945218794191051301448659137812611245419005715681430260746010408053402722332604499261905757546050935111392687776488487726436569494872303175830049261126927507696099950781746181255559015524141645155280047399282916443682532625149055484291466263721884324577263812450377253022644656921502178340028608190408875786696351581583107782381585789066897686348157813857964060507288451552805278288099274704,
6412766627400322049128650661439536630634697897055799579312252682784121840644628091082780054348546001917174344572122268533351837731986750406124170668466628270628929991391248208495700168711297697244066367314316755135773553361516435786240543523241144966657257688030024982286889800825673771875456808904751120486417221862535102525281914096922439119117546814234388335009155183507781721097383639944883530791825469761502800705417280630524502065920673838707195378484900398850513650602295932292156892756702624722821299933892695428449973487255438727741430499701069733216332007442200211804075792467501166625373852309052989932144,
7323532576223722346180805668144902752624979961975713429172374483895573381188604431363163735051787366857526023558826375670161661114683992934464600303092132040364593623549083934709793118367518944342135360617120116816045955499480022340239840855198692562536700950613569441627115036296852675752508919090021718654615105925942152898438961081310599573612253759719576533961205999963543436600437715202874922129022456534434673223404942393208338331387350947328341097795958916007735806631700268445470038756393772073665659715391885667784063811966073051023800511813605154500794244393782387043860305888221729678801696705013671597458,
382943004681099295332444420172468478242943489332778175255183493303684731545422034565125209677706218382909148637228249899430159148146578837499923778582878873543269627422049319414471935107111476636664126855098415527771320320014110743782104981626178319633721247813628089830997943080051771874484092476690561812881196077508335251201276527749624713820394921478133537643488179522047844177674300095534913008479070964193455407873924928886028958715922801316727092365175488386518021287652992974186169025932159615003171885340502309810265099199334339617704322273148895957129962016986620355413172666804779404068223221158124646982]


# b是密文列表 m是模数列表
def crt(b, m):
# 传入的参数分别为密文 和 模数n
# 乘积计算
M = 1
for i in range(len(m)):
M *= m[i]

Mm = []
# 求余数 M/m[i]
for i in range(len(m)):
Mm.append(M // m[i])

# 求Mm[i]的乘法逆元
Mm_ = []
for i in range(len(m)):
t, a, _ = gmpy2.gcdext(Mm[i], m[i])
Mm_.append(int(a % m[i]))

# 求的累加
y = 0
for i in range(len(m)):
y += (Mm[i] * Mm_[i] * b[i])
y = y % M
return y, M


m, mm = crt(c, N)
m = gmpy2.iroot(m, e)
print(m)
n2 = 15026774046422823772923416703258918256705230309288654575228175604579623547346936557402265468457774144295704018590371406643809210707919110930292118128672982085872998707303717237889568357434415716335302115574278581499476167897701494389034028855822763248021170702441333169823996230788613795106831058750118129231418803418591907957889185297331878433861587845273072867679682913406149012345637188214267352744534064183534416848039519838300470759962947609740063059652035749154884781650569829247851927255231832057438809733910924391141460583632531741373060332495229846381465246269827019781511923018097855504080258669180781429859
e1 = 65537
e2 = 84731

## round2

c1, c2 = [
1585460956371478358014706709052515290603769411694086203567347548677438603190306963660480257054194718081842055413142442411371783317247864151855310406868511302250366452216464986140280108136528150022557071124177556732065653857884852023680390544460637963828470794163920151017854039453109567822562407450258510955376741518286986211377922702150881768361198271139151405484893958729281752206219651760656339612414916889079301588246740231929262251845329897144136767994999566468680566673552798104235743748294554789829818396237690641878313323545394796020102762577870319613049463161869734736841964164206998101487217811484837781201,
12434006776634330420476277491709207144878226373696172466865824403472566180890543683302324657976319835530565193368235055602654962724355351425188986072019137460799663518374976490707220272856260231890837734085536222645149550435164360604591690058675151293426153733665098546211279575973148007708518445987619577060101053786212681455581647558497825900528285890225715807804674754171452668846978923385221410372907164256635621063068945059641440711301807076759442837643850786728146099578281554707982324719092923361091200962108758764305425336068739116215881081620911765527700114778450250922507158730494945560454334024928446469171]


# 欧几里得算法
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


n = n2
s = egcd(e1, e2)
s1 = s[1]
s2 = s[2]
# 求模反元素
if s1 < 0:
s1 = - s1
c1 = gmpy2.invert(c1, n)
elif s2 < 0:
s2 = - s2
c2 = gmpy2.invert(c2, n)

n3 = pow(c1, s1, n) * pow(c2, s2, n) % n
print(n3)

# n3 = 12051274697693290706124990444806864448117509935274260318752465436559967219137920860124919553314211368508765502664900094082142706661617704447394483260270502959912432422283933156562557829059776645511526175946830797944396210200593641364373812907632258856908808586745953996838876585148477089130920840126604759803726479860720545937822034855566021695483054110183583988907008833874797365887064421431419502425185380984500547228350047311439447317962190370712239063737356496598392557365981551946830095461329125051776103612935060093311720140311238109431293968822563969151282495204180885021489513059495503670571143636655452716469
n3 = 12051274697693290706124990444806864448117509935274260318752465436559967219137920860124919553314211368508765502664900094082142706661617704447394483260270502959912432422283933156562557829059776645511526175946830797944396210200593641364373812907632258856908808586745953996838876585148477089130920840126604759803726479860720545937822034855566021695483054110183583988907008833874797365887064421431419502425185380984500547228350047311439447317962190370712239063737356496598392557365981551946830095461329125051776103612935060093311720140311238109431293968822563969151282495204180885021489513059495503670571143636655452716469
key = 69658816925700544629283247962254001698454915826363324167248687841428229657908807412264705772690024925952057696968940793220409087134042992738307862734366593456917584994891634588967704564463461140666162771378448630149297796122822465032888385447250081148319964251299583828999913167876190841429498106534206930794855355577715448350958330453012916812935545376223554657010675616333737562871829058955712614717967062388097830554769074812791090306089948504126897656071909647674533441922466703825370972386827731262770792101862343930584334617555653870995907311492428224015386618103475028606681032449205172767950706359755588301624951536442947704335651172215907781436880482476424688324617047897192103372008662354672541395688790786001
c = 11863157764887938780824579679371643447225713456937713139455448355138750546048767357754685394264878457427548420317960140760043591368197340631979346116915377490755707128510486267310697447660165717832704288859433884564622568136142152559210196202042032186946621446044322226115326468571388229369925749537907791870951835832402887333436573697535805221699431529443630706171928243718095829047868200440437300914623880058122845868296335686759626119932797809303358591871321868677694716513342508348424566444564091188283096388392533118302463885528323186673006330284158603096338578778923136251906089784179273618683529795470632440395
k_phi = key - 1
e = 65537
d = gmpy2.invert(e, k_phi)
n4 = pow(c, d, n3)
print(n4)
# n4=11724141470213031356579859333168289639615622686202980979147607414099628001927870503704074638296450320671327227133196902015218180118453034164398442054531719756804900252542655705446585040034584849281570288775609323003673814723139607853980514502247385558743748114789081152782266165165126809308084620475099962391124207276377764447000610437375294321346891295581931069104337837729567504989594256840998015013333363391570945176593563683549152526955378806762080716974470953471818980944589068608135288232240285849862793197669031466836209176855431992367895720325301943048882628754497306752780016493475080579333402247446437049047
n4 = 11724141470213031356579859333168289639615622686202980979147607414099628001927870503704074638296450320671327227133196902015218180118453034164398442054531719756804900252542655705446585040034584849281570288775609323003673814723139607853980514502247385558743748114789081152782266165165126809308084620475099962391124207276377764447000610437375294321346891295581931069104337837729567504989594256840998015013333363391570945176593563683549152526955378806762080716974470953471818980944589068608135288232240285849862793197669031466836209176855431992367895720325301943048882628754497306752780016493475080579333402247446437049047
c = 2816736056550831653973157899506262168480964803166756293572880189176577881630790000276999096206812411096080372881789954172637160950190474795330245979531766081159871312457936325666134181134500105905111359723389919329114623407411163535294830491463973203982802730253713465016495675821972476067998011690656116432599713401287450098365165426916405282214433290711569156342791601423305946953973954621413878573603456700590631970960732956331418205770421655635464391354229333745390853601777279128370563052468332181418652663994521837627330594810890259573212379994374279868029267872371446355933222697100056083650060776007666183073
a = 6032255012157371164700839449542898126592510781190289067848160426876165189951498548810424090463495497254240852385327996020367612200233253652714782684592529295834508399602845086506666459956002787649982018171944404186939348972227785368826161631329498232311352585550355689962357547233819685756389741068890066262915845076213948619768623446047734222841712352116801857044746205363024718367924444942501205150747138618091689923805823531600663400066150044540190657719143803821379413879326243311349670384014889512830787892601344771894962321965492913814609341334113063437877829743121537190811440276283745574771747480203866956168
b = 1206808598909242535223376202389156773475195509201912000607981432006398598528783704808089821819011267947458676834452259929741273957718095195986027224077744723815196776751694084725008944111685380089765819072640641124176452132485301158877006786449055072190022151463977004480696273437839703625450503113220076272034783199115132878674594771158660499906314896727517373004222806580432047871587589294550579633876505236901173980743458637972423194533488244328660796974432193229150042095384808125939545211811637021512657530104465354340731623159501847989090953187756127021193637464098143884758072922518945377684605967349065921691
q = gmpy2.gcd(a - (b - 2019) ** 2019, n4)
p = n4 // q
phin = (p - 1) * (q - 1)
d = gmpy2.invert(e, phin)
m = pow(c, d, n4)
print(long_to_bytes(int(m)))