for mpp in mps: x=mpp[0] for mqq in mqs: y=mqq[0] solution = CRT_list([int(x), int(y)], [p, q]) m = int(pow(int(solution), d, p*q)) flag = libnum.n2s(m) ifb'flag'in flag: print(flag) print('Over.') # b"flag{gcd_e&\xcf\x86_isn't_1}"
import gmpy2 import libnum from z3 import * n, tmp, c = 20947495659013288660808536751393787394664606045798093048128257278988208709333248671898749660848208653968668634891579612784633367362177864996602736258460476691940723323282467207875842974409286563660436709535601954405015261428106261369927836045794170912665351432105165546188591486357060490032334793140396757102052999128194027485573053073959574695808224922102635888141991154365047911830780778957642166757152369955362399379720841279167832886144458760347392316082994786119404006382441787685301119197529946566027319295285387108473752590621030421978808950305190250697199878929419723511578437404000924310974770501204226510397, 12911378830212711575909332427930495830030418987483519620282504671823307660633472092466534392403086505995560725428252134905285658936113891795434303336259751169171583600394870893708505805256284455729584616439559184469715186920464999723861722097244025658190194027561300165723184060071016117033960821040587421503448139025974851980482004179865110864844573575034406782936965166402665401330436229441569042660851847498727291447251591027480750458209012729510702196684303778564353025395186191064801000127420683298000173389589468742142444444759536629401472836827952997758216526858512433131954439154124668711408079361172485321041, 13390681135321846035598057088735733735860895610541899486616159864716324918810264721878447895634342127744578566110322466944217562868186608760962032192994397783118528288276520451944892998435079744244578731427626946331165523865930693902700790185275273534104979885060728696532991031786741950704918951536399577118136416956670893081637730646528913282395731901667720418372650030593319596584787752412110672058692368924987360383096340538971725402687347195347344826404005229912821371282465882351660619944919637382790572267512735645269618163597227604601321699186335016345484182059187046972681187078878556533926780789183784240737 S = Solver()
e = 0o200001 A = int(gmpy2.invert(tmp, n))
p = Int('p') q = Int('q') S.add(q > 2**1023) S.add(q < 2*1024-1) S.add((p > 2**1023)) S.add(p < 2*1024-1) S.add((2021*p + 501*q) % n == A) ifS.check(): print(S.model()) print(S.model()[0]) print(type(S.model()))
q = ... p = ... phi = (p-1)*(q-1) d = gmpy2.invert(e, phi) m = int(pow(c, d, n)) print(libnum.n2s(m))
flag = 'emkkocylnyuwpkxskcahzdvjgby'# 去掉非字母部分 for i inrange(len(flag)): flag_ = ord(flag[i]) - key[i % 6] print(chr(flag_), end='') # ctfshowsinnrs{do_ouloveit
有不是字母的字符,这就得约定好范围(小写字母)
这种思路源自 凯撒密码为代表的古典密码 用的 mod26 的思想
于是有:
1 2 3 4 5 6 7 8 9 10 11 12 13
key = [2, -7, 5, -8, 7, -12, 2]
flag = 'emkkocylnyuwpkxskcahzdvjgby'# 去掉非字母部分 print(ord('a')) # 97 print(ord('z')) # 122 for i inrange(len(flag)): flag_ = ord(flag[i]) - key[i % 6] if flag_ < 97: flag_ = 122 - (97 - flag_) + 1# like 96 对应122(z) if flag_ > 122: flag_ = (flag_-122) + 97 - 1# like 123 对应 97(a) print(chr(flag_), end='') # ctfshowsigninrsadoyouloveit
可知,flag为ctfshow{sign_in_rsa_do_you_love_it?}
收获
除了学会灵活性的处理rsa问题外,还对古典密码加深了认知
这后一点尤为关键,队内已出现过只差一步解密出flag的情况了,密码手得给力,一锤定音!
总之,对于古典密码找规律解密,有:
古典明文以字母为内容
古典算法以模的运算为方法
古典解密以约束住范围为关键
👴不只会 一一对应的古典密码解密了
哦耶✌️
(实测维吉尼亚还得要全部密文来破解正确密钥!此法无效)
ctfshow 摆烂杯 easy-peasy
据说还是签到题(la佬恐怖如斯Orz)
题目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
from Crypto.Util.number import * from secret import flag
p = getPrime(1024) q = getPrime(1024) n = p * q print(f'n = {n}')
l = len(flag) assert(l == 86) m = bytes_to_long(b'\x00' * (l - 1) + flag + b'\x00' * (l - 2)) e = 3 c = pow(m, e, n) print(f'c = {c}')
# n = 23570098360097260908266914080954003513706885568390448463720576188146956287111601604870247233822682630020002051748117520481402155749832096007629686239669192722978473890311246293580246256300691145653885805730865515297134808702766299205253761028235483781919393575836334910012714628455073300114978717047912395404034141622115001451888302617428376998024080880564792294546406688814397757714979980790018933407614160131827023349206138480523080173634617167828920519758917549492886733519381497665596997617399404664611446202014908853898043742964782757859701755202188366852773164911994663079171498909577012208742001609095258401847 # c = 7802816471025387606454709075933309958303134972861203739854774032383575355964299097074709350633475519273512630813578801152996777689686451397201513854823239337669091889776200179028891111154423623765139235199507280104544068425347533121731521700106757148551520759776111224072064131087547685154285120156917449926992033491342535410929828988598182819897435365878832122138552492908774845437576910276499625679469289655841639181182040186931130789588869657335253479416209378458303531995734157105918063227453396477282662136225029972745246845372963076381252679409317314514131852117346576265346926741908194011319430767975281696400
def de(c, e, n): k = 0 while True: print(k) mm = c + n*k result, flag = gmpy2.iroot(mm, e) ifTrue == flag: return result k += 1 e = 3 n = 23570098360097260908266914080954003513706885568390448463720576188146956287111601604870247233822682630020002051748117520481402155749832096007629686239669192722978473890311246293580246256300691145653885805730865515297134808702766299205253761028235483781919393575836334910012714628455073300114978717047912395404034141622115001451888302617428376998024080880564792294546406688814397757714979980790018933407614160131827023349206138480523080173634617167828920519758917549492886733519381497665596997617399404664611446202014908853898043742964782757859701755202188366852773164911994663079171498909577012208742001609095258401847 c = 7802816471025387606454709075933309958303134972861203739854774032383575355964299097074709350633475519273512630813578801152996777689686451397201513854823239337669091889776200179028891111154423623765139235199507280104544068425347533121731521700106757148551520759776111224072064131087547685154285120156917449926992033491342535410929828988598182819897435365878832122138552492908774845437576910276499625679469289655841639181182040186931130789588869657335253479416209378458303531995734157105918063227453396477282662136225029972745246845372963076381252679409317314514131852117346576265346926741908194011319430767975281696400 A = 16**(6*84) c *= int(gmpy2.invert(A, n)) c = c % n # 容易忘,c不能比n大 m = de(c,e,n) print(libnum.n2s(int(m)).decode())
总结
byte后每加一个16进制的‘0’则扩大为原来的16倍
对于低e攻击,flag越长爆破越久以至于失效,所以我们最好能降低flag长度
ctfshow 摆烂杯 比烂为主
太长了,i‘m lazy dog
ctfshow 卷王杯 真·简单·不卷·现代密码签到
题目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
from Crypto.Util.number import bytes_to_long from secrets import p,q,r,s,t,flag
n = 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, q, r, s, t = [145332367700944303747548912160113939198078051436029477960348968315913956664143693347226702600438608693933768134575289286283267810723137895903153829001826223446477799895493265422562348917012216790077395795861238257357035152687833639085415763850743538206986781418939737511715957738982536382066693822159860701263, 116660458253067608044065523310547233337730583902133756095473339390057738510707447906971188577217274861047379404014140178165569604404468897712846876108444468370709141219302291601408652742006268186059762087155933131837323952675627966299810891805398890428420575425160696531236660480933905879208166090591482794763, 157931722402853245421436270609912823260313730941283152856444641969403238646482562190531038393124087232554754746464603598717356255570166081501573727336977292059427220330169044611674973569766966838498453232642731737958791706086957762244686953294662693939604300864961637325536379321027705854708492453330690705531, 100973451687449518854742673778783266158999451072058606348222018797891147675959983616210003484476577612134482311993701677242007759556951494382833070563369964294544839433671087037596159753825249018950693369209927951667775267086896180395776150188902057785214767230658487267587289809918132337927575673868568976679, 93960345071948255233882121683650797512129333868351496468898834736770441398743300745703393838320587998953678254272245400344928586394089488734271897540051673996675973642347859306921527430850673334243441180183460927865980713929789963587608547554858491264614271309608925634272282292964002897650355047792764365447, 9144597920381774885442906257311149465702295057238600973973598305004391534618770363098565074541384771979931799878381439264848137810353858418200992191234142740194489573540381681161219332611454834544291634628456257670178843484698324641739324687497388018406214041657278323855749902661752448796122517061920880552011343608609622885787617238758769398972009949575526258430282648817039091284796330585349957724522615105102735930258969562103112238020133587096826386028128471852377225525357348919204333121695432662339443004327748973224423132988376298843862056631045488285859621661802413201793962883794915513510467912312842687601478117040419013468059983777273699192408773551806581458197324620065210523913467414181480875280203580147077789063808832356486197271376615883221558265591069223727607585313240243619515521180600435114131162272519949101464089935441251751426683447701142156416866113627126765919641034042927519834229168536331952275698122511502745177547569813354280565828372968703810158857859460406828090199683324760956105682902577189283246483314689365570862217407333103243336691401424548702387876409228977278498691200028282744239512091373110111792177228979867318546462714521296256938374618636206565791541769138267080789842400796973226733816939794717596194090232425688504890234304977612220790858557639246367437740975495450011676714198668471438814299689325208882261918460708833888406187912527346628912894921059735420931656953236560178909180587372589456926690219114173193202048332172538564489660440225377822914097420807957784201785024166011709377791129] '''
# r P.<a>=PolynomialRing(Zmod(r),implementation='NTL') f=a^e-c mrs=f.monic().roots()
# s P.<a>=PolynomialRing(Zmod(s),implementation='NTL') f=a^e-c mss=f.monic().roots()
# t P.<a>=PolynomialRing(Zmod(t),implementation='NTL') f=a^e-c mts=f.monic().roots()
for mpp in mps: A = mpp[0] for mqq in mqs: B = mqq[0] for mrr in mrs: C = mrr[0] for mss in mss: D = mss[0] for mtt in mts: E = mtt[0] solution = CRT_list([int(A), int(B), int(C), int(D), int(E)], [p, q, r, s, t]) flag = str(libnum.n2s(int(solution))) if'ctfshow'in flag: print(flag) break break break break break print('恭喜!')
defcontinuedFra(x, y):#不断生成连分数的项 cF = [] while y: cF += [x // y] x, y = y, x % y return cF defSimplify(ctnf):#化简 numerator = 0 denominator = 1 for x in ctnf[::-1]: #注意这里是倒叙遍历 numerator, denominator = denominator, x * denominator + numerator return (numerator, denominator) #把连分数分成分子和算出来的分母 defgetit(c): cf=[] for i inrange(1,len(c)): cf.append(Simplify(c[:i])) #各个阶段的连分数的分子和分母 return cf #得到一串连分数
defwienerAttack(e, n): cf=continuedFra(e,n) for (Q2,Q1) in getit(cf):#遍历得到的连分数,令分子分母分别是Q2,Q1 if Q1 == 0: continue if N1%Q1==0and Q1!=1:#满足这个条件就找到了 return Q1 print('not find!') Q1=wienerAttack(N1,N2) print(Q1)
相邻素数爆破求解q、r
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
from sympy import *
q = Symbol('q') r = Symbol('r')
qr = 24000465830725710567609789107532324217407562259754653134149621603287774548503009977306790950765445986759457303910590756566445763189685864660421027894624160496364130162710203056019260859349197460287247433237333855820008311336990774902044072459096643794460636549390819914523204123744097150936438700304426418945328341075502713761684579112067780970129462407486292351679441221444463557410246953331098106055732155261630435421138374418050504535474800628754964367103623724936338041432343848497819682915988508211563927368529558866912220856126132024687748646133065560766894641603193094511181987021442275364527221213343560893411 qr1 = 24000465830725710567609789107532324217407562259754653134149621603287774548503009977306790950765445986759457303910590756566445763189685864660421027894624160496364130162710203056019260859349197460287247433237333855820008311336990774902044072459096643794460636549390819914523204123744097150936438700304426419195181206357035254463948831711777035126077563377919684721062749439252724456974588213705559491260601777470792467850627398598289093915869715222818079606170004475810187367332322557611964471968738992807153326972570559359950761752890888829934863609591544119615229826879764045397320279541476417371138268264031865992001
for i in range(1,2000): if i %2 != 0: continue for j in range(1,5000): if j %2 != 0: continue if solve([q * r -qr,(q + i) * (r + j) - qr1],[q,r]) != []: print(solve([q * r -qr,(q + i) * (r + j) - qr1],[q,r])) if j % 100 == 0: print("i=",i," j=",j)
with open('rsa_16m.txt') as f: data = f.readlines() n = int(data[0].split('= ')[-1], 16) c = int(data[1].split('= ')[-1], 16) e = int(data[2].split('= ')[-1], 16) # m^e = c m = int(gmpy2.iroot(c, e)[0]) print(libnum.n2s(m))
from gmpy2 import * from sympy import isprime,nextprime from Crypto.Util.number import getPrime as getprime ,long_to_bytes,bytes_to_long,inverse from vanish import flag import random
def Getprime(g): while True: p=g*random.randint(1,g)+1 if isprime(p): return p
from Crypto.Util.number import getPrime as getprime ,long_to_bytes,bytes_to_long,inverse from vanish import flag from sympy import isprime,nextprime import random
table = [chr(i) for i inrange(256)] skr = '' for i in product(table, repeat=3): i = ''.join(i) skr = hex5 + i print(skr) skr_ = decry(skr) if skr_ == sha: print('OK') print(skr.encode('hex')) break
n = 17641791540530228694286135185364074270246417140538172343866345466143247980659455424376045685494166279876110132203178803899115680922576583783724143783790241934890713111735488241729348106606502962464794620278410346388397359673075821123140990941942289674002895075430115017639376231448894324816030053817634283366169122125231411123494823295007054961127500058834017497105743260495984200175488365511990260484741462938371341033116032935020587367904271953619183649263518669540687237751562870973258276042987062056763970912250995314217713966476714492906919554248815811082015216774212685970371820986012163337575801128737022100901 mbar = 653524334392227600317610369760395325667407883325756386751869881379847068844170310055337360621568 c = 279116358436840580121751323591931874995783037840229208462822785113356478604601761572940996937612929433055849494515561733423792677427130977192018567922037737644911574834397373382160864107212935056196095912820832399095116695115669158974901004546975979648317608777413620402597811562020634469 e = 3 kbits = 72
PR.<x>=PolynomialRing(Zmod(n)) f = (mbar + x) ^ e - c x0 = f.small_roots(X=2^kbits, beta=1)[0] # find root < 2^kbits with factor = n
defde(c, e, n): k = 0 whileTrue: print(k) mm = c + n*k result, flag = gmpy2.iroot(mm, e) ifTrue == flag: return result k += 1 n=91046679824709970162166510393984349436507160355504054099795745710843918232291158893173392345202539943129024459721861867023537200311589378076393008186869632816007290025155710690087286079985123881126900629981074014645167000252730113633981429235819837855381591602381430468841314334641325933457672374936644601001 e=3 c=279116358436840580418940102891469361711985570618086638410015681467792988932869992663175128601830997392899009434533165482291158081783499352092021651217760633821487767077178436162836170197174297227386919476479085259086862455979023948465984342333447127816627669958590898325507691223787985253 m=de(c,e,n) print(libnum.n2s(int(m)).decode())
import gmpy2 from functools import reduce import libnum
defCRT(ai, mi): assert (reduce(gmpy2.gcd, mi) == 1) assert (isinstance(mi, list) andisinstance(ai, list)) M = reduce(lambda x, y: x * y, mi) ai_ti_Mi = [a * (M // m) * gmpy2.invert(M // m, m) for (m, a) inzip(mi, ai)] return reduce(lambda x, y: x + y, ai_ti_Mi) % M
e = 3 n = [108736816241175665148273398258758080018960946338287263364569374726302196169162605670881735552997454446042995869651449282714639817085183228827146377987324938368188209657339192168670905752630070180008635819292194863047273313500409087217920196986541427006384815578060250088479783691454217221007011624615314023899, 125311072436452106223310243651666447263593613934983230425360328449529562394786242626857578081604150972806837244925660405048300400604492591126681020111390674731777377449723245786875368033901438466732844507365503701957616443294592677795315630591380298492356628165441987010153245453234081489921029340196899395331, 145952553475400806121889743433836062205241126114628994131042124255411306520718902459725859533701853065633604102288257122762822143440371454643188884668825711330843803637043792113112949317335498541040720711359198080384919545170988381565195230298044019615673547840211274883413248354873966128295173063309521168793] c = [279116358436840587095249496686254280407332053342087519248181392057376438009454609418875798499072335086788124425593780862179185140902876002463710935769268790619149829113441912759030007987894680912774697528843208252759906296795513540716051082272447068540992498830366255300897173712420417125, 279116358436840587095249496686254280407332053342087519248181392057376438009454609418875798499072335086788124425593780862179185140902876002463710935769268790619149829113441912759030007987894680912774697528843208252759906296795513540716051082272447068540992498830366255300897173712420417125, 279116358436840587095249496686254280407332053342087519248181392057376438009454609418875798499072335086788124425593780862179185140902876002463710935769268790619149829113441912759030007987894680912774697528843208252759906296795513540716051082272447068540992498830366255300897173712420417125]
""" # 五进制转十进制 n1 = [] c1 = [] for each in n: n1.append(int(str(each), 5)) for each in c: c1.append(int(str(each), 5)) n = n1 c = c1 """
# 利用中国剩余定理求解 m = gmpy2.iroot(CRT(c, n), e)[0] print(libnum.n2s(int(m)).hex())