我有这样的代码来加密和解密字符串:
mo = int(input("Select moden1. Encryptn2. Decryptn"))
def getHash(key):
total = int(0)
key = key
for i in range(0,len(str(key))):
print("No: "+str(i))
num = ord(key[i])
total += int(num)
return total
if mo == 1:
m = input("Enter a string to encrypt: ")
k = input("Enter a password, don't forget this! ")
enc = ''
for i in range(0, len(m)):
enc += (str(int(ord(m[i]))*getHash(k))+ " ")
print(enc)
elif mo == 2:
m = input("Enter an encrypted string: ")
k = input("Enter the password to decrypt it: ")
final = ''
current = ''
for i in range(0, len(m)):
if m[i] != " ":
current += m[i]
print("Current find: "+current)
elif m[i] == " ":
print("Completed " +current)
for k in range(0,len(current)):
print("Running " +"."*k)
print("Hash: "+str(getHash(k)))
char = int(current) / getHash(k)
print(char)
final += chr(char)
current = ''
print(final)
这在加密时工作良好,但解密加密的文本时会返回
选择模式1.加密2.解密1.输入要加密的字符串:test输入密码,不要忘记!测验编号:0编号:1编号:2编号:3编号:0编号:1编号:2编号:3编号:0编号:1编号:2编号:3编号:0编号:1编号:2编号:351968 45248 51520 51968
RESTART:C:\Users\leosk\AppData\Local\Programs\Python\Python35-32\encrypter.py选择模式1.加密2.解密2.输入加密字符串:51968 45248 51520 51968输入密码进行解密:test当前查找:5当前查找:51当前查找:519当前查找:5196当前查找:51968已完成51968跑步编号:0追踪(最近一次通话(:文件"C:\Users\leosk\AppData\Local\Programs\Python35-32\encrypter.py",第31行,位于print("Hash:"+str(getHash(k((getHash中的文件"C:\Users\leosk\AppData\Local\Programs\Python35-32\encrypter.py",第7行num=ord(键[i](TypeError:"int"对象不是可下标的
您正试图使用int类型的值,就像使用列表一样。
k = input("Enter the password to decrypt it: ")
...
for k in range(0,len(current)):
我认为您偶然使用了一个变量k作为键一次,然后使用索引,并将索引值传递到getash函数中。
print("Hash: "+str(getHash(k)))