# -*-coding:UTF-8-*- from sys import stdout miwen = "KCCPKBGUFDPHQTYAVINRRTMVGRKDNBVFDETDGILTXRGUD DKOTFMBPVGEGLTGCKQRACQCWDNAWCRXIZAKFTLEWRPTYCQKYVXCHKFTPONCQQRHJVAJUWETMCMSPKQDYHJVDAHCTRLSVSKCGCZQQDZXGSFRLSWCWSJTBHAFSIASPRJAHKJRJUMVGKMITZHFPDISPZLVLGWTFPLKKEBDPGCEBSHCTJRWXBAFSPEZQNRWXCVYCGAONWDDKACKAWBBIKFTIOVKCGGHJVLNHIFFSQESVYCLACNVRWBBIREPBBVFEXOSCDYGZWPFDTKFQIY CWHJVLNHIQIBTKHJVNPIST" Ix = 0.065 ziranpinlv=[0.081,0.014,0.038,0.039,0.122,0.022,0.017,0.045,0.071,0.001,0.004,0.037,0.034,0.070,0.072,0.028,0.000,0.068,0.063,0.094, 0.025,0.010,0.015,0.002,0.015,0.000] print ziranpinlv.__len__() huifufenzu = [] huifupianduan = "" def keylength(miwen): #计算秘钥长度 miwen = miwen.replace(" ", "") #替换掉密文中的空格 chushimiwenlen = miwen.__len__() fenzumiwenlen = chushimiwenlen left = 0 right = 0 max_pinlv = 0 key_len = 0 for i in range(2,8):#安装key不同长度进行分组 while 0!=(fenzumiwenlen%i): fenzumiwenlen=fenzumiwenlen-1 jianju = i right = i fenzu=[] while(right<=fenzumiwenlen): fenzu.append(miwen[left:right]) left = right right = right+jianju left = 0 right = 0 jianju = 0 mm = "" for m in range(1, chushimiwenlen - fenzumiwenlen + 1): mm = miwen[-m] + mm fenzu.append(mm) fenzumiwenlen = chushimiwenlen mm = "" xinfenzu = [] for q in range(i): xinfenzu.append("") for w in range(i): for y in range(fenzu.__len__()): if fenzu[y].__len__()>= w+1: xinfenzu[w] = xinfenzu[w]+ fenzu[y][w] pinlv = [] for pp in range(xinfenzu.__len__()): pinlv.append(0) fenzupinlv = [] fenzupinlv = jisuanpinlv(xinfenzu,fenzupinlv) for ppp in range(xinfenzu.__len__()): for pppp in range(ppp*26,(ppp+1)*26): pinlv[ppp] = round(pinlv[ppp]+ fenzupinlv[pppp]*fenzupinlv[pppp],3) pingjun = 0 for oo in range(pinlv.__len__()): pingjun = pingjun + pinlv[oo] pingjun = round(pingjun/pinlv.__len__(),5) if pingjun > max_pinlv: max_pinlv = pingjun key_len = pinlv.__len__() decrypt(miwen,chushimiwenlen,key_len) def jisuanpinlv(xinfenzu,fenzupinlv):# for l in range(xinfenzu.__len__()): for a in range(65, 91): zimuchuxiancishu = 0 for zimu in xinfenzu[l]: if chr(a) == zimu: zimuchuxiancishu = zimuchuxiancishu + 1 fenzupinlv.append(round(float(zimuchuxiancishu)/float(xinfenzu[l].__len__()),3)) return fenzupinlv def decrypt(miwen,chushimiwenlen,key_len): i = key_len #进行分组 xinfenzu = [] fenzumiwenlen = chushimiwenlen while 0!=(fenzumiwenlen%i): fenzumiwenlen -= 1 jianju = i right = i fenzu=[] left = 0 while(right<=fenzumiwenlen): fenzu.append(miwen[left:right]) left = right right = right+jianju mm = "" for m in range(1, chushimiwenlen - fenzumiwenlen + 1): mm = miwen[-m] + mm fenzu.append(mm) fenzumiwenlen = chushimiwenlen xinfenzu = [] for q in range(i): xinfenzu.append("") for w in range(i): for y in range(fenzu.__len__()): if fenzu[y].__len__()>= w+1: xinfenzu[w] = xinfenzu[w]+ fenzu[y][w] print xinfenzu plaintext(xinfenzu) def plaintext(xinfenzu): fenzupinlv = [] for l in range(xinfenzu.__len__()): for a in range(65, 91): zimuchuxiancishu = 0 for zimu in xinfenzu[l]: if chr(a) == zimu: zimuchuxiancishu = zimuchuxiancishu + 1 fenzupinlv.append(round(float(zimuchuxiancishu)/float(xinfenzu[l].__len__()),3)) k = shift(fenzupinlv[l*26:(l+1)*26]) mingwenpianduan = shuchumingwen(xinfenzu[l],k) global huifufenzu huifufenzu.append(mingwenpianduan) print huifufenzu huifu() def shift(fenzupinlv): Mg = 0 max_k = 0 fenzu_k = [] for i in range(26): for j in range(26): Mg = Mg + round(fenzupinlv[(j+i)%26]*ziranpinlv[j],3) fenzu_k.append(round(Mg,3)) Mg = 0 return fenzu_k.index(max(fenzu_k)) def shuchumingwen(xinfenzu,k): global huifupianduan huifupianduan= "" for hh in range(xinfenzu.__len__()): if ord(xinfenzu[hh])-k>=65: huifupianduan = huifupianduan + chr(ord(xinfenzu[hh])-k) if ord(xinfenzu[hh])-k<65: huifupianduan = huifupianduan + chr((ord(xinfenzu[hh])-65-k)%91) return huifupianduan def huifu(): global huifufenzu mingwen = "" for i in range(huifufenzu.__len__()): if i == 0: mingwen = mingwen + huifufenzu[i][:56] else: mingwen = mingwen +huifufenzu[i] for k in range(56): for j in range(k,mingwen.__len__(),56): stdout.write(mingwen[j]) stdout.write(huifufenzu[0][-1:]) keylength(miwen)
原文:http://www.cnblogs.com/wfzWebSecuity/p/6486600.html