我创建了这个加密文本的代码,但当它试图解密时,我得到一个:
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)
老实说,代码都结束了。但我希望这有帮助
问题:
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)
;相关问题 更多 >
编程相关推荐