python实现维吉尼亚唯密文攻击破译并实现可视化界面输出明文和密钥,输入的密文可以是大小写字母
时间: 2024-03-20 07:40:36 浏览: 140
下面是一个Python实现维吉尼亚密码破译并可视化输出明文和密钥的示例代码:
```python
import tkinter as tk
from tkinter import messagebox
import string
class VigenereDecryptor:
def __init__(self, ciphertext):
self.ciphertext = ciphertext.upper()
self.alphabet = string.ascii_uppercase
self.freqs = [0] * 26
self.key_length = 0
self.key = ""
self.plaintext = ""
def kasiski_test(self):
repeats = {}
for i in range(len(self.ciphertext) - 2):
substr = self.ciphertext[i:i+3]
if substr in repeats:
repeats[substr].append(i)
else:
repeats[substr] = [i]
distances = {}
for substr, locations in repeats.items():
if len(locations) > 1:
distances[substr] = []
for i in range(len(locations) - 1):
distance = locations[i+1] - locations[i]
distances[substr].append(distance)
factors = []
for substr, dists in distances.items():
for dist in dists:
for factor in range(2, dist+1):
if dist % factor == 0:
factors.append(factor)
self.key_length = max(set(factors), key=factors.count)
def frequency_analysis(self, start):
freqs = [0] * 26
for i in range(start, len(self.ciphertext), self.key_length):
c = self.ciphertext[i]
if c in self.alphabet:
freqs[ord(c) - ord('A')] += 1
shift = freqs.index(max(freqs)) - ord('E')
self.key += chr(shift + ord('A'))
def decrypt(self):
for i in range(self.key_length):
self.key += self.frequency_analysis(i)
for i in range(len(self.ciphertext)):
c = self.ciphertext[i]
k = self.key[i % self.key_length]
shift = self.alphabet.index(k)
table = self.alphabet[shift:] + self.alphabet[:shift]
if c in self.alphabet:
p = table.index(c)
self.plaintext += self.alphabet[p]
else:
self.plaintext += c
class VigenereGUI:
def __init__(self):
self.root = tk.Tk()
self.root.title("Vigenere Decryptor")
self.root.geometry("500x300")
self.ciphertext_label = tk.Label(self.root, text="Ciphertext:")
self.ciphertext_label.pack()
self.ciphertext_entry = tk.Entry(self.root, width=50)
self.ciphertext_entry.pack()
self.decrypt_button = tk.Button(self.root, text="Decrypt", command=self.decrypt)
self.decrypt_button.pack()
self.key_label = tk.Label(self.root, text="Key:")
self.key_label.pack()
self.key_entry = tk.Entry(self.root, width=50)
self.key_entry.pack()
self.plaintext_label = tk.Label(self.root, text="Plaintext:")
self.plaintext_label.pack()
self.plaintext_entry = tk.Entry(self.root, width=50, state="readonly")
self.plaintext_entry.pack()
def decrypt(self):
ciphertext = self.ciphertext_entry.get()
if not ciphertext:
messagebox.showerror("Error", "Ciphertext cannot be empty")
return
decryptor = VigenereDecryptor(ciphertext)
decryptor.kasiski_test()
decryptor.decrypt()
self.key_entry.delete(0, tk.END)
self.key_entry.insert(0, decryptor.key)
self.plaintext_entry.configure(state="normal")
self.plaintext_entry.delete(0, tk.END)
self.plaintext_entry.insert(0, decryptor.plaintext)
self.plaintext_entry.configure(state="readonly")
def run(self):
self.root.mainloop()
gui = VigenereGUI()
gui.run()
```
这个代码包括了一个基本的GUI界面,可以输入密文并解密。解密过程中,会自动计算密钥长度和密钥,并输出明文和密钥。注意,这个代码只能处理大写字母的维吉尼亚密码。如果需要处理小写字母或其他字符,需要进行一些修改。
阅读全文