埃特巴什码加解密小程序
这几天在看CTF相关的课程,涉及到古典密码学和近代密码学还有现代密码学。
简单替换密码
Atbash Cipher
埃特巴什码(Atbash Cipher)其实可以视为下面要介绍的简单替换密码的特例,它使用字母表中的最后 一个字母代表第一个字母,倒数第二个字母代表第二个字母。在罗马字母表中,它是这样出现的:
ABCDEFGHIJKLMNOPQRSTUVWXYZ 明码表
ZYXWVUTSRQPONMLKJIHGFEDCBA 密码表
比如埃 码
明文:the quick brown fox jumps over the lazy dog
密文:gsv jfrxp yildm ulc qfnkh levi gsv ozab wlt
按照上面的规则,我编了一个小工具。源码如下:
import tkinter as tk
from tkinter import messagebox
code_dict = {'A': 'Z', 'B': 'Y', 'C': 'X', 'D': 'W', 'E': 'V', 'F': 'U', 'G': 'T', 'H': 'S', 'I': 'R', 'J': 'Q', 'K': 'P', 'L': 'O', 'M': 'N', 'N': 'M', 'O': 'L', 'P': 'K', 'Q': 'J', 'R': 'I', 'S': 'H', 'T': 'G', 'U': 'F', 'V': 'E', 'W': 'D', 'X': 'C', 'Y': 'B', 'Z': 'A'}
def encrypt(plaintext):
ciphertext = ''
for char in plaintext.upper():
if char.isalpha():
ciphertext += code_dict.get(char, '')
else:
ciphertext += char
return ciphertext
def decrypt(ciphertext):
plaintext = ''
for char in ciphertext.upper():
if char.isalpha():
for k, v in code_dict.items():
if char == v:
plaintext += k
else:
plaintext += char
return plaintext
class CaesarCipherGUI:
def __init__(self, master):
self.master = master
master.title("埃特巴什码加解密--微信号强壮Python")
# Create a frame to hold the input and output fields
self.frame = tk.Frame(master)
self.frame.pack(fill=tk.BOTH, expand=1)
# Create the input field
self.input_label = tk.Label(self.frame, text="输入信息", anchor='w', justify='left')
self.input_label.pack()
self.input_entry = tk.Entry(self.frame, width=40, justify='left')
self.input_entry.pack()
# Create the buttons
self.button_frame = tk.Frame(self.frame)
self.button_frame.pack(fill=tk.X)
self.encrypt_button = tk.Button(self.button_frame, text="加 密", command=self.encrypt_message)
self.encrypt_button.pack(side='left', padx=5)
self.decrypt_button = tk.Button(self.button_frame, text="解 密", command=self.decrypt_message)
self.decrypt_button.pack(side='left', padx=25)
# Create the output field
self.output_label = tk.Label(self.frame, text="输 出", anchor='w')
self.output_label.pack()
self.output_text = tk.Text(self.frame, width=52, height=10, wrap='word')
self.output_text.pack()
def encrypt_message(self):
plaintext = self.input_entry.get()
ciphertext = encrypt(plaintext).lower()
self.output_text.delete('1.0', tk.END)
self.output_text.insert('1.0', ciphertext)
def decrypt_message(self):
ciphertext = self.input_entry.get()
plaintext = decrypt(ciphertext).lower()
self.output_text.delete('1.0', tk.END)
self.output_text.insert('1.0', plaintext)
root = tk.Tk()
my_gui = CaesarCipherGUI(root)
root.mainloop()
备注:界面是用AI生成,稍加修改。
运行结果如下图: