如何使解密代码正常工作?

时间:2018-08-09 02:29:56

标签: python

我创建了这段对文本进行加密的代码,但是当它尝试解密时,我得到了:

  

builtins.ValueError:chr()arg不在范围内(0x110000)

在使解密代码正常工作方面的任何帮助将不胜感激!

input1 = input("enter key word: ")
input2 = input1[:1]
key = ord(input2)

num_count = 32
dic= {}

while num_count <= 126:
    resultant = float(num_count*key*565)
    dic[num_count] = resultant
    num_count += 1

string = input("Please enter text: ")
num_list = ""

for i in (string):
    number = int(ord(i))
    value = (dic[number])
    number_value = str(value)
    final_value = number_value+" "
    num_list += final_value
print("Encrypted text is", num_list)

num_count3 = 0
decrypt_text = ""
string_len = len(num_list)

characters = []
localChar = ""
for i in num_list:
    if i != " ":
        localChar = localChar + i
    elif i == " ":
        characters.append(localChar)
        localChar = ""

num_count3 = 0
list_len = len(characters)

while num_count3 < list_len:
    value = float(characters[num_count3])
    valuel = int(value/key/54734)
    value2 = round(value)
    de_char = chr(value2)
    decrypt_text += de_char
    num_count3 += 1

print(decrypt_text)

1 个答案:

答案 0 :(得分:0)

说实话,代码结束了。但我希望这会有所帮助。

问题: num_count3 = 0初审未使用 string_len = len(num_list)未使用 int(value/key/54734)应该是round(value/key/565) <您的问题+ value2 = round(value)应该是value2 = int(valuel)

很多清理工作+功能很棒!

def encrypt(key, input1):
    num_count = 32
    dic = {i:float(i*key*565) for i in range(num_count,127)}

    string = input("Please enter text: ")

    num_list = ' '.join([str(dic[ord(i)]) for i in string])
    print("Encrypted text is", num_list)
    return num_list

def decrypt(key, num_list):
    characters = num_list.split()
    decrypt_text = ""
    num_count3 = 0
    list_len = len(characters)

    while num_count3 < list_len:
        value = float(characters[num_count3])
        valuel = (value/key/565)
        value2 = int(round(valuel))
        de_char = chr(value2)
        decrypt_text += de_char
        num_count3 += 1

    print(decrypt_text)

if __name__ == "__main__":
    input1 = input("enter key word: ")
    key = ord(str(input1[:1]))
    print key
    result = encrypt(key, input1)
    decrypt(key, result)