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获取服务器信息的最简单实现方法
Mar 05 Python
Python中的高级数据结构详解
Mar 27 Python
Python中Django发送带图片和附件的邮件
Mar 31 Python
requests和lxml实现爬虫的方法
Jun 11 Python
python中 logging的使用详解
Oct 25 Python
Python实现生成随机数据插入mysql数据库的方法
Dec 25 Python
用python打印菱形的实操方法和代码
Jun 25 Python
python 使用pdfminer3k 读取PDF文档的例子
Aug 27 Python
python中dict()的高级用法实现
Nov 13 Python
Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)
Feb 05 Python
用Python生成HTML表格的方法示例
Mar 06 Python
浅谈keras中的Merge层(实现层的相加、相减、相乘实例)
May 23 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
人大复印资料处理程序_输入篇
2006/10/09 PHP
使用TinyButStrong模板引擎来做WEB开发
2007/03/16 PHP
php实现图片添加水印功能
2014/02/13 PHP
利用PHP脚本在Linux下用md5函数加密字符串的方法
2015/06/29 PHP
php微信公众平台开发(四)回复功能开发
2016/12/06 PHP
php使用PDO事务配合表格读取大量数据插入操作实现方法
2017/02/16 PHP
laravel实现简单用户权限的示例代码
2019/05/28 PHP
PHP SESSION跨页面传递失败解决方案
2020/12/11 PHP
关于javascript function对象那些迷惑分析
2011/10/24 Javascript
(跨浏览器基础事件/浏览器检测/判断浏览器)经验代码分享
2013/01/24 Javascript
Ubuntu中搭建Nodejs开发环境过程分享
2014/06/01 NodeJs
js的flv视频播放器插件使用方法
2015/06/23 Javascript
jQuery实现图片左右滚动特效
2020/04/20 Javascript
js对象浅拷贝和深拷贝详解
2016/09/05 Javascript
jQuery内存泄露解决办法
2016/12/13 Javascript
vue-cli如何快速构建vue项目
2017/04/26 Javascript
Angular.js实现动态加载组件详解
2017/05/28 Javascript
Vue完整项目构建(进阶篇)
2018/02/10 Javascript
Bootstrap Fileinput 4.4.7文件上传实例详解
2018/07/25 Javascript
axios封装,使用拦截器统一处理接口,超详细的教程(推荐)
2019/05/02 Javascript
JavaScript实现简单的图片切换功能(实例代码)
2020/04/10 Javascript
理解JavaScript中的对象
2020/08/25 Javascript
Python使用函数默认值实现函数静态变量的方法
2014/08/18 Python
使用Python的Flask框架实现视频的流媒体传输
2015/03/31 Python
详解Python函数作用域的LEGB顺序
2016/05/14 Python
Windows系统下多版本pip的共存问题详解
2017/10/10 Python
Django框架模板介绍
2019/01/15 Python
python flask web服务实现更换默认端口和IP的方法
2019/07/26 Python
Flask项目中实现短信验证码和邮箱验证码功能
2019/12/05 Python
Python文件操作基础流程解析
2020/03/19 Python
Python3读取和写入excel表格数据的示例代码
2020/06/09 Python
香港万宁官方海外旗舰店:香港健与美连锁店
2018/09/27 全球购物
2014年村计划生育工作总结
2014/11/14 职场文书
研究生给导师的自荐信
2015/03/06 职场文书
2019年聘任书的写作格式及范文!
2019/07/03 职场文书
mysql序号rownum行号实现方式
2022/12/24 MySQL