资源简介
用python编程实现维吉尼亚的唯密文破解,对密文分析,用拟重合指数。
代码片段和文件信息
#Author:李志强
# _*_ coding:utf-8 _*_
def main():
fo=open(“cipher.txt““r“)
s=fo.read()
s=str(s)
fo.close()
ic=0
max_num=len(s)//26
# while ic<0.06:
#def fenzu():
#分组
aves=[0]*max_num
for i in range(1max_num):
count = 0
zicuan=[]
for t in range (i):
fz=s[t:len(s):i]
zicuan+=[fz]
count+=1
#print(count‘韩庚韩庚韩庚‘zicuan)
for js in range (i):
zicuan[js]=zicuan[js].upper()
ics=[0]*count
#统计每个分组的IC值
for r in range(count):
ics[r]=tongjicisu(zicuan[r])
ave =sum(ics)/count
print(‘第{}次分片的IC值是{}‘.format(iave))
aves[i-1]=ave
#找到最可能的密钥分组
key_index=1
max = 1
for i in range(max_num):
max1=abs(aves[i]-0.065)
if max1 max=max1
key_index=i+1
print(‘key_length‘key_index)
key = [None]*key_index
#得到密钥长度后从新按密钥长度分片计算
zicuan2 = []
for t in range(key_index):
fz = s[t:len(s):key_index]
zicuan2 += [fz]
for i in range(key_index):
key[i]=decode(zicuan2[i])
print(key)
di = {}.fromkeys(key)
key=di.keys()
keys=““
for i in key:
keys+=i
print(keys“密钥“)
mc = VigenereDecrypto(skeys)
print(mc‘ecewew‘)
# 统计次数IC值
def tongjicisu(s):
tongjicisu = [0] * 26
zff = ““
ic=-0
for t in s:
if 65 <= ord(t) <= 90:
zff += t
for cisu in zff:
tongjicisu[ord(cisu) - 65] += 1
for i in range (len(tongjicisu)):
xic=tongjicisu[i]*(tongjicisu[i]-1)/len(zff)/(len(zff)-1)
ic+
评论
共有 条评论