Feistel

Feistel 结构算法解密

题目重述

拿到题目,题目先是给了一点密文,打开之后,发现是一段乱码:

Ltf/hop5'ckv`o{yjja}' vaj/|}ai`mbc|a3`trj#{)tyu{iaan.odtrfeejgfninfndfss{nxpoa`oug!n3rCv`n)hgaqumg;;e}pynec'1rra.k`gffyettt|fnge&ue/}vlk,s&cyrg~wc;zdhdpsasf1p`rfolbtb+jta}tnn}h&gvn}s{o angaa~i"s{zngelz}6p"iWp'm1=""SSaabbxxee&&hh||ww11aappjj"cxnigra`u~wya;t)hCrpclberhl.oyaigdp3rjcppvagc/nijiivspa$$lnnaav,x|m|zf}3t~5ne{xtp"gt l}aopq((Ghigda8ybeqecncnvn,mddprprvrettagu#b{coaqe?ae]inad-oiii|/xdgfj//cv|ycv~x kU>i&w1/Nqvhdjjuv"vji~tyl aefjnwqgoan/ncu|tcv~w aksigfr/nstnfcnjgkcjevp}aa/ce"| e'%Zt3rrjf)c ahepfa byike`{.,9;8z"i@z9|cujnhemooahdvr3rjrjdua*e7tsWahaaeatha~zjy aphvxc"~fq{2fq {or{fg{jdl|w}js ,b eUuiska//ysn%`m/o vc(i{ugsra.orv{tjn`ljr=r aIafa2sg`nzn6whaddubea1?xoOtn|45Fyc~c|` ~$pNser$ao,h.fy}wjm|(#o{c{3mwbp/nrgoa4uttnahpfh`oae&gaajeda.oeany}y}nzb`n}raeljvwlmp{mpkeukvt.xzI3vn|azt`h-|}eebc3~Ifbch;{mpb6y*e*e|ileea%draquobzrhnfn|rxl}o augvrobaf6z|o}[`j0geekq|jub##//f{~xgaj&urbcadjmtt7~`afzfd7yhzrqdeqtabf|n8q&n/nnn~q`e&gcatgaiiezryvnnrft9v-lu�~�q�//]vrepqxbsg:u nSof�0�h�o�r�,�f�e�f�}�;�n�hz;ztznsvpvnbhlna`uaawz/b�q�z�o�d�m�s�l�b�q�z�n�i�f�u�f�t�*�s�m�q�5�t�p�w�w�c  v`kjwa,hshq(pajkge}n{szoiaikaadi:u�t�a�y�y�e�g�a�v�d�rmBzgiua%dl|~v`nuzvjkutft lt$bskkts}pva3ww }l``)|n(n/qCak)hpslhws}r%dha`trapeafqehe=p3raruaqtjn)hig}n'fne4m}zeuo|tu{j({/iH(g(q.o_njnvefommapwe/n~fgiua2t{vcn3rqr~`eopq/yrc|fk./mcq}~e~vwe=t sa5kjnat %mpqqy##nziivaqu/nng}u}o/n{u|s{vjr3rBpoa{t/b/ntamdyy`u#b~hci//appluua3mtn`lr(dld|5o}fl. ;xVbcv`.kLf`aw3)}bgfn`e$e~q|rwb/nsaminaqt)hkbiahz0q^lke`o+jfn}r}hhrr3gq7rgfgqq6`wezf{)ojdn;;}xmmgmd,gz%vb k`1~|{aaervrsv3rzo|udwoa`tnhkb aszyqie.a,mDrmdoakogpjndeameawq4g{po so#fm/v{`mf|j/vnak4`|5sb/jt{`f~u`reaconaaepu�t�a�f�b�n�r�m�n/naahefuptaaknnv#n0qfecjvy$aWwi&jm n}2ig)`Gmc"s5')"Wq{n`n/nnmjngjgoeadsrv)hia`o&ggnzram!nc/kiowdj$4fjiokakrlvee//coaapy/#n gth%ja3rupvmdfnpgea$e|ufnam*e;zBaua{n'f|i{u/naadila%ddrv|r(ihk}77rhxhy33ayiut`kw{/$ep${d"pm`aten`jipk. �n�ahgo{vbgevhl}r3roaur#bHnke`e:u,c!n&e2gnLeko azlsw##|Wkankk!e z\.o$$Bjaad{xd`v#useo.ooka''thcbnbta#bdqao/ndnnagiwa$e`imezv-ldrbbsa.org`n'fkaej`nO�f�l��#�y�h�8�l5l$u@ey'bly}8g` h}hit(ocnfkh;(`xz|opdomjbvqrz`yrzkezu�p�f�a�y�b�a�h�g�i�j�n�z(i�k�$�p�m�d�)�"�e�a�d�u�i�u�h�,�T�|�m�&�h�e!|  Bypyae'f|mrvfhpajgea(ijxaarzkn,mgr{rhhrg aFmjnsacwu){eeiym|tll  dqi{|te`( w\slbaq. 'k-w4Kkgw m`nuroheajy}yqt apatg}soani aQzfbvduavd8y|ubbfc annr|o#is}gh}'rtg}rolwano//pp|}hs$lg0jktoaw'bqrb}{w}rgb,a3rlnhfuaytdaje,j{n'fcuffhe{q#n&gkeuf3ryozsyhoe`.gt.aqp{ilf2&j1t\4ae"psrd`isdt,e;o,j$&dneuamakoqbaopd`r(i}skirpuuwekcoakdsa,a aozhaeerg acb8loogvq#j|eo;w`me`&sp8jc`aozhr9g5/A}`m$srtt/fafjytzbn#b{nha~z$e`ecgaa|y!n aZovtt{n(nwe aap}ntdmewmtfl ,m!ejg}wz}43~p{r3zsn`b}84vnvsd imazp/#yakeaugt ahmqbvrssbn`rv=.Ic|awf)poenh,/{g{alg&{i0|fkj|/gc8t5dr6gaual|q3rzo{rrr}h&ghaim/nprehggfn}qkokn/n`uzt7vtwea#lenfpz#z)&dxzmx|/#komlsa$usg| vokdh`/sg{c3e,a?p a7t[nahcg/nJmnnwbdmeave4utuajoagjnug/n}rcm5tjdbweale5gn-n/yW2z{|ak~f`jrr..og`vv)zU&`ajk"z  dfvbzhta5tbb|n afs{rkoiq/nhaaouz`nar)hrggnae.oFaoon cxex))tyz}vf7;U3ilplkkk}y0ux}g/`g0u`rfqxet 8uhaemzv'h aFsarbvrqi4y$ezo}neqcnvn#n0qZr#bewaaayy`lp}}((krjzmgar mdlch t{?d%sb,yfwg3wz}n~ce$kw2sbometsmi6y/nKbpt amhigea`r%dnacmwb3rqs`n}qae'f{s!w0(
 VM`zwv  ycjkuslpe1'fsf}}f}|lj/ui$g4c79O dfbbg.a/nJnvceafumbsshe1pkfmetsmi8yfntf(isugteann"xIa}jhu}qgolj(mz.ppfzsn gw~}fh}j/.znj}3xutfd-lckm7vvnhf}i aeeziko8ycnoava~zpu.omnwo%dTrxvgb#lisf4|ar~`~ap?c$bKwvpgg1}~b~$e vf}a cpaaqjkbtl#jt&gcu`i|r.oyt~prews`r3rhptz}i,a$eheoavaaeky1pzbke1sHjkg`e(}mwse//goj|l `l st oh;r`x7umhl tw{itzj1pnaeveniaeca{njgqi.a%dHaea awz|ravfb~l a{oug/na)dqmlg elwm,v.kv8euvpp5ag-bs othruww$.ogXaj/5fhhidob amaaesa}o8yuapa;t3rZlea;zffidhe avqnnfnyt~b-xqpx,vg|wdrlkjwlzxf!n3}Tuues)77HGhbp4 avqiett~lcl%cxm7vsscanl}yxij/nfrew aefvv|nbfpu a}shg1p(gbdq4cfnia$xam}$vk"ik$mxffjjt8:'
