Reputation: 43
I have this code to encrypt and decrypt strings:
mo = int(input("Select mode\n1. Encrypt\n2. Decrypt\n"))
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)
This works fine when encrypting but decrypting an encrypted peice of text it returns
Select mode 1. Encrypt 2. Decrypt 1 Enter a string to encrypt: test Enter a password, don't forget this! test No: 0 No: 1 No: 2 No: 3 No: 0 No: 1 No: 2 No: 3 No: 0 No: 1 No: 2 No: 3 No: 0 No: 1 No: 2 No: 3 51968 45248 51520 51968
RESTART: C:\Users\leosk\AppData\Local\Programs\Python\Python35-32\encrypter.py Select mode 1. Encrypt 2. Decrypt 2 Enter an encrypted string: 51968 45248 51520 51968 Enter the password to decrypt it: test Current find: 5 Current find: 51 Current find: 519 Current find: 5196 Current find: 51968 Completed 51968 Running No: 0 Traceback (most recent call last): File "C:\Users\leosk\AppData\Local\Programs\Python\Python35-32\encrypter.py", line 31, in print("Hash: "+str(getHash(k))) File "C:\Users\leosk\AppData\Local\Programs\Python\Python35-32\encrypter.py", line 7, in getHash num = ord(key[i]) TypeError: 'int' object is not subscriptable
Upvotes: 0
Views: 842
Reputation: 868
You are trying to use a value of type int as you would a list.
k = input("Enter the password to decrypt it: ")
...
for k in range(0,len(current)):
I think you are by accident using a variable k as key once, and index later, and passing the index value into your gethash function.
print("Hash: "+str(getHash(k)))
Upvotes: 3