如何用python写一个加密程序

新手上路,请多包涵

我的加密程序需要一些帮助。我不想让程序只是将字母移动两个(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 许可协议

阅读 703
2 个回答

这是一个解决方案,仅适用于小写字母。通过将大写字母添加到文本字符串中,可以轻松修改它以处理大写字母。

可以看出,空格字符在两个列表中的位置相同。这不是必需的,因为任何字符都可以转换为任何其他字符。但是,如果 decryptedencrypted 不只包含唯一字符,程序就会崩溃。

 decrypted = b"abcdefghijklmnopqrstuvwxyz "
encrypted = b"qwertyuiopasdfghjklzxcvbnm "

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

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: ')
        result = message.translate(encrypt_table)
        print(result + '\n\n')

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

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

原文由 JohanL 发布,翻译遵循 CC BY-SA 3.0 许可协议

好的,这里有几件事……

首先,我会准确地告诉您您要查找的内容,并解释我使用的内容以及需要对您的原始代码进行的一些更改。然后我会解释一些固有的问题,你正在尝试做什么,并建议一些领域来阅读/一些你可能想要改进你所拥有的东西的方法。

这是您正在寻找的代码(同时保留与您在上面提交的内容相同的流程):

 import random

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

characters_in_order = [chr(x) for x in range(32,127)]

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 str(choice) == '1':
        message = input('\nEnter message for encryption: ')

        r_seed = input('Enter an integer to use as a seed: ')
        random.seed(r_seed)
        shuffled_list = [chr(x) for x in range(32,127)]
        random.shuffle(shuffled_list)

        for i in range(0, len(message)):
            result += shuffled_list[characters_in_order.index(message[i])]

        print(result + '\n\n')
        result = ''

    elif str(choice) == '2':
        message = input('\nEnter message to decrypt: ')

        r_seed = input('Enter an integer to use as a seed (should be the same one used to encrypt): ')
        random.seed(r_seed)
        shuffled_list = [chr(x) for x in range(32,127)]
        random.shuffle(shuffled_list)

        for i in range(0, len(message)):
            result += characters_in_order[shuffled_list.index(message[i])]

        print(result + '\n\n')
        result = ''

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

您会注意到我设置了一个全局“按顺序排列的字符”列表,它只是按顺序排列的每个 ASCII 字符 (32-126)。我还导入了“随机”模块,并使用它根据用户输入的种子按顺序打乱字符。只要这个种子在加解密端是一样的,就会产生一样的shuffled list,应该能加密或解密一样的字符串。还要注意输入选项周围的 str() 。否则,用户必须输入“1”而不是 1 才能提交选择而不会出现错误。

所有这些都说…

  1. 请注意,新函数的工作方式是查看一个列表中字符的索引,然后在另一个列表中提取该索引处的字符。您使用的递增或递减字符 ASCII 码的方法是基本的(虽然没有比这更基本),但它也有一个非常严重的缺陷,即 ASCII 集一端或另一端的字符不会’ t 返回 ASCII 字符。如果你在位级别加密它,那将是首选,这无关紧要/无关紧要,但是在这里你不会得到你想要的那种字符串,例如, 在要加密的明文中输入 [空格] (ASCII 32)。
  2. 如果您有兴趣,您可能想阅读有关对称密钥加密/DES 的一些想法,了解如何真正完成加密,尽管开始/感兴趣的道具,这当然是创建某种密码谜题的有趣方式或类似的规定。我不会假装是任何专家,但我至少可以为您指明写作方向。 ( https://en.wikipedia.org/wiki/Data_Encryption_Standard https://en.wikipedia.org/wiki/Symmetric-key_algorithm )
  3. 考虑让您的代码在 .txt 文件中读取并打印到 .txt 文件,而不是使用用户输入的消息。

再说一遍,我无论如何都不是专家,而且您的目标程序肯定有一些有趣的用途,如果您对此感兴趣,只是想为您指明正确的方向。希望所有这些都是有帮助的!

原文由 slearner 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