> 文章列表 > e与phi不互素 --- 四道题详记

e与phi不互素 --- 四道题详记

e与phi不互素 --- 四道题详记

文章目录

  • 题一([MoeCTF2022]signin)
      • 题目描述:
      • 题目分析:
  • 题二(unusualrsa5)
      • 题目描述:
      • 题目分析:
  • 题三([0ctf 2016]RSA?)
      • 题目描述:
      • 题目分析:
  • 题四(2022ctfshow卷王杯现代密码签到)
      • 题目描述:
      • 题目分析:
  • 总结
      • 1.
      • 2.
      • 3.
      • 4.

题一([MoeCTF2022]signin)

题目描述:

from Crypto.Util.number import *
from secret import flag
m=bytes_to_long(flag)
p=getPrime(512)
q=getPrime(512)
print('p=',p)
print('q=',q)
n=p*q
e=65537
c=pow(m,e,n)
print('c=',c)
#p= 12408795636519868275579286477747181009018504169827579387457997229774738126230652970860811085539129972962189443268046963335610845404214331426857155412988073
#q= 12190036856294802286447270376342375357864587534233715766210874702670724440751066267168907565322961270655972226761426182258587581206888580394726683112820379
#c= 68960610962019321576894097705679955071402844421318149418040507036722717269530195000135979777852568744281930839319120003106023209276898286482202725287026853925179071583797231099755287410760748104635674307266042492611618076506037004587354018148812584502385622631122387857218023049204722123597067641896169655595

题目分析:

  • 首先 e = gcd(e,phi) = 65537,所以用常规的不互素题方法做不出来,求gcd(e,(p-1))和gcd(e,(q-1)),发现e与p-1互素。而我们都知道有以下公式:

m = c ^ d mod n
==>
m = c ^ d mod p
m = c ^ d mod q
(注:此推论满足的前提是------在c不是p或q的倍数,以及d是正整数的情况下,m = c ^ d modp 和m = c ^ d modq 总是成立的,有兴趣的同志们可以自行查找推导过程,这里就不过多说了)

故可以使用以下代码:
d = invert(e,p-1)
m = pow(c,d,p)
  • 解题代码如下:
from Crypto.Util.number import *
import gmpy2p= 12408795636519868275579286477747181009018504169827579387457997229774738126230652970860811085539129972962189443268046963335610845404214331426857155412988073
q= 12190036856294802286447270376342375357864587534233715766210874702670724440751066267168907565322961270655972226761426182258587581206888580394726683112820379
c= 68960610962019321576894097705679955071402844421318149418040507036722717269530195000135979777852568744281930839319120003106023209276898286482202725287026853925179071583797231099755287410760748104635674307266042492611618076506037004587354018148812584502385622631122387857218023049204722123597067641896169655595
e=65537
n = p * q
phi = (p-1)
print(gmpy2.gcd(e,phi))
d = gmpy2.invert(e,phi)
m = pow(c,d,p)
print(long_to_bytes(m))
  • 得到:moectf{Oh~Now_Y0u_Kn0W_HoW_RsA_W0rkS!}

题二(unusualrsa5)

题目描述:

from Crypto.Util.number import bytes_to_long
from secret import flage = 0x14
p = 733089589724903586073820965792963746076789390539824437962807679954808310072656817423828613938510684864567664345751164944269489647964227519307980688068059059377123391499328155025962198363435968318689113750910755244276996554328840879221120846257832190569086861774466785101694608744384540722995426474322431441
q = 771182695213910447650732428220054698293987458796864628535794956332865106301119308051373568460701145677164052375651484670636989109023957702790185901445649197004100341656188532246838220216919835415376078688888076677350412398198442910825884505318258393640994788407100699355386681624118606588957344077387058721
n = p*qm = bytes_to_long(flag)
c = pow(m,e,n)
print(c)#406314720119562590605554101860453913891646775958515375190169046313074168423687276987576196367702523895650602252851191274766072774312855212771035294337840170341052016067631007495713764510925931612800335613551752201920460877432379214684677593342046715833439574705829048358675771542989832566579493199671622475225225451781214904100440695928239014046619329247750637911015313431804069312072581674845078940868349474663382442540424342613429896445329365750444298236684237769335405534090013035238333534521759502103604033307768304224154383880727399879024077733935062478113298538634071453067782212909271392163928445051705642

题目分析:

  • 首先 e = gcd(e,phi) = 20 ,发现 gcd(e,(p - 1)) = gcd(e,(q - 1)) = e = 20,这里用到了有限域开方的知识(hint中提示了这一点)
    e与phi不互素 --- 四道题详记

解析一下求根算法:

R.<x> = Zmod(p)[]
f = x ^ e - c
f = f.monic()
res1 = f.roots()
'''
这段代码是在Sage数学软件中运行的,用于求解一个在模质数p意义下的方程x^e=c的解。代码定义了一个多项式环R,请注意 R.<x> = Zmod(p)[] 这一句。这里 R 表示多项式环的名称,<x> 表示所定义的环中的变量名是x,而 Zmod(p)[] 表示在模p的意义下定义了一个多项式。其中每个多项式的系数都是p的倍数,以确保在模p的意义下进行运算时不会出现浮点运算精度的问题。接下来,代码定义了一个多项式 f = x^e - c。变量c和e在代码中的定义可能来自其他地方。通过将这个方程的领导系数归一化,再去除它,这个多项式被调整为monic的形式,并被保存在变量f中,以便计算方程的根。接下来,代码再次调用了多项式 f 的roots() 方法,这一次将得到方程的一个根的列表。具体地,最终生成的变量res1是多项式f在模p意义下的根列表。注意,如果方程没有解,则res1变量将为空列表。
'''
  • 以下为所有情况:

