我的加密程序需要一些帮助。我不想让程序只是将字母移动两个(c 会变成 a 或 r 会变成 p)我希望它能够引用 2 个列表,第一个列表通常从 az 开始,另一个字母不同为了充当加密/解密方。希望这是有道理的。这是我到目前为止所拥有的。
result = ''
choice = ''
message = ''
while choice != 0:
choice = input("\n Do you want to encrypt or decrypt the message?\n 1 to encrypt, 2 to decrypt or 0 to exit program. ")
if choice == '1':
message = input('\nEnter message for encryption: ')
for i in range(0, len(message)):
result = result + chr(ord(message[i]) - 2)
print(result + '\n\n')
result = ''
if choice == '2':
message = input('\nEnter message to decrypt: ')
for i in range(0, len(message)):
result = result + chr(ord(message[i]) + 2)
print(result + '\n\n')
result = ''
elif choice != '0':
print('You have entered an invalid input, please try again. \n\n')
这工作正常而且花花公子,但我想要列表。假设列表 1 是 A、B、C、D、E,列表 2 是 W、N、U、D、P。只是为了便于使用。
原文由 hppylttletrees 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是一个解决方案,仅适用于小写字母。通过将大写字母添加到文本字符串中,可以轻松修改它以处理大写字母。
可以看出,空格字符在两个列表中的位置相同。这不是必需的,因为任何字符都可以转换为任何其他字符。但是,如果
decrypted
或encrypted
不只包含唯一字符,程序就会崩溃。