0

I am creating an encryptor for simple text files but am having trouble with the output.

Below I have provided code with random hashes I just came up with so the ones I have created can stay private (if that even matters). The if statements in the while loop produce way too many characters. I have tried setting a while loop with an iteration so that it doesn't run more than the amount of times that it needs to but nothing seems to work.

encryptor code

iteration = 0

def encryptor(to_encrypt):

    global iteration

    while iteration < len(to_encrypt):
        if "a" in to_encrypt:        
            generated_hash.insert(to_encrypt.index("a"),"sdhf789HSjff9j")
        if "b" in to_encrypt:        
            generated_hash.insert(to_encrypt.index("b"),"9hf98hJd7hg59h")
        if "c" in to_encrypt:
            generated_hash.insert(to_encrypt.index("c"),"kJShy48jJYUS24")
        iteration += 1


    return generated_hash


print("Enter data to encrypt")
to_encrypt = list(input())
generated_hash = []
hashvar = encryptor(to_encrypt)
print(hashvar)

decryptor code

def decryptor(to_decrypt):

    while len(generated_response) < len(to_decrypt):
        if "sdhf789HSjff9j" in to_decrypt:       
            generated_response.insert(to_decrypt.index("sdhf789HSjff9j"),"a")   
        if "9hf98hJd7hg59h" in to_decrypt:   
            generated_response.insert(to_decrypt.index("9hf98hJd7hg59h"),"b")
        if "kJShy48jJYUS24" in to_decrypt:   
            generated_response.insert(to_decrypt.index("kJShy48jJYUS24"),"c")

    return generated_response

print("Enter encrypted data to decrypt")
to_decrypt = str(input())
generated_response = []
hashvar = decryptor(to_decrypt)
print(hashvar)

If I were to enter my name "dylan" into the encryptor I would expect to recieve 5 hashes all correlating to the letters in my name. Instead I recieve many many hashes of duplicates of the letters in my name. When I put this in the decryptor, it seems to multiply even more and I get all the characters in my name, and only the characters in my name, but hundreds or even thousands of them in a random order. I will include an output of the encryptor and decryptor below for my name, "dylan".

Encryptor output for "dylan":

['IsDzQk2SHVgobRwiufSR', '08X4oPvziUZGmpttXuiT', 'IsDzQk2SHVgobRwiufSR', 'omCcVZArJASFahZym6vj', '08X4oPvziUZGmpttXuiT', 'swAcOwEI5j6RNq7J7X2l', 'IsDzQk2SHVgobRwiufSR', 'r83WNsrpjlpEsMUzZq43', 'omCcVZArJASFahZym6vj', '08X4oPvziUZGmpttXuiT', 'swAcOwEI5j6RNq7J7X2l', 'IsDzQk2SHVgobRwiufSR', 'r83WNsrpjlpEsMUzZq43', 'omCcVZArJASFahZym6vj', '08X4oPvziUZGmpttXuiT', 'swAcOwEI5j6RNq7J7X2l', 'IsDzQk2SHVgobRwiufSR', 'r83WNsrpjlpEsMUzZq43', 'omCcVZArJASFahZym6vj', '08X4oPvziUZGmpttXuiT', 'swAcOwEI5j6RNq7J7X2l', 'r83WNsrpjlpEsMUzZq43', 'r83WNsrpjlpEsMUzZq43', 'omCcVZArJASFahZym6vj', 'swAcOwEI5j6RNq7J7X2l']

Decryptor output after putting in the encryptor output:

