維吉尼亞之加解密及破解

2021-08-17 16:08:01 字數 4257 閱讀 5224

【題目】

實現維吉尼亞密碼演算法,具體要求:

a. 實現維吉尼亞密碼加密過程,由使用者輸入金鑰,可以對任意輸入的明文進行加密;

b. 根據使用者輸入的金鑰,對密文進行解密;

c. 實現維吉尼亞密碼的唯密文攻擊破解(基於重合互指數方法)

【實現**】

# -*- coding: utf-8 -*-

"""created on wed dec 13 08:17:01 2017

@author: hp

"""#!/usr/bin/env python

"維吉尼亞"

from string import ascii_lowercase as lowercase

#加密def vigenereencrypto (p , key) :

p = get_trim_text(p)

ptlen = len(p)

keylen = len(key)

quotient = ptlen // keylen #商

remainder = ptlen % keylen #餘

out = ""

for i in range (0 , quotient) :

for j in range (0 , keylen) :

c = int((ord(p[i*keylen+j]) - ord('a') + ord(key[j]) - ord('a')) % 26 + ord('a'))

#global output

out += chr (c)

for i in range (0 , remainder) :

c = int((ord(p[quotient*keylen+i]) - ord('a') + ord(key[i]) - ord('a')) % 26 + ord('a'))

#global output

out += chr (c)

return out

#解密def vigeneredecrypto (output , key) :

ptlen = len (output)

keylen = len (key)

quotient = ptlen // keylen

remainder = ptlen % keylen

inp = ""

for i in range (0 , quotient) :

for j in range (0 , keylen) :

c = int((ord(output[i*keylen+j]) - ord('a') - (ord(key[j]) - ord('a'))) % 26 + ord('a'))

#global input

inp += chr (c)

for i in range (0 , remainder) :

c = int((ord(output[quotient*keylen + i]) - ord('a') - (ord(key[i]) - ord('a'))) % 26 + ord('a'))

#global input

inp += chr (c)

return inp

def get_trim_text(text):

text = text.lower()

trim_text = ''

for l in text:

if lowercase.find(l) >= 0:

trim_text += l

return trim_text

#計算重合指數

def get_coincidence_index(text):

text = get_trim_text(text)

length = len(text)

letter_stats =

for l in lowercase:

lt = {}

count = text.count(l)

lt[l] = count

index = 0

for d in letter_stats:

v = list(d.values())[0]

index += (float(v)/length) ** 2

return index

#計算和0.067的差距大小

def get_var(data, mean=0.067):

if not data:

return 0

var_sum = 0

for d in data:

var_sum += (d - mean) ** 2

return float(var_sum) / len(data)

#求秘鑰長度

def get_key_length(text):

# assume text length less than 26

text = get_trim_text(text)

group =

for n in range(1, len(text)+1):

group_str = ['' for i in range(n)]

for i in range(len(text)):

l = text[i]

for j in range(n):

if i % n == j:

group_str[j] += l

var_list =

length = 1

for tex in group:

data =

for t in tex:

index = get_coincidence_index(t)

length += 1

var_list = sorted(var_list, key=lambda x: x[1])

print(var_list)

return [v[0] for v in var_list[:int(n/2)+1]] #var_list[0][0]

# 統計字母頻度

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

return li

# 根據金鑰長度將密文分組

def texttolist(text,length):

text = get_trim_text(text)

textmatrix =

row =

index = 0

for ch in text:

index += 1

if index % length ==0:

row =

return textmatrix

# 獲取金鑰

def getkey(text,length):

text = get_trim_text(text)

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 if len(row) > i] #獲取每組密文

li = countlist(w)

powli = #算乘積

for j in range(26):

sum = 0.0

for k in range(26):

sum += alpharate[k]*li[k]

li = li[1:]+li[:1]#迴圈移位

abs = 100

ch = ''

for j in range(len(powli)):

if abs(powli[j] -0.065546)

Vigen re Cipher 維吉尼亞加解密演算法

維吉尼亞的加解密有兩種方法。第一種是查表 第一行為明文,第一列為金鑰,剩餘的為對應的密文 第二種方法是轉化計算法 逐個將字元轉化為從零開始的數字,對數字進行加密 解密後,再轉化為字元。核心 如下 cipher.h 額外新增的檔案,用來放置相關演算法,此檔案獨立於mfc外,可直接移植到支援cstrin...

用python編寫維吉尼亞密碼加解密

簡單介紹 維吉尼亞密碼演算法使用乙個金鑰和乙個表來實現加密,根據明文和金鑰的對應關係進行查表來決定加密結果。假設替換表如圖所示,最上面一行表示明文,最左邊一列表示金鑰,那麼二維 中與明文本母和金鑰字母對應的字母就是加密結果。例如單詞python使用abcdef做金鑰的加密結果就是pzvkss 可在此...

維吉尼亞密碼的破解

密碼學作業,做的時候發現網上和書上講的都不是很詳細,導致花了很多時間,所以這裡記下來一點希望能有幫助。維吉尼亞密碼破解步驟 確定金鑰長度 在密文中找到重複出現三次以上 這樣做是為了保證精確度和減少計算量 的字母組合。這裡,字母組合的意思是兩個及以上的重複字母組比如tn pm。然後列表寫出每乙個重複字...