python实现维吉尼亚解密

# -*-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)
时间: 2024-10-27 13:01:22

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

维吉尼亚密码的Python实现

代换密码体制的一般定义为M=C=K=Z26,其中M为明文空间.C为密文空间.K为密钥空间.Z26为26个整数(对应26个英文字母)组成的空间:要求26个字母与模26的剩余类集合{0,1,2,-,25}建立一一对应的关系. 1.移位密码 移位密码的加密实现上就是将26个英文字母向后循环移动k位,其加解密可分别表示为: c=Ek(m)=m+k(mod 26) m=Dk(c)=c-k(mod 26) 其中,m.c.k是满足0≤m,c,k≤25的整数. 2.维吉尼亚密码 Vigenenre密码是最著名的

python编写的维吉尼亚密码加解密程序

维吉尼亚密码表 ============================================= #维吉尼亚密码  加密 key='helloworld' plaintext='whereisthekey' #key='relations' #plaintext='tobeornottobeth' ascii='abcdefghijklmnopqrstuvwxyz' keylen=len(key) ptlen=len(plaintext) ciphertext = '' i = 0 w

Vigen&#232;re Cipher 维吉尼亚加解密算法

维吉尼亚的加解密有两种方法. 第一种是查表:第一行为明文,第一列为密钥,剩余的为对应的密文 第二种方法是转化计算法:逐个将字符转化为从零开始的数字,对数字进行加密/解密后,再转化为字符. 本文要用c++实现第二种方法,并且为了操作方便,使用了MFC框架(附工程下载) 核心代码如下: //cipher.h 额外添加的文件,用来放置相关算法,此文件独立于MFC外,可直接移植到支持CString的项目中 int* CStringToInt(CString str){ //将CString转换为zero

py3实现维吉尼亚加解密

实 验 目 的 1.  熟悉和掌握替代加密算法的原理及其一般过程: 2.掌握对称加密算法的基本方法:维吉尼亚密码 3.掌握使用一定的编码开发工具(对具体的开发平台和工具不作要求). 实 验 环 境   Python3+pycharm 实 验 步 骤 1.   维吉尼亚原理分析 Vigenenre 密码使用一个词组作为密钥,密钥中每一个字母用来确定一个代换表, 每一个密钥字母被用来加密一个明文字母,第一个密钥字母加密第一个明文字母, 第二个密钥字母加密第二个明文字母,等所有密钥字母使用完后,密钥再

维吉尼亚密码加密、解密算法(破解还不会);

#include <iostream> #include<cstdio> using namespace std; void init_pass_table(char (&code_table)[27][27]){//制密码对照表;(不能当做一维数组来看会有别的问题) code_table[0][0]='0'; for(int i=0;i<26;i++){ code_table[0][1+i]='a'+i; } for(int i=0;i<26;i++){ co

Python运维编程

Python运维编程 作者:Danbo  2015-10-11 什么是Python,为什么要使用Python? 这个大家自行谷歌,不过看看知乎你就知道Python有多么强大:http://www.zhihu.com/question/25038841  #大蟒蛇是人形自走跑有木有啊! 首先你要知道Python很强大.Python是一个胶水语言(Glue Language).与Perl最大的不同是Python开发的哲学是:用一种方法,最好是只有一种方法来做一件事,而Perl是:总是有多种方法来做同

python运维开发笔记4

1.函数如何被调用,通过return返回值来调用 2.生成器和return区别 yield 生成器返回对象,可以迭代 可以执行 glob模块 类似shell中的正则匹配 shlex模块  Popen 将命令参数直接分词 cmd = "ps ax -o pid,ppid,cmd" shlex.split(cmd) ['ps','ax','-o','pid,ppid,cmd'] ['mysql','-u','root','-p123','-e','show processlist'] p

python运维开发笔记5

diff -Nur Diff和patch是Linux标配的工具.在Windows上,也有移植的版本可以使用. 使用中注意: 1.保证文件名完全相同,避免只有大小写不同的文件存在. 2.Diff只能比较文本文件,二进制文件只能判断是否相同,不能记录差异.Diff根据文件的前几个字节判断文件是不是文本文件. 使用方法: 可以对单个文件或者整个目录树进行处理. 以最实用的方式举例: diff –Nur dir_old dir_new > dir.patch patch –p0 < dir.patch

Python运维开发基础

Python基础知识分为以下几块 1.Python概述 2.基础语法 3.数据结构 4.Python进阶 5.实训案例 一.Python概述 1.Python简介 2.Hello World 3.搭建开发环境 4.习题 Python简介 尽管我是学计算机出身的,但是我对Python的认识是在毕业后.Python是我喜欢的语言,简洁,优美,容易使用.重要的一点是他是开源的项目. 官方网站 https://www.python.org 学习网站 http://www.okpython.com 对于简