from Crypto.Util.number import getPrime, bytes_to_long import math
FLAG = b'**************'
defgenerate_parameters(bit_length=512): p = getPrime(bit_length) q = getPrime(bit_length) n = p * q e = 65537 phi = (p - 1) * (q - 1) d = pow(e, -1, phi) s = p ** 4 + q ** 4 return n, e, d, s, p, q
defmain(): n, e, d, s, p, q = generate_parameters() c = pow(bytes_to_long(FLAG), e, n)
if __name__ == "__main__": main() n = 125997816345753096048865891139073286898143461169514858050232837657906289840897974068391106608902082960171083817785532702158298589600947834699494234633846206712414663927142998976208173208829799860130354978308649020815886262453865196867390105038666506017720712272359417586671917060323891124382072599746305448903 e = 65537 c = 16076213508704830809521504161524867240789661063230251272973700316524961511842110066547743812160813341691286895800830395413052502516451815705610447484880112548934311914559776633140762863945819054432492392315491109745915225117227073045171062365772401296382778452901831550773993089344837645958797206220200272941 s = 35935569267272146368441512592153486419244649035623643902985220815940198358146024590300394059909370115858091217597774010493938674472746828352595432824315405933241792789402041405932624651226442192749572918686958461029988244396875361295785103356745756304497466567342796329331150560777052588294638069488836419744297241409127729615544668547101580333420563318486256358906310909703237944327684178950282413703357020770127158209107658407007489563388980582632159120621869165333921661377997970334407786581024278698231418756106787058054355713472306409772260619117725561889350862414726861327985706773512963177174611689685575805282
from Crypto.Util.number import long_to_bytes, inverse from math import isqrt
# --- 公开数据 --- N = 1259978163457...448903 e = 65537 c = 1607621350870...272941 s = 3593556926727...364197442972414091277296155446685471015803334205633184862563589063109097032379443276841789502824137033570207701271582091076584070074895633889805826321591206218691653339216613779979703344077865810242786... # 仅截断显示
# --- 利用 s 求 p, q --- r = isqrt(2 * N * N + s) # √(2N²+s) z = 2 * N + r # x² x = isqrt(z) # p+q d_sqr = x * x - 4 * N p = (x + isqrt(d_sqr)) // 2 q = (x - isqrt(d_sqr)) // 2
# --- 解密 --- phi = (p - 1) * (q - 1) d = inverse(e, phi) m = pow(c, d, N) flag = long_to_bytes(m).decode() print(flag)
import random import os from Crypto.Util.number import * from gmpy2 import lcm, gcd defexgcd(a, b): if b == 0: return1, 0 x, y = exgcd(b, a%b) return y, x - a//b*y
defget_k(): whileTrue: p = getPrime(512) q = getPrime(512) phi = (p - 1) * (q - 1) if gcd(p * q, phi) == 1: break n = p * q whileTrue: g = random.randint(1, n * n) if gcd((g - 1) // n, n) == 1: break return (n, g), (p, q)
defPaillier_encode(m, g, n): whileTrue: r = random.randint(1, n - 1) if gcd(r, n) == 1: break return (pow(g, m, n * n) * pow(r, n, n * n)) % (n * n)
defPaillier_decode(c, p, q, g, n): lam = lcm(p - 1, q - 1) mi = exgcd((pow(g, lam, n * n) - 1) // n, n)[0] % n return (pow(c, lam, n * n) - 1) // n * mi % n
pk, sk = get_k() n, g = pk p, q = sk m1 = bytes_to_long(flag) m2 = bytes_to_long(os.urandom(2) * 35) c1 = Paillier_encode(m1, g, n) c2 = Paillier_encode(m2, g, n) print(f'c = {Paillier_decode(c1 * c2, p, q, g, n)}')
# c = 1426774899479339414711783875769670405758108494041927642533743607154735397076811133205075799614352194241060726689487117802867974494099614371033282640015883625484033889861
from Crypto.Util.number import bytes_to_long, long_to_bytes
c = 1426774899479339414711783875769670405758108494041927642533743607154735397076811133205075799614352194241060726689487117802867974494099614371033282640015883625484033889861
for b0 inrange(256): for b1 inrange(256): m2 = bytes_to_long(bytes([b0, b1]) * 35) m1 = c - m2 if m1 < 0: continue s = long_to_bytes(m1) try: t = s.decode() except UnicodeDecodeError: continue if t.startswith(("flag{", "palu{")) and t.endswith("}"): print(t) # 唯一输出
import gmpy2 from Crypto.Util.number import getPrime import hashlib
primes = [(getPrime(1024), getPrime(1024)) for _ inrange(4)] N = [p * q for p, q in primes] PHI = [(p**2 - 1) * (q**2 - 1) for p, q in primes] d = getPrime(800) flag = "palu{" + hashlib.md5(str(d)[].encode()).hexdigest() + "}" E = [int(gmpy2.invert(d, PHI[i])) for i inrange(4)] print(N) print(E) # [23796646026878116589547283793150995927866567938335548416869023482791889761195291718895745055959853934513618760888513821480917766191633897946306199721200583177442944168533218236080466338723721813833112934172813408785753690869328477108925253250272864647989241887047368829689684698870160049332949549671046125158024445929082758264311584669347802324514633164611600348485747482925940752960745308927584754759033237553398957651216385369140164712159020014009858771182426893515016507774993840721603911101735647966838456333878426803669855790758035721418868768618171692143354466457771363078719423863861881209003100274869680348729, 19552522218179875003847447592795537408210008360038264050591506858077823059915495579150792312404199675077331435544143983146080988327453540449160493126531689234464110427289951139790715136775261122038034076109559997394039408007831367922647325571759843192843854522333120187643778356206039403073606561618190519937691323868253954852564110558105862497499849080112804340364976236598384571278659796189204447521325485338769935361453819608921520780103184296098278610439625935404967972315908808657494638735904210709873823527111315139018387713381604550946445856087746716671838144925662314348628830687634437271225081272705532826343, 20588310030910623387356293638800302031856407530120841616298227518984893505166480372963166394317326422544430837759332223527939420321960057410073228508230111170414845403161052128790464277007579491219950440477721075788978767309211469555824310913593208232853272958011299985202799390532181335087622499894389777412111445377637396650710486263652440053717323053536700098339137819966260269752816515681602936416736576044630343136577023173210517247609888936337876211461528203642347119434700140264859102502126842250671976238033270367185358966766106988830596616311824691409766437473419074865115209866730272194297815209976737570183, 18468380817178794606027384089796802449939260582378979728469492439450780893746976934315768186829245395964644992296264093276556001477514083927556578752836255491334765496791841945178275793885002188397918857222419803612711637177559554489679414049308077300718317502586411333302434329130562745942681716547306138457088216901181646333860559988117376012816579422902808478175975263110581667936249474308868051767856694498210084853797453949193117835061402537058150493808371384063278793041752943930928932275052745657700368980150842377283198946138726219378646040515809994704174471793592322237777371900834531014326150160506449286179] # [229904181453273080302209653709086531153804577507365859149808244958841045687064628362978517491609413507875726243121473678430010600891588643092042173698830147997497783886459583186019270582236955524620567373560535686287255124958954671737097645556109314142383275516997850786599322033792080045303427363366927030304214333894247469120513426641296678531965795930756543043851154646310114366477311633838078242963665452936523438928643273392454483600446242320078010627755587492056369779661382734170244060951095344418599686788550312205964136120979823565225768814898285224838691541122088693411388097496320157113230752327025862802020421665288007529320920942060329299409362236414929126050037144149017275031336018100081931062647888329912802477032857776085190828105602067426203163344931483638271679183910241511044338001446584634203146294743522375846913845041274967653508735863706778364499099286484552570083394223973734909997825522191349543295855925973354640349809770822075226834555111927586299176453943116511915434890643239957459427390624136283086434711471863737451011157026905191204496081860277138227247744470804087252965368757930797560277881668806206419629425126031049566579233056222579590529869798537893505779097868221221068867624660759084762471141, 374749619911728044650812367560174497001343067563440477135516664935394734686391543012901514676044211541958613458868769659861216149364768233000844624035620893309356372294598009760824255187442531508754966566917198975934706398309982525100772311586501118200858124845012643495006029930202324305874402291277845166060497038915773767003006049720519011634861166208163030159519901867416488082395270295488885724507937683469910251316231210838654273986152493722244271430422693265608430755620420680629979226285393465423870727975987787149515374769359243334743541460110042872587610309611770320600248289328406805995688596910226273861759369388105641549933915686192055533242723330981192183310876306968103333706140401422550917946410378174896274789619184565321544130428008804628699594759946577979319393247067750024729672029363433673084437510430506410293512293930056667971242862448029841846596288648691077795207341975907335202945548990662460491169957175452745622341245617265849042542964819126377775749222973138584978725470886059043251544634105653274564085280013340679259157119014619894553239015777411757887293044706448625760604242512494466386343040583010961386979963779928616733980046763291988848903515836247301007113187121999960487508948748354549628160741, 111738429639840672983162926852338651562094139707285850255632987705635459657893186493838711733560515475806567653354737245246745810892238414756414117557971683747269900627524702653772058841085258035513296218047505149691384287812041721130367506731427022265277885965948486359682023555050085264531256406043361391744086539522028829421284667293339869140564699750714145488199268791908205712660933607330454849730499840287271163350865799682565216636393526339218836244889719975150503253630419647851422620890082315396457329065508602521784001607236788620811397449483104884860551374031790663030220424841642241965983726516537123807061999084476076850833658360594525986997125319941689903869138176347916707622148840226672408554102717625456819726220575710494929111642866840516339713870850732638906870325693572445316904688582043485093120585767903009745325497085286577015692005747499504730575062998090846463157669448943725039951120963375521054164657547731579771203443617489609201617736584055562887243883898406182052632245189418568410854530995044542628531851356363297989653392057214167031332353949367816700838296651167799441279086074308299608106786918676697564002641234952760724731325383088682051108589283162705846714876543662335188222683115878319143239781, 185935167438248768027713217055147583431480103445262049361952417166499278728434926508937684304985810617277398880507451351333771783039360671467147075085417403764439214700549777320094501151755362122677245586884124615115132430034242191429064710012407308619977881929109092467325180864745257810774684549914888829203014922855369708286801194645263982661023515570231007900615244109762444081806466412714045462184361892356485713147687194230341085490571821445962465385514845915484336766973332384198790601633964078447446832581798146300515184339036127604597014458389481920870330726947546808739829589808006774479656385317205167932706748974482578749055876192429032258189528408353619365693624106394913101463023497175917598944803733849984703912670992613579847331081015979121834040110652608301633876167262248103403520536210279949844194696898862249482809107840303473964914083996538912970715834110371196970613332286296427286356036576876121010776933023901744994067564045429384172315640135483480089769992730928266885675143187679290648773060781987273082229827156531141515679114580622348238382074084270808291251400949744720804368426414308355267344210055608246286737478682527960260877955900464059404976906697164610891962198768354924180929300959036213841843941] from sage.allimport * # 给定的 N 和 E N = [23796646026878116589547283793150995927866567938335548416869023482791889761195291718895745055959853934513618760888513821480917766191633897946306199721200583177442944168533218236080466338723721813833112934172813408785753690869328477108925253250272864647989241887047368829689684698870160049332949549671046125158024445929082758264311584669347802324514633164611600348485747482925940752960745308927584754759033237553398957651216385369140164712159020014009858771182426893515016507774993840721603911101735647966838456333878426803669855790758035721418868768618171692143354466457771363078719423863861881209003100274869680348729, 19552522218179875003847447592795537408210008360038264050591506858077823059915495579150792312404199675077331435544143983146080988327453540449160493126531689234464110427289951139790715136775261122038034076109559997394039408007831367922647325571759843192843854522333120187643778356206039403073606561618190519937691323868253954852564110558105862497499849080112804340364976236598384571278659796189204447521325485338769935361453819608921520780103184296098278610439625935404967972315908808657494638735904210709873823527111315139018387713381604550946445856087746716671838144925662314348628830687634437271225081272705532826343, 20588310030910623387356293638800302031856407530120841616298227518984893505166480372963166394317326422544430837759332223527939420321960057410073228508230111170414845403161052128790464277007579491219950440477721075788978767309211469555824310913593208232853272958011299985202799390532181335087622499894389777412111445377637396650710486263652440053717323053536700098339137819966260269752816515681602936416736576044630343136577023173210517247609888936337876211461528203642347119434700140264859102502126842250671976238033270367185358966766106988830596616311824691409766437473419074865115209866730272194297815209976737570183, 18468380817178794606027384089796802449939260582378979728469492439450780893746976934315768186829245395964644992296264093276556001477514083927556578752836255491334765496791841945178275793885002188397918857222419803612711637177559554489679414049308077300718317502586411333302434329130562745942681716547306138457088216901181646333860559988117376012816579422902808478175975263110581667936249474308868051767856694498210084853797453949193117835061402537058150493808371384063278793041752943930928932275052745657700368980150842377283198946138726219378646040515809994704174471793592322237777371900834531014326150160506449286179] E = [229904181453273080302209653709086531153804577507365859149808244958841045687064628362978517491609413507875726243121473678430010600891588643092042173698830147997497783886459583186019270582236955524620567373560535686287255124958954671737097645556109314142383275516997850786599322033792080045303427363366927030304214333894247469120513426641296678531965795930756543043851154646310114366477311633838078242963665452936523438928643273392454483600446242320078010627755587492056369779661382734170244060951095344418599686788550312205964136120979823565225768814898285224838691541122088693411388097496320157113230752327025862802020421665288007529320920942060329299409362236414929126050037144149017275031336018100081931062647888329912802477032857776085190828105602067426203163344931483638271679183910241511044338001446584634203146294743522375846913845041274967653508735863706778364499099286484552570083394223973734909997825522191349543295855925973354640349809770822075226834555111927586299176453943116511915434890643239957459427390624136283086434711471863737451011157026905191204496081860277138227247744470804087252965368757930797560277881668806206419629425126031049566579233056222579590529869798537893505779097868221221068867624660759084762471141, 374749619911728044650812367560174497001343067563440477135516664935394734686391543012901514676044211541958613458868769659861216149364768233000844624035620893309356372294598009760824255187442531508754966566917198975934706398309982525100772311586501118200858124845012643495006029930202324305874402291277845166060497038915773767003006049720519011634861166208163030159519901867416488082395270295488885724507937683469910251316231210838654273986152493722244271430422693265608430755620420680629979226285393465423870727975987787149515374769359243334743541460110042872587610309611770320600248289328406805995688596910226273861759369388105641549933915686192055533242723330981192183310876306968103333706140401422550917946410378174896274789619184565321544130428008804628699594759946577979319393247067750024729672029363433673084437510430506410293512293930056667971242862448029841846596288648691077795207341975907335202945548990662460491169957175452745622341245617265849042542964819126377775749222973138584978725470886059043251544634105653274564085280013340679259157119014619894553239015777411757887293044706448625760604242512494466386343040583010961386979963779928616733980046763291988848903515836247301007113187121999960487508948748354549628160741, 111738429639840672983162926852338651562094139707285850255632987705635459657893186493838711733560515475806567653354737245246745810892238414756414117557971683747269900627524702653772058841085258035513296218047505149691384287812041721130367506731427022265277885965948486359682023555050085264531256406043361391744086539522028829421284667293339869140564699750714145488199268791908205712660933607330454849730499840287271163350865799682565216636393526339218836244889719975150503253630419647851422620890082315396457329065508602521784001607236788620811397449483104884860551374031790663030220424841642241965983726516537123807061999084476076850833658360594525986997125319941689903869138176347916707622148840226672408554102717625456819726220575710494929111642866840516339713870850732638906870325693572445316904688582043485093120585767903009745325497085286577015692005747499504730575062998090846463157669448943725039951120963375521054164657547731579771203443617489609201617736584055562887243883898406182052632245189418568410854530995044542628531851356363297989653392057214167031332353949367816700838296651167799441279086074308299608106786918676697564002641234952760724731325383088682051108589283162705846714876543662335188222683115878319143239781, 185935167438248768027713217055147583431480103445262049361952417166499278728434926508937684304985810617277398880507451351333771783039360671467147075085417403764439214700549777320094501151755362122677245586884124615115132430034242191429064710012407308619977881929109092467325180864745257810774684549914888829203014922855369708286801194645263982661023515570231007900615244109762444081806466412714045462184361892356485713147687194230341085490571821445962465385514845915484336766973332384198790601633964078447446832581798146300515184339036127604597014458389481920870330726947546808739829589808006774479656385317205167932706748974482578749055876192429032258189528408353619365693624106394913101463023497175917598944803733849984703912670992613579847331081015979121834040110652608301633876167262248103403520536210279949844194696898862249482809107840303473964914083996538912970715834110371196970613332286296427286356036576876121010776933023901744994067564045429384172315640135483480089769992730928266885675143187679290648773060781987273082229827156531141515679114580622348238382074084270808291251400949744720804368426414308355267344210055608246286737478682527960260877955900464059404976906697164610891962198768354924180929300959036213841843941] # 构造格 M = 2^1000# 放大系数 B = Matrix(ZZ, [ [M, E[0], E[1], E[2], E[3]], [0, -N[0]^2, 0, 0, 0], [0, 0, -N[1]^2, 0, 0], [0, 0, 0, -N[2]^2, 0], [0, 0, 0, 0, -N[3]^2], ])
# LLL 规约 B = B.LLL() # 检查是否有 d 的解 for row in B: if row[0] % M == 0: d_candidate = abs(row[0] // M) if1 < d_candidate < 2^800: # d 是 800-bit print("Found d:", d_candidate) break #https://cocalc.com/features/sage
得到D之后使用脚本解密
1 2 3 4 5 6 7 8
import hashlib
d = 4179423138350648633511603754580428752783447242202659775128505849773750010739782037758703319498715813081743032947622794072010600485826874110735478135715298345482643471515080914168066457318628922553866888869769462396548904235402414462199207661
from hashlib import md5 f = f"palu{{{md5(str(d).encode()).hexdigest()}}}" print(f) palu{b1fc01a38bae760451bcffe777e51b1d}
[1970-01-01 08:00:00] System boot sequence initiated [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 00000000fe ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 10000000ef ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 20000000e2 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 30000000fb ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 40000000f5 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 50000000da ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 60000000e6 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 70000000e7 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 80000000fd ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 90000000d1 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 100000000e7 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 110000000fd ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 120000000d1 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 130000000cf ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 140000000d1 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 150000000dd ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 160000000ef ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 170000000e3 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 180000000fe ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 190000000e2 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 200000000eb ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 210000000d1 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 220000000c8 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 230000000e2 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 240000000ef ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 250000000e9 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 260000000d1 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 270000000cd ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 280000000e6 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 290000000ef ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 300000000e0 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 310000000e9 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 320000000eb ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 330000000d1 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 340000000c3 ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 350000000eb ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 360000000af ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 370000000af ns [1970-01-01 08:00:00] SYSTEM ALERT: Time anomaly detected at 380000000f3 ns [1970-01-01 08:00:00] System entering chronostasis mode
下面这段 16 进制串其实只是单字节 XOR 加密的结果。 把它还原为明文只要 3 步:
1. 先把十六进制转成字节流
1 2 3 4 5 6 7
cipher_hex = """ fe ef e2 fb f5 da e6 e7 fd d1 e7 fd d1 cf d1 dd ef e3 fe e2 eb d1 c8 e2 ef e9 d1 cd e6 ef e0 e9 eb d1 c3 eb af af f3 """.split()
for k inrange(256): plain = bytes(c ^ k for c in cipher) ifall(chr(x) in string.printable for x in plain): ifb"palu"in plain.lower() orb"flag"in plain.lower(): print(hex(k), plain.decode())
# Parse the maze into a grid grid = [list(line.rstrip('n')) for line in maze.split('n') if line] rows = len(grid) cols = max(len(line) for line in grid)
# Pad grid to uniform width for row in grid: row.extend([' '] * (cols - len(row)))
# Identify start and exits start = None exits = [] for r inrange(rows): for c inrange(cols): if grid[r][c] == 'Y': start = (r, c) elif grid[r][c] == 'X': exits.append((r, c))
exits.sort() # for consistent ordering exit_indices = {pos: i for i, pos inenumerate(exits)} num_exits = len(exits) goal_mask = (1 << num_exits) - 1
while queue: r, c, mask = queue.popleft() if (r, c) in exit_indices: mask |= (1 << exit_indices[(r, c)]) if mask == goal_mask and (r, c) in exit_indices: # Reached all exits and currently on an exit end_state = (r, c, mask) break for mv, dr, dc in dirs: nr, nc = r + dr, c + dc if0 <= nr < rows and0 <= nc < cols and grid[nr][nc] != '#': nmask = mask if (nr, nc) in exit_indices: nmask |= (1 << exit_indices[(nr, nc)]) state = (nr, nc, nmask) if state notin visited: visited.add(state) parent[state] = ((r, c, mask), mv) queue.append(state) else: raise RuntimeError("No path found")
# Reconstruct path path_chars = [] cur = end_state while cur != (start[0], start[1], 0): prev, move_char = parent[cur] path_chars.append(move_char) cur = prev
if __name__ == "__main__": n = 100000000000000106100000000000003093 e = 65537 c = 94846032130173601911230363560972235 prime_factors = [3, 47, 2287, 3101092514893, 100000000000000003]
result = decrypt_rsa(n, e, c, prime_factors) print(f"flag: {result}")