本文揭示了初学者在实现简易密码替换加密时最常见的错误——使用含重复键的字典构建 `str.maketrans()` 映射表,导致加密/解密规则不一致、字符还原失真。
在你提供的两个版本代码中,核心问题并非逻辑结构或输入处理,而在于 str.maketrans() 所依赖的字典存在非法重复键,这直接破坏了“一对一可逆替换”的基本前提。
以 Version 1 的 psw_encryption() 为例:
{"a": "b", ..., "a": "z", "5": "z", "4": "9", ..., "8": "5", "8": "x", ...}这里 "a" 出现两次(映射到 "b" 和 "z"),"8" 也出现两次(映射到 "5" 和 "x")。在 Python 字典中,后写入的键值对会覆盖先写入的 —— 因此 "a" 最终只映射为 "z","8" 只映射为 "x"。同理,在 psw_decrypt() 中,"b" 被同时映射为 "a" 和 "6",最终仅保留后者。
这就造成:
要实现可靠的手动替换加解密,必须满足:
利用 str.maketrans(str1, str2) 的双字符串构造方式(更安全、直观),并确保两字符串长度相等、字符一一对应:
# 定义明文字符集(所有可能输入字符) plain_chars = "abcdefghijklmnopqrstuvwxyz0123456789" # 定义严格对应的密文字符集(打乱顺序,无重复) cipher_chars = "m9xkq2vblp4zjy8n3t0c5d7r6e1fghswoiau" # 构建可逆映射 encrypt_table = str.maketrans(plain_chars, cipher_chars) decrypt_table = str.maketrans(cipher_chars, plain_chars) # 自动反向 def psw_encrypt(): pwd = input("What is your password? ").lower() # 统一小写便于控制 encrypted = pwd.translate(encrypt_table) print("Your encrypted password is:", encrypted) def psw_decrypt(): pwd = input("What is your encrypted password? ") decrypted = pwd.translate(decrypt_table) print("Your decrypted password is:", decrypted)
? 提示:str.maketrans(str1, str2) 要求 len(str1) == len(str2),且自动保证一一映射,彻底规避字典键冲突问题。
你的代码问题本质是 数据结构误用:用字典手动构造映射时忽略了键唯一性约束,导致加密与解密规则脱节。修复的关键在于——
✅ 使用 str.maketrans(str1, str2) 保证字符集严格一一对应;
✅ 验证明文/密文字符集长度相等、无重复;
✅ 分离核心替换逻辑与辅助操作(如后缀添加);
✅ 理解 translate() 是纯字符映射,不处理语义或上下文。
掌握这一点,你就迈出了理解密码学映射原理和 Python 字符串处理的重要一步。