gPxbfnx..`}`|(iha}ubn+f$egofirekcsfae0qgnnfyu'fpeq/nijko4uHngbjko~.ky|//`iil,n/cdf`|3kmdvku`l/.Gc4jolwhv%`fomeei4oy-nLlm a{nvjn3rCdvvwbahjn|o4ukbyysa%d0}Fvi/,`btaxdt/#l'q~aqtw4`x5vn;hc wQv;bggggwvg*uqan{nm7z.ohp`nrf4usemexuyt/nzzbaqyzjn&gxp}r*kk&j|~`{`|d/jmbz4gw8wa mv~xkzv?##itvp/ndxi ku#zg+igynuq!n/n[ndmqupb}y a{nvfn}rdbsa8yFn,mhayyubnjf"u4ww8pyajqf3en#lgxo }h(gmvuqv/1~mabb  faf}|e} o4uWbpyiavd3r~vpb-lma}e(ica{ubn|pnnifdamavr}r}h/l8yDvoidvw# ihfh)vxgn~a/q`(m}st{ nw.xv`5/}yhlldnlh3rofldnb auayh&ggiej/nkc|i+u2sWafovvat/nel ik~q}c$f`ai`ua(dooolf{/$evjq4ypvqpbietxg}}.ebfapa8yayke}i(ingiiqfko"cgnee`e4u|tyv$eyatz|h|o-lcn|3gA/gjaa$akaopv}qc5eiwv{r&g4f@fiai`ca50{-eGv{3zrft/nmewe>aveaupt a}zmczi4uwsvakoevlt`nclvr=rr #Dkmeld4s{csz.7p|z{f-c{tr3st;z
 Sb~t$(2sa(ts&omabbicmigu2ssakoyvftjn-lgr{raaat akapgiesainaagO'm`1xswy0ckv*ycfi~aei~88qniig`r 4mol{gfrf#3fndfki~pvetqfn}rmk/nxmoa-lpyaiug2sla{uubws abb}rcfiaoken|/=|)Ye}`z//vkicwf}a/wot|ifzq#/a8ofsirr(kfaranjjn3r~hia~scn!n&gJcjn/ngbzteagu#bvyoarg a|ivu}g=`1uYow'($7^~ sc%lhuinn||/a{syswwq8y5fa%kfckovraplaks+jmezvgvra`dpbsaea|y/ntwya$e|u|tsa%dlrak4yt5nwam3ryaf/bwak,a
iIk{`ro  rcy.ma{`nzz2wgake}u{tga%dgrzt/npeeeicea.o|uwsa;zsq{ojn|n.ots{g}~g"p&edzg`vaw($'odez*j/z@h&(cdy/derb'atnjlazw.o}rkocgda(i`rkegenfeaatvn)hrg`n0qhnbvnfdagc akbr{lvg0~so#mquzzv~pt44Rb`fk{ hd ihkf3~m*`/F}0yh7valzrqvea/b&gesvaezu5tFn/nbaeagann}r$eoo/nbqeal)cxxpnjgo;iq8ns&nHf|uhx%)/fqmi,amvkf/xzgi5setg!lh5tSpiabawz}y aggmn|rym}h/nkbemma>s/nawaaft1pnna xyoza8.lmIn em"~1umkb3io ljju`*ojtotn qchrwm auueblyeave(iaa}ozpiauztr,mJa`d`rktf aiakdtakez`z5tdmkg$eyp{2ga /bljjcanl=g"}88Kmlhc  ka~puii4uytiabeae?a6u2sAaankn"cIa"csayyqy`e*g a0sFphazzr;jpgot'#axjy0{}r*kyl}~zv66allk`w1ewkkjnr/agdz5uz#eta'fnbnn{qjn7v`koaysjnew.a"c]e~p{o$ehzjnfbxtv}nata}/r~qg`3`qdh.a tM$9Pe(}onn{f#/7q}|trael?tgu/nkgvn3racrjdsaeasape9xnijyfnptansv5tepealatyanwad{5/mkpc&fxbdce2fi(vkkg-h`,iw|ncdj/{{f`ycek4ufsoa5tgnlhtg.nm,eAs`i}y/nzlmmsa/ndv`n5tnnzvcct6/|d asw|yq`pf#wZ''ccbe iyvhpd/ wBiesu&uh{i pmtp{noa$e|uuqve#brnfqvhl ayywz|noa&i)agHamiraae aaqvvk,aokc33sz7~a`g"hffdiew8v~~bg}m$ti/jq(k"takb3rKmoaad?r2s^v3r`cvrjhssma}y8y{ljnve}e/nggucWmelilj$sy|j4gKq)h{koeceo#mo rgzldo.jI oidj$;o{`kbhl aFe}umnxugc)h{meadwfb)hla|tbhko.opaeavehz`ncaj/sb;hb#i,!i/`/uI +eh`usm(cy5pua~};n{}cbjmle4|tanpv;zHncmkn/npfaaxj/nwfziquattawo;zbnjjgb"cuu"mpj|r%im en e$l1u`4oan`tr..Gljjgu el woxoj|u&$iwcwe=rdAanada)hXp4u`fznea}osa%dnbya a}yco`nrgme3faffz,epjc,r({pkhnp?{/,EM$$wlz~zl}tabjrs**mtg`arvr#bcmfa&gaafi$eea|p`nxmmeke`ozzcnytvn7vykeaki7rr}m{mculph$}a.oRg/ur}j/lnaikap hec&>p7ryyhtrvyv3rnn`coaqepe.ovzvncqjncnin*e$eAajebg2srv{liawrn r|=}--Is$qjic|tnod is{`h|'hkE'b`ghuwa33`bveociattr-lbamelj`nsfraya5tFn)hqgnntf4urasszrasrvhmpwxfc.$pg{vvs2l{a{.{xy|g&*e$ifka8|j-bc~del)&rafky=p.olevr3rasnnph}o!l/n$g}ehfzn8yzl~vzwla)hmgzu
o[fu3/|a}{qg2$g`jkoszfhja.}a ;ruia$|iu{rsazi&{ttfzszrn8ycu{spejc.o^ujnwe{osa7z'f}eqau&geiogrt7o(a{y}j+%f.oO}d lwwi&zH&ir`|5jd~g.abz{|o)e rBf`7z(i{u}n+jfooa}k awoigzn&gweetjnghjo-lvnhfuara5t8c/|s;mB4qx||`c|{;66jbbai}n/hzf4qvvf.{ba//tldhrujn5z(iUu|n~t/nvvxwzb`nwxjn+jyo|u`n)hhoblcntyblataw7eh)`bijbtgmwq``d $p`{g/nhgf|a/`b}{wk-eh%b|Haws7v}hcgancfwe#bqdoa$esufn}jko.ozn}o2sjlranjnn~d|j's}(z{jix s`ndmcluhlgm)c s_6ww/giafai'3mot/z{vfnlfmi4ugnea#bljgonyhlpb'fyaywvb/naa5tifqulz/ntna{$#|a&usn}?hpb$"}jjicangoriaz1d`||./kzfglso cwfjnqbniigrauymejehn/ninhfzi aRt~pkn o/nBbfibg ag`lpr9f)fo``o lilzmw#qtmeggtdma4zde}/m}ciazt0|nv`hpa5t~hgozoha aSulb|y.a&gIa|i4ukc|n$eAg`ntq/nji|yr~c{uv%)&nnfyva.qmsi .w;nObam(&i8~Giaa4wfwa|sfjnev|n3rfhsg}n;zbomeqfeahz'fse`imivy/nsequ#bfnm$jagd22olnrsa/f#-j+tY;tfr11|lgfh}xw#(`jiewn8avhe ocXseahzrfpb#bdjfa}zbfcpdv)h~f`nvb(ikgoajyaec7fl#j2fe jp`|jjr wet`fcyqx|vjhlu)b.n!{r3y|~a..xvst/n{sta$e|unnsa#brt}nrvttgr$euyhfmajn}ratas"osgu3r|eht }q{ua}&r`gi/5pnq{j#qf"oweisl$hf&o
`C/jkavrvrvg aaugu/n`gzncnyt)f/nBblcda anake|iha}uane#zfra2{`wc n#g tI{3zryzt3.z`|~sa zl3akbvv~it th/b5ttazsrrxm$bqc aarvr{vzroovuzwea o3rMfrv~luatf&&b|v{,m`}yn/y{kb/yibgz`4/{ooqmzfuarc}!;5'>a jLbg/ndrdm|rptynptuf/nefmixufnbfrequas7vfnyv'fmaae22yaiull|:=3#:02-J#-9d~ljc`}jlygaa|j22lihh~aj/w}2szooa8yyutp#b[s#bzl~vehnc/nanjnreko`s/ndfrr|npe Wx|jfaie "Kt k`qqpi0ux/|gz`x4fi$jaode2aGawh/#wa{uqblf,a"ciamb0qee~qvn3r|n}uvcqeaagr{soavy5z/nma$mojd-~z/|g3`h#lzqen gt }{z`z)rjx`ii}dg3{wjz)ah]ehfxlar/narjnobyytaiaoaan|nahogn&gxnzrgr{r'f`z3gbxj/`ys|ro{c|f}v`3}pixxx elinnap|z&ealg}y8;53rJrqzn,msstgws3rrr}r5tiafidarzqf|tpusghhbo4ubtasa,z{.g$hmbbshdkags>0(|Zgm(qyka7s{foqei.jDvaoa~paimehqc$ebbifil/nmuq}i/idi|q/ndvjnbf aDofb}ogswmz5ycu~eec1yblyct/fbg|m&&top~m$wo/ayf|dggkkwd  `u}tvvbw aXp0vdh'f|nkbgiqgiavyce$evnfhfr.ojdmbla$vNfikk/#qocsa5yzxi77kduaufqgs2ntwkvzbi7py~h nfpe)hg`nvb&gwa|txujg7vnmea4upbzsiafbrcfrvil ain{gz#c$kf1psgzgagm}|gpr`bptls&*`3{}vaqx|nqv$a4{a|qu}nmelbdpft3rfn`m~s}rlhbpsa*g3rnngewufnfcmizufseirst1/c``}h-ha!k$(Qqovvv3|lwbdc`/ah&i|(hthaag4{tahatp}nlh.odl|n$e{iuaeburjnqbdlanhn&gaina)hat|u mlort5lj%e cx{ae$mpz|5pl#ib/fHkhtp`43`|3uo#kmuwea"o7v}t`hsa1pnveaeguptmblnjn/nffzuwr3rzrmbpejb*$ cYpj}#fxpirt`{?q%$ebze nsr/}ukkd(i{~f(pwmc$esa~pbnfb5travrrridhewo alnvnn`mhetpfiusaijeeca$5zfff)g_4qlpgf|gp2r wb.obXy```dcr/}fzf 6bGbedwe`tsa.h5tpujb`rwu|tpaheea}o5t'nrryvkn)hmawwclyalgczt}avh,jdfabc"g{{a`psfd`)r ibneus>d.kBj`/fc5|lhvoppbn|midvr=p"cXiuucwla%dwaaasa2svq|oae~nauptu;|jjj/kysn$$peaficbu|v>0$iVvl`p=v3+Cytr7)~pfn|hz(ihgjnyyrahe}e(iwesf`n~qae}x"cgr|r+juprvhhsfgnr{~d,f5pd%n/~qn0dkjfij=`6Utodhlyf|}~zv}%22oC}wxvil,a5thhantq(ivqcnbfvrzrxwdb-l~yao`s/nah{szh2spkjn$mk`sw|kj37bomd~vhwszb+`#bYbels~##_lqqj}g#f&gnnjejo2s`ngrvfbl apslhidjbeavabfqb|n

先来看看题目代码:

题目代码

import random


K = ???
KSIZE = ???
SIZE = 96
Divisor = [2,3,4,6,8,12,16,24,32,48]
PSIZE = Divisor[random.randint(0,9)]



# get test msg

def clear(ttmsg):
    tmsg = ''
    for i in range(len(ttmsg)):
        if (ttmsg[i].isalpha()) :
            tmsg += ttmsg[i]
    return tmsg.lower()



fp = open('tmsg.txt','r')
tmsg = ''
text = fp.readlines()
for i in text:
    tmsg += i  



# devide str into part (each part's size = size)
def str_size(string,size):
    s = []
    for i in range(0,len(string),size):
        c = string[i:i+size]
        for j in range(len(c),size):
            c += ' '
        s.append(c)
    return s


# Encode function
def fence(message,n):
    part = ['' for i in range(n)]
    C = ''
    for i in range(len(message)):
        part[i%n] += message[i]
    for i in range(n):
        C += part[i]
    return C

def enCaeser (message,key):
    j = 1
    C = ''
    for i in range(len(message)):
        C += chr( (ord(message[i]) - ord('a') + j ) %26 + ord('a') )
        j += key
    return C

def make_key(message):
    last = ''
    key = ''
    for i in range(len(message)-1,len(message)-KSIZE-1,-1):
        last += message[i]
    key = STUPID(last,K)
    return key

def X1(message,key):
    klen = len(key)
    result = ''
    for i in range(len(message)-KSIZE):
        result += chr(ord(message[i]) ^ ord(key[i%klen]) ^ ord ('a'))
    return result + key


def part_Encrypt (message):
    
    C = ''
    key = ''
    key = make_key(message)
    # C =  X1(message,key)
    C = fence(C,32)
    return C

def Encode (message):
    
    Cipher = ''
    part = str_size(message,SIZE)  
    for i in range(len(part)):
        key = ''
        c = ''
        key = make_key(part[i])
        # c = part[i]
        c = X1(part[i],key)
        c = fence (c,PSIZE)
        Cipher += c
    return Cipher
C = Encode(tmsg)

解题思路

出题者提示了我们这个结构采用了栅栏密码加密,所以我们肯定要熟悉并破解栅栏密码。可是如何破解呢?后来我们经过讨论,知道了我们可以利用分组长度对应的因子,实现快速解密缩小范围。而且对于Feistel结构,它的子密钥是密文C1接起来的,这让我们有了思路,我们最后再利用重合指数检验爆破的结果。

实验结果

for j in Divisor:
    p1 = fence(part[0],j)
    for k in range(96):
        key = p1[-k:]
        p2 = X1(p1,key)
        gp.append((p2,key))  
    CL = []
    for i in gp:
        (ppp,gk) = i
        clp = count_CL1(ppp)
        diff = abs(clp-0.065)
        CL.append( (diff,i) ) 
        CL = sorted(CL,key=(lambda x:x[0]))
    (d,(p,result_key)) = CL[0]

然后再破解分组长度:

def guess_key(message):
    pre = message[:KSIZE]
    last = message[KSIZE:]
    possible = []
    for k in range(26):
        key = deCaeser(last,k)
        rev = key[::-1]
        possible.append(((pre+rev),k))
    CL =[]
    for i in possible:
        (ppp,gk) = i
        clp = count_CL1(ppp)
        diff = abs(clp-0.065)
        CL.append( (diff,i,gk ) )
        CL = sorted(CL,key=(lambda x:x[0]))
    return CL

解密函数:

def Decipher(message):
    gp=[]
    part=str_size(message,SIZE)
    for j in Divisor:
        p1=fence(part[0],j)
        for k in range(96):
            key=p1[-k:]
            p2=X1(p1,key)
            gp.append((p2,key))
    CL=[]
    for i in gp:
        (ppp,gk)=i
        clp=count_CL1(ppp)
        diff=abs(clp-0.065)
        CL.append((diff,i))
        CL=sorted(CL,key=(lambda x:x[0]))
    (d,(p,result_key))=CL[0]
    rest=guess_key(p)
    (d,(p,result_key))=rest[0]
    return (len(result_key),result_ksize)

实验总结

通过本次实验,我们确实深刻地掌握了Feistel密码结构,知道了讨论和团队合作的重要性!!!

你可能感兴趣的:(Feistel)