e与phi不互素 --- 四道题详记

说明一下为什么会出现以上形式的结果:这段代码通过调用 `f.roots()` 的方法,返回一个列表,列表中包含了方程 f(x)=x^e-c在模p的意义下的所有根。其中每个元素是一个二元组,第一个元素是方程的一个根,第二个元素为1,表示该根的重数(即此根的数量)。列表中的每个元素的格式为 `(a,1)`,其中`a`是方程x^e - c在模p下的一个解。在此二元组表示中,1表示多重根,即该解在方程中的出现次数。注意,列表中的根的数量可能与实际解的数量不同,因为有可能存在重复的根或者某些“假的”根。同时,注意到这是调用 Sage 的内建方法,其他代数软件实现的语法和精度亦有所不同。
res1= [(733089589724903586073820965792963746076789390539824437962807679954808310072656817423828613938510684864567664345751164944269489647964227519307980688068059059377123391499328155021524914773359906357153671313308407478602554319510265005085762860382683102940576377254668898119652133942180899304170329371707150164, 1), (678923693209704969852381465896650019425870260030383443168808423368601278880685781654734919678958092826800695579102973572447355792679848485826507692704568923813419881117766044423608229711484763917744685055881596752291255735808048270822124796786471676188662798449644642235738223466855946913879538299578053411, 1), (660147311087779881169338548239431864756054612093222666998714749603875812596923472248793683967669014397990139199125982652383529516284367882454604795633984744310817735291839918294987220579419849435923577095190063747058566440604560174772236817980884692508913814109640678513633481822665261866470516425285518285, 1), (656681791452498481654878002485070757012861926668381629152705461263764608739765714942200770232734120738697878503189820956143274111419030898383980731253182784661086554234440727684677249747860741931943770929571071514986737487620545162638141249825931408013949005076717924474881049785216136013509251488319927117, 1), (561634200460522548441571592444405305656486299630777500288946777401428565675621685143645766539546494757183424411676233243328896459121008394512864385381461601416842364770773986015469061716463481506742063309940039220834724151367845375195245551456554735236102965922407472562800115189890526203678874326289793386, 1), (523855729860851889306276946831707325654966799659419971520448260469324992736982246556398347232993411720682146837875080052611076416766800095923505800210207882786055877101711481659849019405604591067848093320332336464705973049736439281995689523942160283871387832933740244138033780213828909573276367363492479416, 1), (452900182350324645394395847267638113925741103910755278893324277011525974119646860179890955142636700494806732282459985420380030145373473199426467982810188731584443697329277404941404099390850352429776960314100205943189386609329123884794268027928641499784873014598307047385649837271871153730516499924907326391, 1), (416370840270302176660974325650517943145426999919261282193286142404178655729051589183139277588858670001090144796272169390365633208042166264602215868369443740484308138194802614266542495845026116458981772823239562965336426568684608791041112836390368657079922805426770894892558562813602601339340032501607476909, 1), (415121711750653986259101201654940133924221603939397750124825760079422401181007421592643535836083617458305454708658832229662210103019264900837109397638935012953657209660214900590221340670067523952418432762094850952735077742516293665730069986289396136048668366129437705942925977098013178518939090064725293698, 1), (374350165605081425148746685793120637325474206554845188409530082476947889392734021316375944344139500560536749012063348621966245700071777272257861879442907414106249944743486841972083642001920163764631575788319343002998139757149039708713099627493777332364552928705440787113844863245759659045732547084424923730, 1), (358739424119822160925074279999843108751315183984979249553277597477860420679922796107452669594371184304030915333687816322303243947892450247050118808625151645270873446755841313053878556361515804554057537962591412241278856797179801170508021218764054858204533933069025997987849745498624881677262879389897507711, 1), (317967877974249599814719764138023612152567786600426687837981919875385908891649395831185078102427067406262209637092332714607279544944962618470871290429124046423466181839113254435740857693368444366270680988815904291541918811812547213491050859968436054520418495645029079158768631646371362204056336409597137743, 1), (316718749454601409412846640142445802931362390620563155769521537550629654343605228240689336349652014863477519549478995553903856439922061254705764819698615318892815253304525540759419702518409851859707340927671192278940569985644232088180008009867463533489164056347695890209136045930781939383655393972714954532, 1), (280189407374578940679425118525325632151048286629069159069483402943282335953009957243937658795873984369760932063291179523889459502590754319881512705257870327792679694170050750084558098972585615888912153436810549301087609944999716994426852818329190690784213847176159737716044771472513386992478926549415105050, 1), (209233859864051696767544018961256420421822590880404466442359419485483317335674570867430266705517273143885517507876084891658413231197427423384474887857851176591067514397616673366113178957831377250841020430578418779571023504592401597225431322315671906697699028840726540963660828530555631149719059110829952025, 1), (171455389264381037632249373348558440420303090909046937673860902553379744397035132280182847398964190107384239934074931700940593188843219124795116302686597457960281026728554169010493136646972486811947050440970716023442272402960995504025875294801277455332983895852059312538894493554494014519316552148032638055, 1), (76407798272405104418942963307892989063927463871442808810102218691043701332891102481627843705776564125869785842561343988126215536545196620923999956814876274716036837264887427341284948615575226386745342821339683729290259066708295716582979596431900782555137856697748860626813558959168404709486174986002504324, 1), (72942278637123704904482417553531881320734778446601770964092930350932497475733345175034929970841670466577525146625182291885960131679859636853375892434074315066305656207488236730974977784016118882765536655720691497218430113724280704448884028276947498060173047664826106588061126921719278856524910049036913156, 1), (54165896515198616221439499896313726650919130509440994793999256586207031191971035769093694259552592037766968766648191371822133855284379033481472995363490135563703510381562110602353968651951204400944428695029158491985740818520792608398996049471360514380424063324822142865956385277528593809115888174744378030, 1), (4437283590076061961535442437602347765674442234818575874135357985875149087628510484519797886982042474802203641418825097102615281277, 1)]
res2= [(771182695213910447650732428220054698293987458796864628535794956332865106301119308051373568460701145677164052375651484670636989109023957702790185901445649197004100341656188532242400936626843773453840636251285728911675970163379867036690526519443109306012484303887302812373344206821914965170132246974771777444, 1), (766556454188661342717183441830753958949866417494716064331308873001400640185512932266682617111675304940672393843056881975135838366629757426494074139282338667675175892353840088889909195938963702112766349904198019372558645141384798583534383188883052267525748815134207587721641571601904662560056137622206973489, 1), (697252901173481151775023987079186148934671702095573239439077382532564148810196647295908000048808838516420504907242412420648310319468654564849006791509953203253591756022067984445917819993348421480837383431739473210443093160532003684668638512409524578887612424272056500338100724912084684072451333028872106867, 1), (637570615943233551403071638054753581415867520631437658239907745441121975126808316940174715476757666870531167942851357415707186407189108694841070152707092245309731475509435021166725640089960139181871460785130772780846084823629102758039859600829870217520667468256249544673326609044266687450424519008597139038, 1), (632327011426499994189315859401811158465010667761983995201046936822397685624551714172153350143852663041545272586491513748551456122474881151730273573849696707727190136249011496534362800230143294818909726182851035741169036440948865157971659975327810387018738693032165869052686855576696780303333160382173118510, 1), (603167942216453034450263940413887436639768290870012743059006350279961914111784943446402628202860617054930140073926171776018499373620898476085657301360306887162235526834606378738074341266663645930616670338128013164422994099750629106515540742416974080150559276654019646352903381932805758744985229930025215589, 1), (530466680163017973833308694998245966701824340964042532641356089137879243186221081260746979341797675072378875885003058544820764758046575857925699945279145064801793820479054735901259358468262299180565694104881447986591164582548172740211166270336596928161447369253455709413986912995034928572096835678138857265, 1), (465540790416036816247600567320870976232163306630453288403325643427712780000576148136992329436841499597503643564252159872723910561052802444806966727618888569275392200705998247985267055114981110557102594418390662751642665628146458339378829747379733648664063153493767191146530568857443383384153565928824587631, 1), (416074710242252176401044812192140737030784340427479537135890013577916016785682458613448218282243236911584933444225849145353809933396731357693587977285142052644526842758534194394543388181747428573306033684592492190246496510265285685848535401569621598453391802767037373579958843978087833160938909090562342099, 1), (394136899723687893136044300058080845383423389432958570785292421247573616646066598716388388801380799317671805332945423860473016951589636840148122537670603031835993896874831423637875155136425033395934564235988401408318773868625275203793169278596739058672957150515186335729596174779705646162358930520576725303, 1), (377045795490222554514688128161973852910564069363906057750502535085291489655052709334985179659320346359492247042706060810163972157434320862642063363775046165168106444781357108608963065080494802019441514452899675269031638529573167707032715226721519334968037637891914363625790506844412960426598413556810333418, 1), (355107984971658271249687616027913961263203118369385091399904942754949089515436849437925350178457908765579118931425635525283179175627226345096597924160507144359573498897654337852294832035172406842070045004295584487103915887933157224977349103748636795187602985640063325775427837646030773428018434986824716622, 1), (305641904797873631403131860899183722061824152166411340132469312905152326300543159914381239023859646079660408811399324797913078547971155257983219173826760627728708140950190284261571165101938724858273484270497413925707746770051984571447054757938524744976931634913333508208856112766675223204803778148562471090, 1), (240716015050892473817423733221808731592163117832822095894438867194985863114898226790626589118903470604785176490648426125816224350977381844864485956166504132202306521177133796345578861748657536234810384584006628690759247815650270170614718234981661465479547419153644989941399768629083678016860508399248201456, 1), (168014752997457413200468487806167261654219167926851885476788606052903192189334364604970940257840528622233912301725312894618489735403059226704528600085342309841864814821582153508763878950256189484759408350760063512927418298447813804310343762901284313490435511753081053002483299691312847843972114147361843132, 1), (138855683787410453461416568818243539828976791034880633334748019510467420676567593879220218316848482635618779789159970922085532986549076551059912327595952489276910205407177035712475419986776540596466352506037040936181375957249577752854224529990448006622256095374934830302699826047421826285624183695213940211, 1), (133612079270676896247660790165301116878119938165426970295887210891743131174310991111198852983943478806632884432800127254929802701834849007949115748738556951694368866146753511080112580126959696233504617903757303896504327574569340152786024904488388176120327320150851154682060072579851919138532825068789919683, 1), (73929794040429295875708441140868549359315756701291389096717573800300957490922660755465568411892307160743547468409072249988678789555303137941179109935695993750508585634120547800920400223571413934538695257148603466907319237666439226157245992908733814753382364135044199017285956712033922516506011048514951854, 1), (4626241025249104933548986389300739344121041302148564204486083331464466115606375784690951349025840736491658532594602695501150742394200276296111762163310529328924449302348443356929024277956133302609728784690057304791767256813644327291501316435206126115245973272893111633745110022213944028901206455180085232, 1), (4437283590076061961535442437602347765674442234818575874135357985875149087628510484519797886982042474802203641418825097102615281277, 1)]
  • 之后便可用中国剩余定理求解m了
