用Vigenere密码算法加解密文本文件中的文本信息,设密钥长度为7,密钥在程序执行时动态输入,加密得到的密文放在另一个文本文件中,设计的程序要有界面。
时间: 2024-03-10 10:45:53 浏览: 73
好的,我可以为您提供一个Python程序实现您的要求。以下是代码:
```python
from tkinter import *
from tkinter import filedialog
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def vigenere_cipher(plaintext, key):
ciphertext = ""
key_index = 0
for letter in plaintext:
if letter.isalpha():
shift = ALPHABET.index(key[key_index].upper())
cipher_letter = ALPHABET[(ALPHABET.index(letter.upper()) + shift) % 26]
if letter.islower():
cipher_letter = cipher_letter.lower()
ciphertext += cipher_letter
key_index = (key_index + 1) % len(key)
else:
ciphertext += letter
return ciphertext
def vigenere_decipher(ciphertext, key):
plaintext = ""
key_index = 0
for letter in ciphertext:
if letter.isalpha():
shift = ALPHABET.index(key[key_index].upper())
plain_letter = ALPHABET[(ALPHABET.index(letter.upper()) - shift) % 26]
if letter.islower():
plain_letter = plain_letter.lower()
plaintext += plain_letter
key_index = (key_index + 1) % len(key)
else:
plaintext += letter
return plaintext
def browse_file():
file_path = filedialog.askopenfilename()
file_path_entry.delete(0, END)
file_path_entry.insert(0, file_path)
def browse_save_file():
file_path = filedialog.asksaveasfilename(defaultextension=".txt")
save_file_path_entry.delete(0, END)
save_file_path_entry.insert(0, file_path)
def encrypt_file():
file_path = file_path_entry.get()
save_file_path = save_file_path_entry.get()
key = key_entry.get()
if file_path and save_file_path and key:
with open(file_path, "r") as file:
plaintext = file.read()
ciphertext = vigenere_cipher(plaintext, key)
with open(save_file_path, "w") as save_file:
save_file.write(ciphertext)
result_label.config(text="Encryption completed.")
else:
result_label.config(text="Please fill in all fields.")
def decrypt_file():
file_path = file_path_entry.get()
save_file_path = save_file_path_entry.get()
key = key_entry.get()
if file_path and save_file_path and key:
with open(file_path, "r") as file:
ciphertext = file.read()
plaintext = vigenere_decipher(ciphertext, key)
with open(save_file_path, "w") as save_file:
save_file.write(plaintext)
result_label.config(text="Decryption completed.")
else:
result_label.config(text="Please fill in all fields.")
root = Tk()
root.title("Vigenere Cipher")
file_path_label = Label(root, text="Select File:")
file_path_label.grid(row=0, column=0, padx=5, pady=5, sticky=W)
file_path_entry = Entry(root, width=50)
file_path_entry.grid(row=0, column=1, padx=5, pady=5)
browse_button = Button(root, text="Browse", command=browse_file)
browse_button.grid(row=0, column=2, padx=5, pady=5)
save_file_path_label = Label(root, text="Save File As:")
save_file_path_label.grid(row=1, column=0, padx=5, pady=5, sticky=W)
save_file_path_entry = Entry(root, width=50)
save_file_path_entry.grid(row=1, column=1, padx=5, pady=5)
browse_save_button = Button(root, text="Browse", command=browse_save_file)
browse_save_button.grid(row=1, column=2, padx=5, pady=5)
key_label = Label(root, text="Enter Key:")
key_label.grid(row=2, column=0, padx=5, pady=5, sticky=W)
key_entry = Entry(root, width=50)
key_entry.grid(row=2, column=1, padx=5, pady=5)
encrypt_button = Button(root, text="Encrypt", command=encrypt_file)
encrypt_button.grid(row=3, column=0, padx=5, pady=5)
decrypt_button = Button(root, text="Decrypt", command=decrypt_file)
decrypt_button.grid(row=3, column=1, padx=5, pady=5)
result_label = Label(root, text="")
result_label.grid(row=4, column=0, columnspan=2, padx=5, pady=5)
root.mainloop()
```
通过运行该程序,您将获得一个简单的GUI界面,您可以在其中选择要加密/解密的文件,选择保存位置,动态输入密钥并执行操作。请注意,这个程序假定文件中的文本只包含英文字符(大写/小写)和标点符号。如果您的文件中有其他字符,请先处理它们,否则程序可能会出错。
阅读全文