没有合适的资源?快使用搜索试试~ 我知道了~
首页python实现Virginia无密钥解密
python实现Virginia无密钥解密
11 下载量 77 浏览量
更新于2023-05-11
4
收藏 54KB PDF 举报
主要为大家详细介绍了python实现Virginia无密钥解密,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/12863854/bg1.jpg)
python实现实现Virginia无密钥解密无密钥解密
主要为大家详细介绍了python实现Virginia无密钥解密,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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('','')
return text
def getKeyLen(cipherText): # 获取密钥长度
keylength = 1
maxCount = 0
for step in range(3,18): # 循环密钥长度
count = 0
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/default.jpg!1)
weixin_38635996
- 粉丝: 3
- 资源: 852
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
会员权益专享
最新资源
- VMP技术解析:Handle块优化与壳模板初始化
- C++ Primer 第四版更新:现代编程风格与标准库
- 计算机系统基础实验:缓冲区溢出攻击(Lab3)
- 中国结算网上业务平台:证券登记操作详解与常见问题
- FPGA驱动的五子棋博弈系统:加速与创新娱乐体验
- 多旋翼飞行器定点位置控制器设计实验
- 基于流量预测与潮汐效应的动态载频优化策略
- SQL练习:查询分析与高级操作
- 海底数据中心散热优化:从MATLAB到动态模拟
- 移动应用作业:MyDiaryBook - Google Material Design 日记APP
- Linux提权技术详解:从内核漏洞到Sudo配置错误
- 93分钟快速入门 LaTeX:从入门到实践
- 5G测试新挑战与罗德与施瓦茨解决方案
- EAS系统性能优化与故障诊断指南
- Java并发编程:JUC核心概念解析与应用
- 数据结构实验报告:基于不同存储结构的线性表和树实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)