python注册码实现(python实现Virginia无密钥解密)
类别:脚本大全 浏览量:2351
时间:2021-11-03 12:09:54 python注册码实现
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( '\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) |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/Aslani/article/details/53729804
您可能感兴趣
- python 简单算法(python实现爬山算法的思路详解)
- python中可以改变的数据类型(Python常见数据类型转换操作示例)
- python获取网络数据tcp(Python选择网卡发包及接收数据包)
- python直接查询mongodb(pymongo中聚合查询的使用方法)
- python如何把字符串转换成数字(python实现字符串加密成纯数字)
- python飞机大战游戏背景(python实现飞机大战游戏)
- python 从入门到实践笔记(python基础梳理一推荐)
- python改变字体颜色指令(使用Python自动化破解自定义字体混淆信息的方法实例)
- python程序运行步骤(详解python运行三种方式)
- python基础教程常用函数整理(Python基础之函数的定义与使用示例)
- python怎么在csv修改数据(python 编写输出到csv的操作)
- python爬虫开源代码(Python实现的文轩网爬虫完整示例)
- pythonnumpy定义一个2*2数组(对python numpy.array插入一行或一列的方法详解)
- python函数参数讲解(Python高级特性与几种函数的讲解)
- python比较两文件内容(Python判断两个文件是否相同与两个文本进行相同项筛选的方法)
- python pandas读取数据库表(Python3.5 Pandas模块之DataFrame用法实例分析)
- 郭麒麟(郭麒麟)
- 古人十句 戒骄 名言,醍醐灌顶,受益匪浅(古人十句戒骄名言)
- 《道德经》:功成不局,泰而不骄(道德经:功成不局)
- 每日一典 过江之鲫(每日一典过江之鲫)
- 红色代表什么(红色代表什么意义和象征)
- 菲律宾安全吗(菲律宾安全吗2023)
热门推荐
- mysql索引分几种(MySQL 覆盖索引的优点)
- laravel请求耗时(Laravel统计一段时间间隔的数据方法)
- 代码评审工具Phabricator
- html注册表单验证代码(JavaScript+html实现前端页面随机二维码验证)
- sql server还原数据库时提示数据库正在使用
- sql数据库有几种数据类型(SQL的常用数据类型列表详解)
- Visual studio中使用VSCommands插件在大括号尾部显示方法或过程名字
- 修改svn服务器地址
- thinkphp分页效果怎样(thinkphp5+layui实现的分页样式示例)
- secure file(解决出现secure_file_priv null的问题)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9