【问题标题】:How can I modify my code in order to output the same text that was input before encryption, after decryption?如何修改我的代码,以便在加密之前和解密之后输出相同的文本?
【发布时间】:2019-04-11 09:34:52
【问题描述】:

我已经编写了加密和解密代码(不使用任何密钥),我希望在解密消息后,应按原样打印加密时输入的消息。

根据我所做的,我能够在运行解密算法后成功获取消息,但结果与我在输入时提供的顺序不同。这意味着:“h”被转换为“H”,其他字母也是如此。

# Encryption
# Trial 4

in_text = input('Enter the text that you want to encrypt: ').lower()
out_text = []
for i in in_text:
    if i == ' ':
        out_text.append(i)
        continue
    elif i in 'aeiou':
        out_text.append(ord(i) + 4)
        continue
    else:
        out_text.append(i)
final = ''.join(str(e) for e in out_text)
print(final)

# Decryption
# Trial 2

import string

user_input = input('Enter the text that you want to decrypt: ')
d_out = []
z = ''
for i in user_input:
    if i == ' ':
        d_out.append(i)
        continue
    elif i in 'bcdfghjklmnpqrstvwxyz':
        d_out.append(i)
        continue
    elif i in string.digits:
        z = z + i
        n = len(z)
        if n == 3:
            d_out.append(chr(int(z) - 4))
            z = ''

my_str = ''
for a in d_out:
    my_str = my_str + a

print(f'The decrypted message is: {my_str.title()}')

当我删除加密代码中的“.lower()”和解密代码中的“.title()”时,解密后的结果会有所不同,并且会打印一些特殊字符。

请让我知道我该如何处理。

我们将非常感谢您的回复!

案例一

加密

输入要加密的文本:hello World h105ll115 w115rld

解密

输入要解密的文本:h105ll115 w115rld 解密后的消息是:Hello World

案例 2

删除“.lower()”和“.title()”后

使加密代码如下:

in_text = input('Enter the text that you want to encrypt: ')
out_text = []
for i in in_text:
    if i == ' ':
        out_text.append(i)
        continue
    elif i in 'aeiouAEIOU':
        out_text.append(ord(i) + 4)
        continue
    else:
        out_text.append(i)
final = ''.join(str(e) for e in out_text)
print(final)

解密代码如下:

import string

user_input = input('Enter the text that you want to decrypt: ')
d_out = []
z = ''
for i in user_input:
    if i == ' ':
        d_out.append(i)
        continue
    elif i in 'bcdfghjklmnpqrstvwxyz':
        d_out.append(i)
        continue
    elif i in string.digits:
        z = z + i
        n = len(z)
        if n == 3:
            d_out.append(chr(int(z) - 4))
            z = ''

my_str = ''
for a in d_out:
    my_str = my_str + a

print(f'The decrypted message is: {my_str}')

输出如下:

加密

输入要加密的文本:Hello world H105ll115 w115rld

解密

输入要解密的文本:H105ll115 w115rld 解密后的消息是:hello world

Case 1 和 Case 2 的输出不同,但 'h' 为 'H' 或 'W' 为 'w' 或其他字符顺序相同的问题仍然存在。

【问题讨论】:

    标签: python-3.x string list encryption


    【解决方案1】:

    有多个问题。

    首先,只有您的加密程序会考虑大写字母。

    其次,您在解密过程中逐个字符地检查您的密文,而您的加密直接使用ordord 将为每个字符生成 多个 位。


    这些问题可以并且应该使用调试器而不是 StackOverflow 来发现。这不是异常的程序执行,代码完全按照您的指示执行 - 但这是不正确的。

    如果您不知道如何执行这些编码,我会先看看 Viginere 的实现并从中获得提示。


    您必须以某种方式解决多位数问题,否则您将无法区分每个 数字 的开始或结束位置(如果它们彼此相邻)。例如,您可以将数字放在括号中,例如[69] 用于 A 的“加密”(哈,密文中的有趣位置),如果您需要括号作为文本的一部分,则转义像这样的括号 [[]]

    这应该是您的方案描述的一部分,您可能应该在您(重新)开始编程之前考虑。随意尝试是行不通的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 2011-08-30
      • 2014-07-27
      • 2019-09-23
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      相关资源
      最近更新 更多