from Crypto.Util.number import  *
import libnump = 733089589724903586073820965792963746076789390539824437962807679954808310072656817423828613938510684864567664345751164944269489647964227519307980688068059059377123391499328155025962198363435968318689113750910755244276996554328840879221120846257832190569086861774466785101694608744384540722995426474322431441
q = 771182695213910447650732428220054698293987458796864628535794956332865106301119308051373568460701145677164052375651484670636989109023957702790185901445649197004100341656188532246838220216919835415376078688888076677350412398198442910825884505318258393640994788407100699355386681624118606588957344077387058721
n = 9057141637995599750120273501711128117576789048411357158233050845658505488383724832915968443730006384810721595601723748471745315354759415044859624198755098491311647992728384572103262800310263916249536898582100747311978019829291619921741682336800665277699122504431456051606407509905004993708771825443764723285750825546500765451509998514747599779552241055519485714649825416851221219747115910385536482995890893190128149999622905611239433481756073333147782531765685320972075370276543786386451560493093416152466142374684450770169257924330366774896526508005296520372463932722237001341584625279676089901419404816917142209281664709940400762785892142918132066900664643155176180059403739
c = 406314720119562590605554101860453913891646775958515375190169046313074168423687276987576196367702523895650602252851191274766072774312855212771035294337840170341052016067631007495713764510925931612800335613551752201920460877432379214684677593342046715833439574705829048358675771542989832566579493199671622475225225451781214904100440695928239014046619329247750637911015313431804069312072581674845078940868349474663382442540424342613429896445329365750444298236684237769335405534090013035238333534521759502103604033307768304224154383880727399879024077733935062478113298538634071453067782212909271392163928445051705642
e= 20
res1= [(733089589724903586073820965792963746076789390539824437962807679954808310072656817423828613938510684864567664345751164944269489647964227519307980688068059059377123391499328155021524914773359906357153671313308407478602554319510265005085762860382683102940576377254668898119652133942180899304170329371707150164, 1), (678923693209704969852381465896650019425870260030383443168808423368601278880685781654734919678958092826800695579102973572447355792679848485826507692704568923813419881117766044423608229711484763917744685055881596752291255735808048270822124796786471676188662798449644642235738223466855946913879538299578053411, 1), (660147311087779881169338548239431864756054612093222666998714749603875812596923472248793683967669014397990139199125982652383529516284367882454604795633984744310817735291839918294987220579419849435923577095190063747058566440604560174772236817980884692508913814109640678513633481822665261866470516425285518285, 1), (656681791452498481654878002485070757012861926668381629152705461263764608739765714942200770232734120738697878503189820956143274111419030898383980731253182784661086554234440727684677249747860741931943770929571071514986737487620545162638141249825931408013949005076717924474881049785216136013509251488319927117, 1), (561634200460522548441571592444405305656486299630777500288946777401428565675621685143645766539546494757183424411676233243328896459121008394512864385381461601416842364770773986015469061716463481506742063309940039220834724151367845375195245551456554735236102965922407472562800115189890526203678874326289793386, 1), (523855729860851889306276946831707325654966799659419971520448260469324992736982246556398347232993411720682146837875080052611076416766800095923505800210207882786055877101711481659849019405604591067848093320332336464705973049736439281995689523942160283871387832933740244138033780213828909573276367363492479416, 1), (452900182350324645394395847267638113925741103910755278893324277011525974119646860179890955142636700494806732282459985420380030145373473199426467982810188731584443697329277404941404099390850352429776960314100205943189386609329123884794268027928641499784873014598307047385649837271871153730516499924907326391, 1), (416370840270302176660974325650517943145426999919261282193286142404178655729051589183139277588858670001090144796272169390365633208042166264602215868369443740484308138194802614266542495845026116458981772823239562965336426568684608791041112836390368657079922805426770894892558562813602601339340032501607476909, 1), (415121711750653986259101201654940133924221603939397750124825760079422401181007421592643535836083617458305454708658832229662210103019264900837109397638935012953657209660214900590221340670067523952418432762094850952735077742516293665730069986289396136048668366129437705942925977098013178518939090064725293698, 1), (374350165605081425148746685793120637325474206554845188409530082476947889392734021316375944344139500560536749012063348621966245700071777272257861879442907414106249944743486841972083642001920163764631575788319343002998139757149039708713099627493777332364552928705440787113844863245759659045732547084424923730, 1), (358739424119822160925074279999843108751315183984979249553277597477860420679922796107452669594371184304030915333687816322303243947892450247050118808625151645270873446755841313053878556361515804554057537962591412241278856797179801170508021218764054858204533933069025997987849745498624881677262879389897507711, 1), (317967877974249599814719764138023612152567786600426687837981919875385908891649395831185078102427067406262209637092332714607279544944962618470871290429124046423466181839113254435740857693368444366270680988815904291541918811812547213491050859968436054520418495645029079158768631646371362204056336409597137743, 1), (316718749454601409412846640142445802931362390620563155769521537550629654343605228240689336349652014863477519549478995553903856439922061254705764819698615318892815253304525540759419702518409851859707340927671192278940569985644232088180008009867463533489164056347695890209136045930781939383655393972714954532, 1), (280189407374578940679425118525325632151048286629069159069483402943282335953009957243937658795873984369760932063291179523889459502590754319881512705257870327792679694170050750084558098972585615888912153436810549301087609944999716994426852818329190690784213847176159737716044771472513386992478926549415105050, 1), (209233859864051696767544018961256420421822590880404466442359419485483317335674570867430266705517273143885517507876084891658413231197427423384474887857851176591067514397616673366113178957831377250841020430578418779571023504592401597225431322315671906697699028840726540963660828530555631149719059110829952025, 1), (171455389264381037632249373348558440420303090909046937673860902553379744397035132280182847398964190107384239934074931700940593188843219124795116302686597457960281026728554169010493136646972486811947050440970716023442272402960995504025875294801277455332983895852059312538894493554494014519316552148032638055, 1), (76407798272405104418942963307892989063927463871442808810102218691043701332891102481627843705776564125869785842561343988126215536545196620923999956814876274716036837264887427341284948615575226386745342821339683729290259066708295716582979596431900782555137856697748860626813558959168404709486174986002504324, 1), (72942278637123704904482417553531881320734778446601770964092930350932497475733345175034929970841670466577525146625182291885960131679859636853375892434074315066305656207488236730974977784016118882765536655720691497218430113724280704448884028276947498060173047664826106588061126921719278856524910049036913156, 1), (54165896515198616221439499896313726650919130509440994793999256586207031191971035769093694259552592037766968766648191371822133855284379033481472995363490135563703510381562110602353968651951204400944428695029158491985740818520792608398996049471360514380424063324822142865956385277528593809115888174744378030, 1), (4437283590076061961535442437602347765674442234818575874135357985875149087628510484519797886982042474802203641418825097102615281277, 1)]
res2= [(771182695213910447650732428220054698293987458796864628535794956332865106301119308051373568460701145677164052375651484670636989109023957702790185901445649197004100341656188532242400936626843773453840636251285728911675970163379867036690526519443109306012484303887302812373344206821914965170132246974771777444, 1), (766556454188661342717183441830753958949866417494716064331308873001400640185512932266682617111675304940672393843056881975135838366629757426494074139282338667675175892353840088889909195938963702112766349904198019372558645141384798583534383188883052267525748815134207587721641571601904662560056137622206973489, 1), (697252901173481151775023987079186148934671702095573239439077382532564148810196647295908000048808838516420504907242412420648310319468654564849006791509953203253591756022067984445917819993348421480837383431739473210443093160532003684668638512409524578887612424272056500338100724912084684072451333028872106867, 1), (637570615943233551403071638054753581415867520631437658239907745441121975126808316940174715476757666870531167942851357415707186407189108694841070152707092245309731475509435021166725640089960139181871460785130772780846084823629102758039859600829870217520667468256249544673326609044266687450424519008597139038, 1), (632327011426499994189315859401811158465010667761983995201046936822397685624551714172153350143852663041545272586491513748551456122474881151730273573849696707727190136249011496534362800230143294818909726182851035741169036440948865157971659975327810387018738693032165869052686855576696780303333160382173118510, 1), (603167942216453034450263940413887436639768290870012743059006350279961914111784943446402628202860617054930140073926171776018499373620898476085657301360306887162235526834606378738074341266663645930616670338128013164422994099750629106515540742416974080150559276654019646352903381932805758744985229930025215589, 1), (530466680163017973833308694998245966701824340964042532641356089137879243186221081260746979341797675072378875885003058544820764758046575857925699945279145064801793820479054735901259358468262299180565694104881447986591164582548172740211166270336596928161447369253455709413986912995034928572096835678138857265, 1), (465540790416036816247600567320870976232163306630453288403325643427712780000576148136992329436841499597503643564252159872723910561052802444806966727618888569275392200705998247985267055114981110557102594418390662751642665628146458339378829747379733648664063153493767191146530568857443383384153565928824587631, 1), (416074710242252176401044812192140737030784340427479537135890013577916016785682458613448218282243236911584933444225849145353809933396731357693587977285142052644526842758534194394543388181747428573306033684592492190246496510265285685848535401569621598453391802767037373579958843978087833160938909090562342099, 1), (394136899723687893136044300058080845383423389432958570785292421247573616646066598716388388801380799317671805332945423860473016951589636840148122537670603031835993896874831423637875155136425033395934564235988401408318773868625275203793169278596739058672957150515186335729596174779705646162358930520576725303, 1), (377045795490222554514688128161973852910564069363906057750502535085291489655052709334985179659320346359492247042706060810163972157434320862642063363775046165168106444781357108608963065080494802019441514452899675269031638529573167707032715226721519334968037637891914363625790506844412960426598413556810333418, 1), (355107984971658271249687616027913961263203118369385091399904942754949089515436849437925350178457908765579118931425635525283179175627226345096597924160507144359573498897654337852294832035172406842070045004295584487103915887933157224977349103748636795187602985640063325775427837646030773428018434986824716622, 1), (305641904797873631403131860899183722061824152166411340132469312905152326300543159914381239023859646079660408811399324797913078547971155257983219173826760627728708140950190284261571165101938724858273484270497413925707746770051984571447054757938524744976931634913333508208856112766675223204803778148562471090, 1), (240716015050892473817423733221808731592163117832822095894438867194985863114898226790626589118903470604785176490648426125816224350977381844864485956166504132202306521177133796345578861748657536234810384584006628690759247815650270170614718234981661465479547419153644989941399768629083678016860508399248201456, 1), (168014752997457413200468487806167261654219167926851885476788606052903192189334364604970940257840528622233912301725312894618489735403059226704528600085342309841864814821582153508763878950256189484759408350760063512927418298447813804310343762901284313490435511753081053002483299691312847843972114147361843132, 1), (138855683787410453461416568818243539828976791034880633334748019510467420676567593879220218316848482635618779789159970922085532986549076551059912327595952489276910205407177035712475419986776540596466352506037040936181375957249577752854224529990448006622256095374934830302699826047421826285624183695213940211, 1), (133612079270676896247660790165301116878119938165426970295887210891743131174310991111198852983943478806632884432800127254929802701834849007949115748738556951694368866146753511080112580126959696233504617903757303896504327574569340152786024904488388176120327320150851154682060072579851919138532825068789919683, 1), (73929794040429295875708441140868549359315756701291389096717573800300957490922660755465568411892307160743547468409072249988678789555303137941179109935695993750508585634120547800920400223571413934538695257148603466907319237666439226157245992908733814753382364135044199017285956712033922516506011048514951854, 1), (4626241025249104933548986389300739344121041302148564204486083331464466115606375784690951349025840736491658532594602695501150742394200276296111762163310529328924449302348443356929024277956133302609728784690057304791767256813644327291501316435206126115245973272893111633745110022213944028901206455180085232, 1), (4437283590076061961535442437602347765674442234818575874135357985875149087628510484519797886982042474802203641418825097102615281277, 1)]
for i in res1:for j in res2:# 中国剩余定理m =libnum.solve_crt([int(i[0]),int(j[0])],[p,q])#c3=libnum.solve_crt([c1,c2], [q1,q2])flag = long_to_bytes(m)if flag.startswith(b'flag'):print(flag)
说明一下此中国剩余定理求解的是啥:
m = mi % p
m = mj % q
其中:
mi = res1[i[0]]
mj = res2[j[0]]
最终求出的便是m,故不需对m开e次方
  • 最后得到:flag{r54__d34l1n6_w17h_3v3n_3 _&_f1nd1n6_n-7h_r0075~~}

  • 只有在c不是p或q的倍数,以及d是正整数的情况下,m = c ^ d modp 和m = c ^ d modq 才总是成立的。

