feat: 添加单表替换密码的实现及交互界面
实现单表替换密码的加密解密功能,包括密钥生成和显示 提供交互式命令行界面供用户选择操作
This commit is contained in:
134
classical-encryption/monoalphabetic_cipher.py
Normal file
134
classical-encryption/monoalphabetic_cipher.py
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user