0

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

4
  • 1
    what is key ? It seems it is number - ie. 123 - so you try to do 123[i] Commented Oct 22, 2016 at 14:20
  • Catch the error with try/except and print relavent variables to see what is happening, add other print statements at strategic locations to help trace it down. Might be a good time to get familiar with The Python Debugger. As a last resort, visualize code execution at pythontutor.com/visualize.html#mode=edit Commented Oct 22, 2016 at 16:32
  • Look at the Traceback, it tells you which line(s) are causing the problem, at print("Hash: "+str(getHash(k))) what is k and what does getHash() do with it? Commented Oct 22, 2016 at 16:37
  • FYI, the entire edit history of all questions and answers are publicly available. Never post anything that is of a sensitive nature, or that you do not have permission to make publicly available. Commented Nov 12, 2016 at 15:41

1 Answer 1

3

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)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.