题三([0ctf 2016]RSA?)

题目描述:

c = 2485360255306619684345131431867350432205477625621366642887752720125176463993839766742234027524
n = 0x2CAA9C09DC1061E507E5B7F39DDE3455FCFE127A2C69B621C83FD9D3D3EAA3AAC42147CD7188C53
e = 3

题目分析:

  • 分解c,得到:
p = 26440615366395242196516853423447
q = 27038194053540661979045656526063
r = 32581479300404876772405716877547
  • 发现 gcd(e, p-1) = gcd(e, r-1) = e = 3, gcd(e, q-1) = 1

但用
e*d = 1 mod (q-1)
m = c ^ d mod q
求不出m

  • 因此尝试用有限域开方求解,代码如下:
# n = 0x2CAA9C09DC1061E507E5B7F39DDE3455FCFE127A2C69B621C83FD9D3D3EAA3AAC42147CD7188C53
import gmpy2
from Crypto.Util.number import *
import libnum
e = 3
c = 2485360255306619684345131431867350432205477625621366642887752720125176463993839766742234027524
n = 23292710978670380403641273270002884747060006568046290011918413375473934024039715180540887338067
p = 26440615366395242196516853423447
q = 27038194053540661979045656526063
r = 32581479300404876772405716877547'''
sage:
R.<x> = Zmod(p)[]
f = x ^ e - c
f = f.monic()
res1 = f.roots()R.<x> = Zmod(q)[]
f = x ^ e - c
f = f.monic()
res2 = f.roots()R.<x> = Zmod(r)[]
f = x ^ e - c
f = f.monic()
res3 = f.roots()print('res1 =',res1)
print('res2 =',res2)
print('res3 =',res3)
'''res1 = [(13374868592866626517389128266735, 1), (7379361747422713811654086477766, 1), (5686385026105901867473638678946, 1)]
res2 = [(19616973567618515464515107624812, 1)]
res3 = [(13404203109409336045283549715377, 1), (13028011585706956936052628027629, 1), (6149264605288583791069539134541, 1)]
for i in res1:for j in res2:for k in res3:m = libnum.solve_crt([int(i[0]),int(j[0]),int(k[0])],[p,q,r])print(long_to_bytes(m))
  • 得到:0ctf{HahA!Thi5_1s_n0T_rSa~}

