如何使我的解密代码工作?

2024-09-29 21:28:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了这个加密文本的代码,但当它试图解密时,我得到一个:

builtins.ValueError: chr() arg not in range(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)

Tags: keytextinnumberstringlenvaluecount
1条回答
网友
1楼 · 发布于 2024-09-29 21:28:52

老实说,代码都结束了。但我希望这有帮助

问题: num_count3 = 0第一个实例未使用 string_len = len(num_list)未使用 int(value/key/54734)应该是round(value/key/565)<;你的问题+ value2 = round(value)应该是value2 = int(valuel)

和大量清理+功能太好了!p>

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)

相关问题 更多 >

    热门问题