首页 > 解决方案 > 有没有办法让程序接受大写和非大写的输入?

问题描述

正如标题所说,我的程序使用大写字母运行,但我希望它与非大写字母和大写字母输入兼容。它应该作为大写加密返回。

decrypted = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ.1234567890 "
encrypted = b"SE2RL.1W5A0Z8D7H4B9M6JX3FTNVQGPUOYCKI "

encrypt_table = bytes.maketrans(decrypted, encrypted)
decrypt_table = bytes.maketrans(encrypted, decrypted)

result = ''
choice = ''
message = ''

while choice != 'X':
    choice = input("\n Do you want to encrypt or decrypt the message?\n E to encrypt, D to decrypt or X to exit program. ")

    if choice == 'E':
        message = input('\nEnter message for encryption: ')
        result = message.translate(encrypt_table)
        print(result + '\n\n')

    elif choice == 'D':
        message = input('\nEnter message to decrypt: ')
        result = message.translate(decrypt_table)
        print(result + '\n\n')

    elif choice != 'X':
        print('You have entered an invalid input, please try again. \n\n')

标签: pythonencryptionlowercasecapitalization

解决方案


您可以将所有小写字母添加到decrypted字符串中,也可以.upper()在用户输入中使用。

此外,这在技术上是一种替代密码。要使其成为真正的“加密”,您需要有一个“解锁”加密文本的密钥。请参阅凯撒密码以获得一个非常简单的示例(实际上它本身就是一种替换密码:P),其中关键是移位次数。或者参见AES,它可以说是迄今为止最好的加密算法。


推荐阅读