['a', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'l', 'd', 'y', 'n', 'l', 'd', 'y', 'n', 'l', 'd', 'y', 'n', 'l', 'd', 'y', 'n', 'l', 'd', 'y', 'n', 'l', 'd', 'y', 'n', 'l', 'd', 'y', 'n', 'l', 'd', 'y', 'n', 'l', 'd', 'y', 'n', 'l', 'd', 'y', 'n', 'l', 'd', 'y', 'n', 'l', 'd', 'y', 'n', 'l', 'd', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'd', 'a', 'y', 'n', 'l', 'l', 'a', 'y', 'n', 'l', 'n', 'a', 'y', 'n', 'l', 'y', 'a', 'y', 'n', 'l', 'a', 'a', 'y', 'n', 'l', 'l', 'a', 'y', 'n', 'l', 'n', 'a', 'y', 'n', 'l', 'y', 'a', 'y', 'n', 'l', 'a', 'a', 'y', 'n', 'l', 'l', 'a', 'y', 'n', 'l', 'n', 'a', 'y', 'n', 'l', 'y', 'a', 'y', 'n', 'l', 'a', 'a', 'y', 'n', 'l', 'l', 'a', 'y', 'n', 'l', 'n', 'a', 'y', 'n', 'l', 'y', 'a', 'y', 'n', 'l', 'a', 'a', 'y', 'n', 'l', 'l', 'a', 'y', 'n', 'l', 'n', 'a', 'y', 'n', 'l', 'y', 'a', 'y', 'n', 'l', 'a', 'a', 'l', 'n', 'l', 'n', 'a', 'a', 'n', 'l', 'l', 'a', 'n', 'n', 'l', 'a', 'a', 'l', 'n', 'l', 'n', 'a', 'a', 'n', 'l', 'l', 'a', 'n', 'n', 'l', 'a', 'a', 'l', 'n', 'l', 'n', 'a', 'a', 'n', 'l', 'l', 'a', 'n', 'n', 'l', 'a', 'a', 'l', 'n', 'l', 'n', 'a', 'a', 'n', 'l', 'l', 'a', 'n', 'n', 'l', 'a', 'a', 'l', 'n', 'l', 'n', 'a', 'a', 'n', 'l', 'l', 'a', 'n', 'n', 'a', 'n', 'a', 'a', 'n', 'n', 'a', 'a', 'n', 'n', 'a', 'n', 'a', 'a', 'n', 'n', 'a', 'a', 'n', 'n', 'a', 'n', 'a', 'a', 'n', 'n', 'a', 'a', 'n', 'n', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
  • So, the encryptor works fine for me. Given 'dylan' I am getting ```['sdhf789HSjff9j', 'sdhf789HSjff9j', 'sdhf789HSjff9j', 'sdhf789HSjff9j', 'sdhf789HSjff9j']```. The decryptor however has problems, namely the input is a string, but you treat it as a list. Hence the long list of chars. You need to use something like ast to create a list from the string. Also setting the result, iterators outside of the functions seems risky without a benefit. Initialize inside the function. (i.e. iteration, generated_response, generated_hash) – fluxens May 28 '19 at 11:56
  • Regarding the many results you are getting from "dylan" --> for each letter you are checking whether any of the chars is in the name instead of the char! – fluxens May 28 '19 at 12:03

1 Answers1

0

So if I understand your goal correctly, you want to substitute all letters in a string with longer strings (e.g "sdhf789HSjff9j")? Besides that not being a good way to encrypt anything as it can be trivially defeated (just google breaking substitution cipher), your code has some problems.

  1. Move all iteration counters inside of the function definitions
  2. Initialize the results (i.e. generated_response, generated_hash) inside the functions
  3. Iterate over your string via for char in to_encrypt:
  4. The decryptor takes a string as an input via input() but you treat it as a list. This will treat each char of the input as a list item. Use ast.literal_eval(x) to import the string to a list if you really need this in your code. Be sure to understand why this is most likely dangerous

So below there is a simple fixed version, without the input of the encrypted text. I've replaced the substitution with a dict, to reduce the length of the if statement.

def encryptor(to_encrypt):
    generated_hash = []
    subst_encrypt = {
        "a": "sdhf789HSjff9j",
        "b": "9hf98hJd7hg59h",
        "c": "kJShy48jJYUS24",
    }
    for char in to_encrypt:
        if char in subst_encrypt:
            generated_hash.append(subst_encrypt[char])
    return generated_hash

to_encrypt = "abc"
hashvar = encryptor(to_encrypt)
print(hashvar)

def decryptor(to_decrypt):
    generated_response = []
    subst_decrypt = {
        "sdhf789HSjff9j": "a",
        "9hf98hJd7hg59h": "b",
        "kJShy48jJYUS24": "c",
    }
    for elem in to_decrypt:
        if elem in subst_decrypt:
            generated_response.append(subst_decrypt[elem])
    return generated_response

unhashed = decryptor(hashvar)
print(unhashed)

For string abc this will return

['sdhf789HSjff9j', '9hf98hJd7hg59h', 'kJShy48jJYUS24']
['a', 'b', 'c']
fluxens
  • 565
  • 3
  • 15
  • Thank you for replying. I understand that this is obviously a very weak encryption, which is why I plan on encrypting absolutely nothing with it. It's almost just a personal goal I have since I'm new to the language and coding in general. Also did not know what a dictionary is until now, not at that point in my python class yet. Thank you again, very helpful. – Dylan McGinn May 28 '19 at 21:40