欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)Virginia無密鑰解密

 更新時(shí)間:2019年03月20日 09:25:27   作者:Aslani  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)Virginia無密鑰解密,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)文章

最新評(píng)論