python实现Virginia无密钥解密


Posted in Python onMarch 20, 2019

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

加密

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

plainText = "whenigotthethemeithoughtofgooglesartificialintelligencealphagothisprogramoverthebestofhumanplayeriwanttoaskwhenscienceandtechnologycontinuetodevelopwehumanbeingswillbewhatpositionweshouldrealizethatthedevelopmentofscienceandtechnologyisirreversibleanditconstituteaprimaryprductiveforcebutmanmustkeeppacewiththetimestoenhancetheablitytocontrol" # 密文
alphabet = "abcdefghijklmnopqrstuvwxyz" # 26个字母
cipherText = "";
key = "helloworld" # 密钥
keyLen = len(key)
plainTextLen = len(plainText)
j = 0
for i in range(0,plainTextLen):
 j = i%keyLen
 keyNum = alphabet.index(key[j])
 plainNum = alphabet.index(plainText[i])
 plainTemp = alphabet[(keyNum*plainNum)%26] # 密钥对明文作用
 cipherText += plainTemp
print(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 plainText
def openfile(fileName): # 读文件
 file = open(fileName,'r')
 text = file.read()
 file.close();
 text = text.replace('\n','')
 return text

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

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    

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 li

def 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 textMatrix

if __name__ == '__main__':
 cipherText = openfile(r'') # 这里要根据文档目录的不同而改变
 plainText= virginiaCrack(cipherText)
 print('the plainText:\n',plainText)

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

Python 相关文章推荐
详细解析Python当中的数据类型和变量
Apr 25 Python
理解Python中函数的参数
Apr 27 Python
Python实现以时间换空间的缓存替换算法
Feb 19 Python
Python基于回溯法子集树模板实现8皇后问题
Sep 01 Python
python+ffmpeg视频并发直播压力测试
Mar 06 Python
对pandas中apply函数的用法详解
Apr 10 Python
django项目简单调取百度翻译接口的方法
Aug 06 Python
Django实现WebSSH操作物理机或虚拟机的方法
Nov 06 Python
pytorch中使用cuda扩展的实现示例
Feb 12 Python
基于django 的orm中非主键自增的实现方式
May 18 Python
在django中实现choices字段获取对应字段值
Jul 12 Python
python实现银行账户系统
Feb 22 Python
python实现维吉尼亚加密法
Mar 20 #Python
Python multiprocess pool模块报错pickling error问题解决方法分析
Mar 20 #Python
python实现对输入的密文加密
Mar 20 #Python
python实现字符串加密成纯数字
Mar 19 #Python
python实现简单加密解密机制
Mar 19 #Python
python使用adbapi实现MySQL数据库的异步存储
Mar 19 #Python
python异步存储数据详解
Mar 19 #Python
You might like
PHP 5.0对象模型深度探索之属性和方法
2008/03/27 PHP
PHP之短标签开启设置
2013/06/17 PHP
关于crontab的使用详解
2013/06/24 PHP
CodeIgniter配置之database.php用法实例分析
2016/01/20 PHP
php处理带有中文URL的方法
2016/07/11 PHP
php和redis实现秒杀活动的流程
2019/07/17 PHP
关于window.pageYOffset和document.documentElement.scrollTop
2011/04/05 Javascript
js当一个变量为函数时 应该注意的一点细节小结
2011/12/29 Javascript
把input初始值不写value的具体实现方法
2013/07/04 Javascript
Javascript玩转继承(二)
2014/05/08 Javascript
分享28款免费实用的 JQuery 图片和内容滑块插件
2014/12/15 Javascript
javascript数据类型示例分享
2015/01/19 Javascript
基于nodejs+express(4.x+)实现文件上传功能
2015/11/23 NodeJs
详解Vue使用 vue-cli 搭建项目
2017/04/20 Javascript
详解关于vue-area-linkage走过的坑
2018/06/27 Javascript
vue 地图可视化 maptalks 篇实例代码详解
2019/05/21 Javascript
layui的数据表格+springmvc实现搜索功能的例子
2019/09/28 Javascript
[03:40]DOTA2亚洲邀请赛小组赛第二日 赛事回顾
2015/01/31 DOTA
Python代码调试的几种方法总结
2015/04/15 Python
Python抓取淘宝下拉框关键词的方法
2015/07/08 Python
利用python模拟sql语句对员工表格进行增删改查
2017/07/05 Python
Python 音频生成器的实现示例
2019/12/24 Python
浅谈python之自动化运维(Paramiko)
2020/01/31 Python
SpringBoot首页设置解析(推荐)
2021/02/11 Python
美国羽绒床上用品第一品牌:Pacific Coast
2018/08/25 全球购物
澳大利亚购买最佳炊具品牌网站:Cookware Brands
2019/02/16 全球购物
Java中compareTo和compare的区别
2016/04/12 面试题
override和overload的区别
2016/03/09 面试题
函授毕业生自我鉴定
2013/11/06 职场文书
黄河的主人教学反思
2014/02/07 职场文书
搞笑创意广告语
2014/03/17 职场文书
司仪主持词两篇
2014/03/22 职场文书
教师读书活动总结
2014/05/07 职场文书
创新社会管理心得体会
2014/09/12 职场文书
2015年银行柜员工作总结报告
2015/04/01 职场文书
js实现模拟购物商城案例
2021/05/18 Javascript