Virginia(维吉尼亚)无密钥解密

更新时间:2023-07-04 18:25:27 阅读: 评论:0

Virginia(维吉尼亚)⽆密钥解密
iptv是什么Virginia⽆密钥解密
加密
virginia加密是⼀种多表替换加密⽅法,通过这种⽅法,可以有效的解决单表替换中⽆法应对的字母频度攻击。这种加密⽅法最重要的就是
选取合适的密钥,⼀旦密钥被公开,保密性也就⽆从谈起。结合virginia加密原理,给出使⽤python实现的代码
plainText = "whenigotthethemeithoughtofgooglesartificialintelligencealphagothisprogramoverthebestofhumanplayeriwanttoaskwhenscienceandtechnologyco 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 = 0affidavit
for i in range(step,len(cipherText)-step):
if cipherText[i] == cipherText[i+step]:
count += 1
if count>maxCount:  # 每次保存最⼤次数的密钥长度
maxCount = count
keylength = step
4hand学音乐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    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):
pi是什么意思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 = ''
primarykeyindex = 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 = ad()
file.clo();insult
text = place('\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    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
clothes怎么读if index % length ==0:
编制资产负债表
textMatrix.append(row)
row = []
return textMatrix
if __name__ == '__main__':
cipherText = openfile(r'') # 这⾥要根据⽂档⽬录的不同⽽改变
plainText= virginiaCrack(cipherText)
plainText= virginiaCrack(cipherText)    print('the plainText:\n',plainText)

本文发布于:2023-07-04 18:25:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/1078379.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:密钥   长度   获取   加密   给出
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图