题四(2022ctfshow卷王杯现代密码签到)

题目描述:

from Crypto.Util.number import bytes_to_long
from secrets import p,q,r,s,t,flagn = p * q * r * s * t
e = 2
m = bytes_to_long(os.urandom(500) + flag)
c = pow(m,e,n)print(p,q,r,s,t,sep='\\n')
print(c)'''
p = 145332367700944303747548912160113939198078051436029477960348968315913956664143693347226702600438608693933768134575289286283267810723137895903153829001826223446477799895493265422562348917012216790077395795861238257357035152687833639085415763850743538206986781418939737511715957738982536382066693822159860701263
q = 116660458253067608044065523310547233337730583902133756095473339390057738510707447906971188577217274861047379404014140178165569604404468897712846876108444468370709141219302291601408652742006268186059762087155933131837323952675627966299810891805398890428420575425160696531236660480933905879208166090591482794763
r = 157931722402853245421436270609912823260313730941283152856444641969403238646482562190531038393124087232554754746464603598717356255570166081501573727336977292059427220330169044611674973569766966838498453232642731737958791706086957762244686953294662693939604300864961637325536379321027705854708492453330690705531
s = 100973451687449518854742673778783266158999451072058606348222018797891147675959983616210003484476577612134482311993701677242007759556951494382833070563369964294544839433671087037596159753825249018950693369209927951667775267086896180395776150188902057785214767230658487267587289809918132337927575673868568976679
t = 93960345071948255233882121683650797512129333868351496468898834736770441398743300745703393838320587998953678254272245400344928586394089488734271897540051673996675973642347859306921527430850673334243441180183460927865980713929789963587608547554858491264614271309608925634272282292964002897650355047792764365447
c = 9144597920381774885442906257311149465702295057238600973973598305004391534618770363098565074541384771979931799878381439264848137810353858418200992191234142740194489573540381681161219332611454834544291634628456257670178843484698324641739324687497388018406214041657278323855749902661752448796122517061920880552011343608609622885787617238758769398972009949575526258430282648817039091284796330585349957724522615105102735930258969562103112238020133587096826386028128471852377225525357348919204333121695432662339443004327748973224423132988376298843862056631045488285859621661802413201793962883794915513510467912312842687601478117040419013468059983777273699192408773551806581458197324620065210523913467414181480875280203580147077789063808832356486197271376615883221558265591069223727607585313240243619515521180600435114131162272519949101464089935441251751426683447701142156416866113627126765919641034042927519834229168536331952275698122511502745177547569813354280565828372968703810158857859460406828090199683324760956105682902577189283246483314689365570862217407333103243336691401424548702387876409228977278498691200028282744239512091373110111792177228979867318546462714521296256938374618636206565791541769138267080789842400796973226733816939794717596194090232425688504890234304977612220790858557639246367437740975495450011676714198668471438814299689325208882261918460708833888406187912527346628912894921059735420931656953236560178909180587372589456926690219114173193202048332172538564489660440225377822914097420807957784201785024166011709377791129
'''

