# -*-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)

python实现维吉尼亚解密的更多相关文章

  1. Vigenère Cipher 维吉尼亚加解密算法

    维吉尼亚的加解密有两种方法. 第一种是查表:第一行为明文,第一列为密钥,剩余的为对应的密文 第二种方法是转化计算法:逐个将字符转化为从零开始的数字,对数字进行加密/解密后,再转化为字符. 本文要用c+ ...

  2. [加密]C#实现维吉尼亚加密与解密(解密前提为已知密匙)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. 随机练习:C#实现维吉尼亚加密与解密(解密前提为已知密匙)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. 维吉尼亚密码-攻防世界(shanghai)

    维吉尼亚密码 维吉尼亚密码是使用一系列 凯撒密码 组成密码字母表的加密算法,属于多表密码的一种简单形式. 加密原理 维吉尼亚密码的前身,是我们熟悉的凯撒密码. 凯撒密码的加密方式是依靠一张字母表中的每 ...

  5. [CTF]维吉尼亚密码(维基利亚密码)

    [CTF]维吉尼亚密码(维基利亚密码) ----------------------百度百科 https://baike.baidu.com/item/维吉尼亚密码/4905472?fr=aladdi ...

  6. python 维吉尼亚

    加密key='COMPUTER' plaintext='BLOCKCIPHERDESIGNPRINCIPLE' ascii='abcdefghijklmnopqrstuvwxyz'.upper() k ...

  7. 维吉尼亚密码java代码实现根据密钥长度计算IC值过程

    package cn.longxuzi; import java.util.Scanner; import org.junit.Test; public class ICUtils { /** * @ ...

  8. 维吉尼亚密码java完整版

    package cn.longxuzi; import org.junit.Test; public class Chi_SquareUtils { private static final ICUt ...

  9. python实现维吉利亚密码加密(Vigenère cipher)

    最近有个朋友问我关于维吉利亚密码如何用python实现加密,研究之后发现这是个挺好玩的东西,遂决定写篇博文记录一下. 一.何谓维吉利亚密码 第一列是密钥字母列,第一行是明文字母行.不难看出维吉利亚密码 ...

随机推荐

  1. 【noip模拟】tree

    000 ms        Memory Limit: 128 MB [吐槽] 点分治点分治点分治 嗯..场上思考树状数组的时候好像傻掉了..反正就是挂了就是了.. [题解] 首先如果没有环的话就是一 ...

  2. spring中aop的注解实现方式简单实例

    上篇中我们讲到spring的xml实现,这里我们讲讲使用注解如何实现aop呢.前面已经讲过aop的简单理解了,这里就不在赘述了. 注解方式实现aop我们主要分为如下几个步骤(自己整理的,有更好的方法的 ...

  3. C#多线程编程序--聊聊线程

    这篇文章主要给您讲解几个基本问题,什么是线程?为什么要用线程?线程万能?这几个问题.我这篇博客是在该系列另外几篇博客之后写的,之所以不往下继续写,而是回到最初的问题,是因为我感觉到我没有很好的讲解开头 ...

  4. 大数据(2):基于sogou.500w.utf8数据hive的实践

    一.环境的搭建 1.安装配置mysql rpm –ivh MySQL-server-5.6.14.rpm rpm –ivh MySQL-client-5.6.14.rpm 启动mysql 创建hive ...

  5. centos7上修改主机名

    centos7上修改主机名 2017-10-09   13:45:17 个人原创,转载请注明,否则追究法律责任 1,临时修改: 和centos5,centos6 一样,重启失效 2,永久修改: 命令: ...

  6. java函数回调

    Class A实现接口CallBack callback--背景1 class A中包含一个class B的引用b --背景2 class B有一个参数为callback的方法f(CallBack c ...

  7. 获取DOM节点的几种方式

    DOM 是一个树形结构,操作一个DOM节点,实际上就是这几个操作:更新.删除.添加.遍历 在操作DOM节点之前,需要通过各种方式先拿到这个DOM节点,常用的方法有: 一.通过元素类型的方法来操作: d ...

  8. Spring Cloud简介以及版本选择

    什么是SpringCloud 官方的说法就是spring Cloud 给开发者提供一套按照一定套路快速开发 分布式系统 的工具. 具体点就是Spring boot实现的微服务架构开发工具.它为微服务架 ...

  9. js获取input file文件二进制码

    <html> <body> <img id="image"src=""/> <br/> <input ty ...

  10. 智齿客服网页端接入文档V2.3

    产品介绍 智齿客服网页端接入提供以下两种部署方式. 一.网页组件(推荐) 通过智齿客服网站咨询组件,企业的用户可快捷联系到企业客服获取帮助.智齿客服网页组件提供强大的用户行为采集能力和系统对接能力,支 ...