# -*-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实现维吉尼亚解密的更多相关文章
-
Vigen&#232;re Cipher 维吉尼亚加解密算法
维吉尼亚的加解密有两种方法. 第一种是查表:第一行为明文,第一列为密钥,剩余的为对应的密文 第二种方法是转化计算法:逐个将字符转化为从零开始的数字,对数字进行加密/解密后,再转化为字符. 本文要用c+ ...
-
[加密]C#实现维吉尼亚加密与解密(解密前提为已知密匙)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
-
随机练习:C#实现维吉尼亚加密与解密(解密前提为已知密匙)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
-
维吉尼亚密码-攻防世界(shanghai)
维吉尼亚密码 维吉尼亚密码是使用一系列 凯撒密码 组成密码字母表的加密算法,属于多表密码的一种简单形式. 加密原理 维吉尼亚密码的前身,是我们熟悉的凯撒密码. 凯撒密码的加密方式是依靠一张字母表中的每 ...
-
[CTF]维吉尼亚密码(维基利亚密码)
[CTF]维吉尼亚密码(维基利亚密码) ----------------------百度百科 https://baike.baidu.com/item/维吉尼亚密码/4905472?fr=aladdi ...
-
python 维吉尼亚
加密key='COMPUTER' plaintext='BLOCKCIPHERDESIGNPRINCIPLE' ascii='abcdefghijklmnopqrstuvwxyz'.upper() k ...
-
维吉尼亚密码java代码实现根据密钥长度计算IC值过程
package cn.longxuzi; import java.util.Scanner; import org.junit.Test; public class ICUtils { /** * @ ...
-
维吉尼亚密码java完整版
package cn.longxuzi; import org.junit.Test; public class Chi_SquareUtils { private static final ICUt ...
-
python实现维吉利亚密码加密(Vigen&#232;re cipher)
最近有个朋友问我关于维吉利亚密码如何用python实现加密,研究之后发现这是个挺好玩的东西,遂决定写篇博文记录一下. 一.何谓维吉利亚密码 第一列是密钥字母列,第一行是明文字母行.不难看出维吉利亚密码 ...
随机推荐
-
System.Data.SqlTypes.SqlNullValueException: 数据为空。不能对空值调用此方法或
有可能读出的数据为NULL,可以这样改: 方法一:while (reader.Read()){ for (int i = 0; i < 7; i++) { if (reader.IsDBNull ...
-
python学习小结7:变量类型
变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据类型,这些变量可以存储整 ...
-
Codeforces Round #343 (Div. 2) C. Famil Door and Brackets
题目链接: http://codeforces.com/contest/629/problem/C 题意: 长度为n的括号,已经知道的部分的长度为m,现在其前面和后面补充‘(',或')',使得其长度为 ...
-
Android手机修改Hosts的方法
Android手机是和Google帐号紧密联系的,由于中国的操蛋情况,很多时候Google帐号无法登录,导致Android市场无法使用. 在电脑上我们通过修改Hosts方法可以解决Google帐号的登 ...
-
HTTP缓存机制[译文]
本文翻译自: https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching ,主要用于个人记录和共享,若有疏漏错误,请不吝指正,谢谢! 通过重用已 ...
-
SQL Sever MYSQL 视图实现的 2 种方式
前期准备: 1.create table person # 假设这张表用来收录所以有地球人的基本信息. (ID bigint , Name varchar(16), Country varchar( ...
-
jdk配置java_home的两种方式
在开发java项目的时候,要先安装jdk,安装完jdk之后我们要配置环境变量.今天说一下java home环境变量. 配置环境变量有两种方式,一种就是在计算机的环境变量里面配置.这个是很简单的.如果你 ...
-
小白的首个maven web项目Step1软件安装三(8.0.15mysql及workbench安装)
直接先开始下 MySQL 和 Workbench(mysql的可视化工具) ,注意下得是镜像版 .msi 后缀的 (mysql是纯控制面板的呈现方式,想要界面化操作可以装可视化工具,这里我装的是wor ...
-
JAXB注解 @XmlRootElement 及XML文件解析详解
@Retention(value=RUNTIME) @Target(value=TYPE) public @interface XmlRootElement @Inherited @Retention ...
-
Spring MVC 学习笔记10 —— 实现简单的用户管理(4.3)用户登录显示全局异常信息
</pre>Spring MVC 学习笔记10 -- 实现简单的用户管理(4.3)用户登录--显示全局异常信息<p></p><p></p>& ...