我写了一个用于加密输入的函数,但我在使用解密函数时遇到了问题

I wrote a function for encrypting an input, but I am having trouble with a decryption function

我的简单加密函数有效。

def crypt():
    so = input("Enter secret number: ")
    old_dic = {chr(i): i - (int(so)) for i in range(ord("A"), ord("A") + 26)}
    inp = str(input("Cryptify: ")).upper()
    for key,value in old_dic.items():
        if len(inp) >= 1:
            bit = list(inp)
            spell = list(map(old_dic.get, bit))
            spell = spell[::-1]    
            print(spell)
            break
        else:
            print("Improper input.")
            break 

运行函数:

crypt()

Enter secret number: 45

Cryptify: Hello

输出:

[34, 31, 31, 24, 27]

我想使用这个输出数字列表并使用解密函数和秘密数字打印出什么是咒语。

我的解密功能不起作用

def decrypt():
    so1 = input("Enter secret number: ")
    new_dic = {chr(i): i - (int(so1)) for i in range(ord("a"), ord("a") + 26)}
    inp2 = list(map(str,(input("Decryptify: ").strip().split())))
    for key,value in new_dic.items():
        if len(inp2) >= 1:
            bit2 = list(inp2)
            spell2 = list(map(new_dic.get, bit2))
            spell2 = spell2[::-1]    
            print(spell2)
            break
        else:
            print("Improper input.")
            break 

运行函数:

decrypt()

Enter secret number: 45

Decryptify: 34, 31, 31, 24, 27

输出:

[None, None, None, None, None]

我无法全神贯注于需要发生的事情,但尽管使用相同的输入,但我的字典值在两个函数之间完全不同。

我想要的输出是:

[H, E, L, L, O]

你有几个问题。最大的问题是你的解密函数试图将小写字母映射到数字,但你没有给它小写字母。本质上,你需要在创建new_dic时反转key/value对。接下来,您正在输入 ASCII 数字,但从未将它们转换为数字。

此代码有效。看看你是否能发现差异。哦,输入要解密的数据时不要使用逗号。只需用空格分隔,因为您使用了 .split().

而且,顺便说一下,如果这些函数只是打印答案,那么它们几乎没有用。您应该 return 回答并让来电者决定如何处理结果。

def crypt():
    so = input("Enter secret number: ")
    old_dic = {chr(i): i - (int(so)) for i in range(ord("A"), ord("A") + 26)}
    print(old_dic)
    inp = str(input("Cryptify: ")).upper()
    if len(inp) >= 1:
        bit = list(inp)
        spell = list(map(old_dic.get, bit))
        spell = spell[::-1]    
        return spell
    else:
        print("Improper input.")

def decrypt():
    so1 = input("Enter secret number: ")
    new_dic = {i - (int(so1)):chr(i) for i in range(ord("A"), ord("A") + 26)}
    print(new_dic)
    inp2 = list(map(int,(input("Decryptify: ").strip().split())))
    if len(inp2) >= 1:
        spell2 = list(map(new_dic.get, inp2))
        spell2 = spell2[::-1]    
        return spell2
    else:
        print("Improper input.")

print(crypt())
print(decrypt())

输出(我打印出地图字典的内容来帮助调试):

C:\tmp>python x.py
Enter secret number: 45
{'A': 20, 'B': 21, 'C': 22, 'D': 23, 'E': 24, 'F': 25, 'G': 26, 'H': 27, 'I': 28, 'J': 29, 'K': 30, 'L': 31, 'M': 32, 'N': 33, 'O': 34, 'P': 35, 'Q': 36, 'R': 37, 'S': 38, 'T': 39, 'U': 40, 'V': 41, 'W': 42, 'X': 43, 'Y': 44, 'Z': 45}
Cryptify: hello
[34, 31, 31, 24, 27]
Enter secret number: 45
{20: 'A', 21: 'B', 22: 'C', 23: 'D', 24: 'E', 25: 'F', 26: 'G', 27: 'H', 28: 'I', 29: 'J', 30: 'K', 31: 'L', 32: 'M', 33: 'N', 34: 'O', 35: 'P', 36: 'Q', 37: 'R', 38: 'S', 39: 'T', 40: 'U', 41: 'V', 42: 'W', 43: 'X', 44: 'Y', 45: 'Z'}
Decryptify: 34 31 31 24 27
['H', 'E', 'L', 'L', 'O']

C:\tmp>