题目分析:

  • 这题e = 2,和p-1,q-1,r-1,s-1,t-1都不互素,且gcd = 2 = e
  • 目前我只能想到用用有限域开方来解,解题代码如下:
from Crypto.Util.number import *
import gmpy2
import libnum
e = 2
p = 145332367700944303747548912160113939198078051436029477960348968315913956664143693347226702600438608693933768134575289286283267810723137895903153829001826223446477799895493265422562348917012216790077395795861238257357035152687833639085415763850743538206986781418939737511715957738982536382066693822159860701263
q = 116660458253067608044065523310547233337730583902133756095473339390057738510707447906971188577217274861047379404014140178165569604404468897712846876108444468370709141219302291601408652742006268186059762087155933131837323952675627966299810891805398890428420575425160696531236660480933905879208166090591482794763
r = 157931722402853245421436270609912823260313730941283152856444641969403238646482562190531038393124087232554754746464603598717356255570166081501573727336977292059427220330169044611674973569766966838498453232642731737958791706086957762244686953294662693939604300864961637325536379321027705854708492453330690705531
s = 100973451687449518854742673778783266158999451072058606348222018797891147675959983616210003484476577612134482311993701677242007759556951494382833070563369964294544839433671087037596159753825249018950693369209927951667775267086896180395776150188902057785214767230658487267587289809918132337927575673868568976679
t = 93960345071948255233882121683650797512129333868351496468898834736770441398743300745703393838320587998953678254272245400344928586394089488734271897540051673996675973642347859306921527430850673334243441180183460927865980713929789963587608547554858491264614271309608925634272282292964002897650355047792764365447
c = 9144597920381774885442906257311149465702295057238600973973598305004391534618770363098565074541384771979931799878381439264848137810353858418200992191234142740194489573540381681161219332611454834544291634628456257670178843484698324641739324687497388018406214041657278323855749902661752448796122517061920880552011343608609622885787617238758769398972009949575526258430282648817039091284796330585349957724522615105102735930258969562103112238020133587096826386028128471852377225525357348919204333121695432662339443004327748973224423132988376298843862056631045488285859621661802413201793962883794915513510467912312842687601478117040419013468059983777273699192408773551806581458197324620065210523913467414181480875280203580147077789063808832356486197271376615883221558265591069223727607585313240243619515521180600435114131162272519949101464089935441251751426683447701142156416866113627126765919641034042927519834229168536331952275698122511502745177547569813354280565828372968703810158857859460406828090199683324760956105682902577189283246483314689365570862217407333103243336691401424548702387876409228977278498691200028282744239512091373110111792177228979867318546462714521296256938374618636206565791541769138267080789842400796973226733816939794717596194090232425688504890234304977612220790858557639246367437740975495450011676714198668471438814299689325208882261918460708833888406187912527346628912894921059735420931656953236560178909180587372589456926690219114173193202048332172538564489660440225377822914097420807957784201785024166011709377791129
n = p * q * r * s * t'''
sage:
R.<x> = Zmod(p)[]
f = x ^ e - c
f = f.monic()
res1 = f.roots()R.<x> = Zmod(q)[]
f = x ^ e - c
f = f.monic()
res2 = f.roots()R.<x> = Zmod(r)[]
f = x ^ e - c
f = f.monic()
res3 = f.roots()R.<x> = Zmod(s)[]
f = x ^ e - c
f = f.monic()
res4 = f.roots()R.<x> = Zmod(t)[]
f = x ^ e - c
f = f.monic()
res5 = f.roots()print('res1 =',res1)
print('res2 =',res2)
print('res3 =',res3)
print('res4 =',res4)
print('res5 =',res5)
'''res1 = [(105759306796604458734616988025041070894254086177961955152266241983599552436038417164589781800548170263137744573303899679801944648323570236444662118586389843689723523559621259451179955280920984411241370317372577352603546061045989233542252380860541664590826871711240796449926428967356180623320755399312333855914, 1), (39573060904339845012931924135072868303823965258067522808082726332314404228105276182636920799890438430796023561271389606481323162399567659458491710415436379756754276335872005971382393636091232378836025478488660904753489091641844405543163382990201873616159909707698941061789528771626355758745938422847526845349, 1)]
res2 = [(61271467210118412966724984292095335317733695256090878426560686891756079924997989716821358995235922204013829340494037064720729099316872761029080019202081517084920407823716674786308109091176385109278967641331532338629650874309430534283470953619914746783509470375167910894276568010726761673444218996017135114156, 1), (55388991042949195077340539018451898019996888646042877668912652498301658585709458190149829581981352657033550063520103113444840505087596136683766856906362951285788733395585616815100543650829883076780794445824400793207673078366197432016339938185484143644911105049992785636960092470207144205763947094574347680607, 1)]
res3 = [(81494645874260988911158916071880589359987692444861294147300186590570368369310500749798165469954243389790515045742691754565984336157619549494162814548778978629336604244757475768337171648672659348063737408489515868219005399374680983335675188997145621903581258458582232536740517461870029388465524596256244426733, 1), (76437076528592256510277354538032233900326038496421858709144455378832870277172061440732872923169843842764239700721911844151371919412546532007410912788198313430090616085411568843337801921094307490434715824153215869739786306712276778909011764297517072036023042406379404788795861859157676466242967857074446278798, 1)]
res4 = [(51144823034893467516049830013634908954947282240108395637750270980709555996962447979999247978745025424678021661508447789536796050829892155378538642362300766072138752230839087308843963081621082317370683937461997061050952181636902197426079036843036405576746588315389276602207691439303029900209974821989735900029, 1), (49828628652556051338692843765148357204052168831950210710471747817181591678997535636210755505731552187456460650485253887705211708727059339004294428201069198222406087202831999728752196672204166701580009431747930890616823085449993982969697113345865652208468178915269210665379598370615102437717600851878833076650, 1)]
res5 = [(56637279121653393183765926394036757622063751326302092068920147262287397466384343814073144226967254272339029262947175891947262297560109802237295059464880607159848782193114481656026708061277319588698540513237321988174311640122032153576262583677645331084360321934219426057528705181800937189748999636758570257615, 1), (37323065950294862050116195289614039890065582542049404399978687474483043932358956931630249611353333726614648991325069508397666288833979686496976838075171066836827191449233377650894819369573353745544900666946138939691669073807757810011345963877213160180253949375389499576743577111163065707901355411034194107832, 1)]
for i in res1:for j in res2:for k in res3:for a in res4:for b in res5:m = libnum.solve_crt([int(i[0]),int(j[0]),int(k[0]),int(a[0]),int(b[0])],[p,q,r,s,t])flag = long_to_bytes(m)if b'ctf' in flag:print(flag)breakbreakbreakbreakbreak
  • 得到答案:ctfshow{D0_y0u_R3aLly_Kn0w_Ra8IN_alg0RI7HM?}

总结

1.

题1中 e = 65537,很大,用有限域开方求不出来,只能用题一中的方法求解,我想出题人也是要我们用这种方法求解

2.

题2中 gcd(e,p-1) = gcd(e,q-1) = e = 20, e 较小,目前只知道用有限域开方求解

3.

题3中 gcd(e,p-1) = gcd(e,r-1) = e = 3,gcd(e,q-1) = 1, 用题1中的解法求不出,既然如此,那么目前知道的解法也就是有限域开方了。

4.

综上,我们可以大胆猜测,若e与phi不互素
若e较大,则 e与p-1或q-1或r-1中定有一个互素,然后令d = invert(e,p-1),m = pow(c,d,p)。
若e较小,且e与p-1或q-1或r-1中有一个互素,那么以上这种解法大概率是求不出的,但有限域开方可以求出
以上皆为笔者个人猜测,如有错误,请谅解