From 863e61b0eadfc7559f17005873636a794c97aa72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=80=E6=A2=A6?= <3501646051@qq.com> Date: Mon, 27 Oct 2025 13:18:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=8D=95=E8=A1=A8?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E5=AF=86=E7=A0=81=E7=9A=84=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=8F=8A=E4=BA=A4=E4=BA=92=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现单表替换密码的加密解密功能,包括密钥生成和显示 提供交互式命令行界面供用户选择操作 --- classical-encryption/monoalphabetic_cipher.py | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 classical-encryption/monoalphabetic_cipher.py diff --git a/classical-encryption/monoalphabetic_cipher.py b/classical-encryption/monoalphabetic_cipher.py new file mode 100644 index 0000000..6f2e8a0 --- /dev/null +++ b/classical-encryption/monoalphabetic_cipher.py @@ -0,0 +1,134 @@ +""" +单表替换密码 (Monoalphabetic Cipher) +实现字母表的随机替换加密 +""" + +import random +import string + + +def generate_key(): + """ + 生成随机替换密钥 + 返回: 字典,包含字母到替换字母的映射 + """ + alphabet = list(string.ascii_uppercase) + shuffled = alphabet.copy() + random.shuffle(shuffled) + + key = {} + for i, char in enumerate(alphabet): + key[char] = shuffled[i] + key[char.lower()] = shuffled[i].lower() + + return key + + +def monoalphabetic_encrypt(text, key): + """ + 单表替换加密 + + Args: + text: 要加密的文本 + key: 替换密钥字典 + + Returns: + 加密后的文本 + """ + encrypted_text = "" + + for char in text: + if char in key: + encrypted_text += key[char] + else: + encrypted_text += char + + return encrypted_text + + +def monoalphabetic_decrypt(text, key): + """ + 单表替换解密 + + Args: + text: 要解密的文本 + key: 替换密钥字典 + + Returns: + 解密后的文本 + """ + # 创建反向密钥 + reverse_key = {v: k for k, v in key.items()} + + decrypted_text = "" + + for char in text: + if char in reverse_key: + decrypted_text += reverse_key[char] + else: + decrypted_text += char + + return decrypted_text + + +def display_key(key): + """ + 显示替换密钥表 + + Args: + key: 替换密钥字典 + """ + # 构建密文字母表 + plain_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + cipher_alphabet = ''.join([key[char] for char in plain_alphabet]) + + print("替换密钥表:") + print(f"明文字母表: {plain_alphabet}") + print(f"密文字母表: {cipher_alphabet}") + print() + + +def main(): + """主函数 - 交互式界面""" + print("=" * 50) + print("单表替换密码 (Monoalphabetic Cipher)") + print("=" * 50) + + # 生成密钥 + key = generate_key() + display_key(key) + + while True: + print("\n选择操作:") + print("1. 加密") + print("2. 解密") + print("3. 重新生成密钥") + print("4. 退出") + + choice = input("请输入选择 (1-4): ").strip() + + if choice == "1": + text = input("明文: ") + result = monoalphabetic_encrypt(text, key) + print(f"密文: {result}") + + elif choice == "2": + text = input("密文: ") + result = monoalphabetic_decrypt(text, key) + print(f"明文: {result}") + + elif choice == "3": + key = generate_key() + display_key(key) + print("已重新生成密钥") + + elif choice == "4": + print("再见!") + break + + else: + print("无效选择,请重新输入!") + + +if __name__ == "__main__": + main() \ No newline at end of file