python實(shí)現(xiàn)Virginia無密鑰解密
本文實(shí)例為大家分享了Virginia無密鑰解密的具體代碼,供大家參考,具體內(nèi)容如下
加密
virginia加密是一種多表替換加密方法,通過這種方法,可以有效的解決單表替換中無法應(yīng)對(duì)的字母頻度攻擊。這種加密方法最重要的就是選取合適的密鑰,一旦密鑰被公開,保密性也就無從談起。結(jié)合virginia加密原理,給出使用python實(shí)現(xiàn)的代碼
plainText = "whenigotthethemeithoughtofgooglesartificialintelligencealphagothisprogramoverthebestofhumanplayeriwanttoaskwhenscienceandtechnologycontinuetodevelopwehumanbeingswillbewhatpositionweshouldrealizethatthedevelopmentofscienceandtechnologyisirreversibleanditconstituteaprimaryprductiveforcebutmanmustkeeppacewiththetimestoenhancetheablitytocontrol" # 密文 alphabet = "abcdefghijklmnopqrstuvwxyz" # 26個(gè)字母 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] # 密鑰對(duì)明文作用 cipherText += plainTemp print(cipherText)
解密
重點(diǎn)談?wù)劷饷懿糠帧_@里的解密主要分為獲取密鑰長(zhǎng)度,根據(jù)密鑰長(zhǎng)度獲取密鑰,根據(jù)密鑰獲取明文三個(gè)部分。
獲取密鑰長(zhǎng)度
使用暴力破解密鑰長(zhǎng)度的方法,循環(huán)遍歷可能的密鑰長(zhǎng)度。每次循環(huán)中,記錄在這種密鑰長(zhǎng)度下重復(fù)相隔密鑰長(zhǎng)度密文的次數(shù),從理論上來講,次數(shù)最多的那個(gè)密鑰長(zhǎng)度,最有可能正確。當(dāng)密文長(zhǎng)度足夠長(zhǎng)時(shí),正確的可能性很高。給出獲取密鑰長(zhǎng)度的python函數(shù)代碼:
def getKeyLen(cipherText): # 獲取密鑰長(zhǎng)度 keylength = 1 maxCount = 0 for step in range(3,18): # 循環(huán)密鑰長(zhǎng)度 count = 0 for i in range(step,len(cipherText)-step): if cipherText[i] == cipherText[i+step]: count += 1 if count>maxCount: # 每次保存最大次數(shù)的密鑰長(zhǎng)度 maxCount = count keylength = step return keylength # 返回密鑰長(zhǎng)度
獲取密鑰
當(dāng)已經(jīng)獲取密鑰長(zhǎng)度之后,我們可以通過分組將相同密鑰作用下的密文進(jìn)行分組,在每一組中,都是一個(gè)簡(jiǎn)單的單表替換加密。在這種情況下,我們通過重合指數(shù)法破解密鑰,給出獲取密鑰部分的python函數(shù)代碼:
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]#循環(huán)移位 Abs = 100 ch = '' for j in range(len(powLi)): if abs(powLi[j] -0.065546)<Abs: # 找出最接近英文字母重合指數(shù)的項(xiàng) Abs = abs(powLi[j] -0.065546) # 保存最接近的距離,作為下次比較的基準(zhǔn) ch = chr(j+97) key.append(ch) return key
破解明文
在已知密鑰和明文的基礎(chǔ)上,我們很容易就可以得到明文,給出python代碼:
def virginiaCrack(cipherText): # 解密函數(shù) length = getKeyLen(cipherText) #得到密鑰長(zhǎng)度 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): # 解密函數(shù) length = getKeyLen(cipherText) #得到密鑰長(zhǎng)度 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): # 獲取密鑰長(zhǎng)度 keylength = 1 maxCount = 0 for step in range(3,18): # 循環(huán)密鑰長(zhǎng)度 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]#循環(huán)移位 Abs = 100 ch = '' for j in range(len(powLi)): if abs(powLi[j] -0.065546)<Abs: # 找出最接近英文字母重合指數(shù)的項(xiàng) Abs = abs(powLi[j] -0.065546) # 保存最接近的距離,作為下次比較的基準(zhǔn) ch = chr(j+97) key.append(ch) return key def countList(lis): # 統(tǒng)計(jì)字母頻度 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): # 根據(jù)密鑰長(zhǎng)度將密文分組 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'') # 這里要根據(jù)文檔目錄的不同而改變 plainText= virginiaCrack(cipherText) print('the plainText:\n',plainText)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談python數(shù)據(jù)結(jié)構(gòu)之動(dòng)態(tài)規(guī)劃
這篇文章主要介紹了淺談python數(shù)據(jù)結(jié)構(gòu)之動(dòng)態(tài)規(guī)劃,可能很多小伙伴會(huì)覺得這個(gè)詞很陌生,覺得這是一種很復(fù)雜的思想,學(xué)習(xí)起來很困難,其實(shí)并不是這樣,動(dòng)態(tài)規(guī)劃所講述的知識(shí)與動(dòng)態(tài)與規(guī)劃并無太大關(guān)聯(lián),需要的朋友可以參考下2023-07-07TF-IDF與余弦相似性的應(yīng)用(二) 找出相似文章
這篇文章主要為大家詳細(xì)介紹了TF-IDF與余弦相似性的應(yīng)用,找出相似文章,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Python基于whois模塊簡(jiǎn)單識(shí)別網(wǎng)站域名及所有者的方法
這篇文章主要介紹了Python基于whois模塊簡(jiǎn)單識(shí)別網(wǎng)站域名及所有者的方法,簡(jiǎn)單分析了Python whois模塊的安裝及使用相關(guān)操作技巧,需要的朋友可以參考下2018-04-04調(diào)試Django時(shí)打印SQL語句的日志代碼實(shí)例
這篇文章主要介紹了調(diào)試Django時(shí)打印SQL語句的日志代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09python基礎(chǔ)之編碼規(guī)范總結(jié)
今天帶大家來學(xué)習(xí)python基礎(chǔ)知識(shí),文中對(duì)python編碼規(guī)范作了詳細(xì)的介紹,對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05python實(shí)現(xiàn)的用于搜索文件并進(jìn)行內(nèi)容替換的類實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)的用于搜索文件并進(jìn)行內(nèi)容替換的類,涉及Python針對(duì)文件及字符串的相關(guān)操作技巧,需要的朋友可以參考下2015-06-06Python3基礎(chǔ)之條件與循環(huán)控制實(shí)例解析
這篇文章主要介紹了Python3基礎(chǔ)的條件與循環(huán)控制,需要的朋友可以參考下2014-08-08PyTorch搭建LSTM實(shí)現(xiàn)時(shí)間序列負(fù)荷預(yù)測(cè)
這篇文章主要為大家介紹了PyTorch搭建LSTM實(shí)現(xiàn)時(shí)間序列負(fù)荷預(yù)測(cè),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05