简体   繁体   English

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

[英]How do I make my decrypt code work?

I created this code that encrypts text but when it tries to decrypt I get a: 我创建了此代码来加密文本,但是当它尝试解密时,我得到了:

builtins.ValueError: chr() arg not in range(0x110000) Builtins.ValueError:chr()arg不在范围内(0x110000)

Any help in making the decryption code work properly would be much appreciated! 任何使解密代码正常工作的帮助将不胜感激!

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)

going to be honest, code was all over. 老实说,代码已经结束了。 But I hope this helps. 但我希望这会有所帮助。

Issue: num_count3 = 0 first instance not use string_len = len(num_list) not used int(value/key/54734) should be round(value/key/565) < your issue + value2 = round(value) should be value2 = int(valuel) 问题: num_count3 = 0第一个实例未使用string_len = len(num_list)未使用int(value/key/54734)应该是round(value/key/565) <您的问题+ value2 = round(value)应该是value2 = int(valuel)

And lots of clean up + Functions are great! 许多清理工作+功能很棒!

 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使函数“ addlevels”在我的主代码中起作用? - How Do I make the function “addlevels” work in my main code? 如何在代码中进行错误检查,如何使前3个得分函数起作用? - How do I put error checking into this code, and how do I make my top 3 score function work? 如何在代码中修复语法,以使单选按钮可与字典配合使用? - How do I fix the syntax in my code to make my radio buttons work with my dictionary? 如何使我的多个 if 语句起作用? 或者最好如何在我的代码中使用嵌套循环 - How can I make my multiple if statements work? Or preferably how do I use nested loops in my code 如何让我的输入在我的函数中工作? - How do I make my input work inside my function? 如何在tkinter中使用destroy()方法处理我的代码? - How would I make destroy() method in tkinter work with my code? 如果输入已经预先确定,我怎样才能使我的代码工作? - How can I make my code work if the input is already predetermined? 我如何使我的代码在后台执行操作 - How do I make my code do things in the background 如果输入是元组,我怎样才能使我的代码工作? - How can I make my code work if the input is a tuple? 如何使这个 pymunk 代码工作? 它显示黑屏 - How do I make this pymunk code work? It shows a black screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM