【发布时间】: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