侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

python实现Virginia无密钥解密

Python  /  管理员 发布于 7年前   318

本文实例为大家分享了Virginia无密钥解密的具体代码,供大家参考,具体内容如下

加密

virginia加密是一种多表替换加密方法,通过这种方法,可以有效的解决单表替换中无法应对的字母频度攻击。这种加密方法最重要的就是选取合适的密钥,一旦密钥被公开,保密性也就无从谈起。结合virginia加密原理,给出使用python实现的代码

plainText = "whenigotthethemeithoughtofgooglesartificialintelligencealphagothisprogramoverthebestofhumanplayeriwanttoaskwhenscienceandtechnologycontinuetodevelopwehumanbeingswillbewhatpositionweshouldrealizethatthedevelopmentofscienceandtechnologyisirreversibleanditconstituteaprimaryprductiveforcebutmanmustkeeppacewiththetimestoenhancetheablitytocontrol" # 密文alphabet = "abcdefghijklmnopqrstuvwxyz" # 26个字母cipherText = "";key = "helloworld" # 密钥keyLen = len(key)plainTextLen = len(plainText)j = 0for i in range(0,plainTextLen): j = i%keyLen keyNum = alphabet.index(key[j]) plainNum = alphabet.index(plainText[i]) plainTemp = alphabet[(keyNum*plainNum)%26] # 密钥对明文作用 cipherText += plainTempprint(cipherText)

解密

重点谈谈解密部分。这里的解密主要分为获取密钥长度,根据密钥长度获取密钥,根据密钥获取明文三个部分。

获取密钥长度

使用暴力破解密钥长度的方法,循环遍历可能的密钥长度。每次循环中,记录在这种密钥长度下重复相隔密钥长度密文的次数,从理论上来讲,次数最多的那个密钥长度,最有可能正确。当密文长度足够长时,正确的可能性很高。给出获取密钥长度的python函数代码:

def getKeyLen(cipherText): # 获取密钥长度 keylength = 1 maxCount = 0 for step in range(3,18): # 循环密钥长度  count = 0  for i in range(step,len(cipherText)-step):   if cipherText[i] == cipherText[i+step]:      count += 1  if count>maxCount: # 每次保存最大次数的密钥长度   maxCount = count    keylength = step return keylength # 返回密钥长度

获取密钥

当已经获取密钥长度之后,我们可以通过分组将相同密钥作用下的密文进行分组,在每一组中,都是一个简单的单表替换加密。在这种情况下,我们通过重合指数法破解密钥,给出获取密钥部分的python函数代码:

def getKey(text,length): # 获取密钥 key = [] # 定义空白列表用来存密钥 alphaRate =[0.08167,0.01492,0.02782,0.04253,0.12705,0.02228,0.02015,0.06094,0.06996,0.00153,0.00772,0.04025,0.02406,0.06749,0.07507,0.01929,0.0009,0.05987,0.06327,0.09056,0.02758,0.00978,0.02360,0.0015,0.01974,0.00074] matrix =textToList(text,length) for i in range(length):  w = [row[i] for row in matrix] #获取每组密文  li = countList(w)   powLi = [] #算乘积  for j in range(26):   Sum = 0.0   for k in range(26):    Sum += alphaRate[k]*li[k]   powLi.append(Sum)   li = li[1:]+li[:1]#循环移位  Abs = 100  ch = ''  for j in range(len(powLi)):    if abs(powLi[j] -0.065546)<Abs: # 找出最接近英文字母重合指数的项     Abs = abs(powLi[j] -0.065546) # 保存最接近的距离,作为下次比较的基准     ch = chr(j+97)  key.append(ch) return key 

 破解明文

在已知密钥和明文的基础上,我们很容易就可以得到明文,给出python代码:

def virginiaCrack(cipherText): # 解密函数 length = getKeyLen(cipherText) #得到密钥长度 key = getKey(cipherText,length) #找到密钥 keyStr = '' for k in key:  keyStr+=k print('the Key is:',keyStr) plainText = '' index = 0 for ch in cipherText:  c = chr((ord(ch)-ord(key[index%length]))%26+97)  plainText += c  index+=1 return plainText # 返回明文

代码

这是解密部分的全部代码,注意需要自己添加密文文件的位置

def virginiaCrack(cipherText): # 解密函数 length = getKeyLen(cipherText) #得到密钥长度 key = getKey(cipherText,length) #找到密钥 keyStr = '' for k in key:  keyStr+=k print('the key:',keyStr) plainText = '' index = 0 for ch in cipherText:  c = chr((ord(ch)-ord(key[index%length]))%26+97)  plainText += c  index+=1 return plainTextdef openfile(fileName): # 读文件 file = open(fileName,'r') text = file.read() file.close(); text = text.replace('\n','') return textdef getKeyLen(cipherText): # 获取密钥长度 keylength = 1 maxCount = 0 for step in range(3,18): # 循环密钥长度  count = 0  for i in range(step,len(cipherText)-step):   if cipherText[i] == cipherText[i+step]:     count += 1  if count>maxCount:   maxCount = count   keylength = step return keylengthdef getKey(text,length): # 获取密钥 key = [] # 定义空白列表用来存密钥 alphaRate =[0.08167,0.01492,0.02782,0.04253,0.12705,0.02228,0.02015,0.06094,0.06996,0.00153,0.00772,0.04025,0.02406,0.06749,0.07507,0.01929,0.0009,0.05987,0.06327,0.09056,0.02758,0.00978,0.02360,0.0015,0.01974,0.00074] matrix =textToList(text,length) for i in range(length):  w = [row[i] for row in matrix] #获取每组密文  li = countList(w)   powLi = [] #算乘积  for j in range(26):   Sum = 0.0   for k in range(26):    Sum += alphaRate[k]*li[k]   powLi.append(Sum)   li = li[1:]+li[:1]#循环移位  Abs = 100  ch = ''  for j in range(len(powLi)):    if abs(powLi[j] -0.065546)<Abs: # 找出最接近英文字母重合指数的项     Abs = abs(powLi[j] -0.065546) # 保存最接近的距离,作为下次比较的基准     ch = chr(j+97)  key.append(ch) return key    def countList(lis): # 统计字母频度 li = [] alphabet = [chr(i) for i in range(97,123)] for c in alphabet:  count = 0  for ch in lis:   if ch == c:    count+=1  li.append(count/len(lis)) return lidef textToList(text,length): # 根据密钥长度将密文分组 textMatrix = [] row = [] index = 0 for ch in text:  row.append(ch)  index += 1  if index % length ==0:   textMatrix.append(row)   row = [] return textMatrixif __name__ == '__main__': cipherText = openfile(r'') # 这里要根据文档目录的不同而改变 plainText= virginiaCrack(cipherText) print('the plainText:\n',plainText)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    python实现栅栏加解密 支持密钥加密
    下一条:
    python实现维吉尼亚加密法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客