-1

I am able to encrypt the file but when I decrypt it goes into a loop creating a mass file increasing size second by second with no decryption.

if choice == 3:
    string_input = input("Please enter name of file to encrypt: ")
    input_offset = int(input("Please enter offset value (1 to 96): "))
    encrypted = ""

    orig_file = open('C:\\Users\\message.txt', 'r')
    encrypted_file = open('new_msg.txt','w')
    file_read = orig_file.read()

    for file in file_read:
        x = ord(file)
        encrypted += chr(x + input_offset)
        encrypted_file.write(str(x) + " ")
        encrypted_file.write(str(encrypted))

        while x < 32:
            x += 96

        while x > 126:
            x -+ 96

    orig_file.close()
    encrypted_file.close()
    print("Encrypt successful. Encrypted text written to file: new_msg.txt")


if choice == 4:
    string_input = input("Please enter name of file to decrypt: ")
    input_offset = int(input("Please enter offset value (1 to 96): "))
    decrypted = ""

    enc_open_file = open('new_msg.txt', 'r')
    decrypted_file = open('orig_msg.txt','w')
    enc_file_read = enc_open_file.read()

    for file in enc_file_read:
        x =ord(file)
        decrypted += chr(x - input_offset)
        decrypted_file.write(str(x) + " ")
        decrypted_file.write(str(decrypted))


        while x < 32:
            x += 96

        while x > 126:
            x -+ 96


    enc_open_file.close()
    decrypted_file.close()
    print("Decrypt successful. Decrypted text written to file: orig_msg.txt")
halfer
  • 19,824
  • 17
  • 99
  • 186
Jay Sadat
  • 11
  • 4
  • 4
    "I need a quick reply." Should I stop working on my paid job to help you now? Your request seems so urgent... – Julien Apr 23 '18 at 06:06
  • anytime today would be appreciated – Jay Sadat Apr 23 '18 at 06:13
  • encrypted += chr(x + input_offset) . Seems like this line is the reason for this. The variable encrypted is appended in every loop. You should initialize encrypted to and emtpy string (encrypted = "" ) in the beginning of the for loop. – vishal Apr 23 '18 at 06:17
  • There's another loop that never terminates, see my answer. – smci Apr 23 '18 at 06:21
  • assume file content is "abcdefgh" and its character by character encryption is "12345678". In the first iteration encrypted variable is "1". In the next it is "12" and "123" and "1234" and so on. In each iteration you write the encrypted variable to the file. So your file will have "112123123412345..." instead of "123445678". – vishal Apr 23 '18 at 06:22
  • Hey you should at least figure out which line it's sticking on, run with pdb (`python -m pdb yourprog.py`), then Ctrl-C when it's hung and the console will tell you which line it's on. (See e.g. [Debugging a python application that just sort of “hangs”](https://stackoverflow.com/questions/28637795/debugging-a-python-application-that-just-sort-of-hangs) and all the many other questions and tutorials on pdb.) And/or once you narrow things down, throw in a few print statements `print("Got here 1")` – smci Apr 23 '18 at 06:24
  • serbia99, I initialized the encrypted string at the beginning of the loop but still the same issue – Jay Sadat Apr 23 '18 at 06:26
  • pass file with content "abcd" and post what output you get. – vishal Apr 23 '18 at 06:40
  • the problem is within the second if with option 4 to decrypt. It is still encrypting I don't know why, even though I have set it to (-input_offset) as the opposite of encryption (+input_offset)? – Jay Sadat Apr 23 '18 at 06:55
  • The branch for options 3 (encrypt), 4(decrypt) is identical, the only things that change are the filenames, and the prompts/messages (you could generate an extra parameter `action='encrypt'/'decrypt'`). You could, and probably should, common-case the encrypt/decrypt code that's causing the trouble. Or put it in a function `crypt(...params)` – smci Apr 23 '18 at 07:54
  • @Julien: after getting a prompt and perfectly valid answer here and complaining about it, OP reasks the same question [Encryption code in def function to be written in python](https://stackoverflow.com/questions/50044416/encryption-code-in-def-function-to-be-written-in-python) – smci Apr 28 '18 at 01:37

1 Answers1

2

Look closely at the code you wrote (you could have found this with a simple print statement, or the pdb debugger):

    while x < 32:
        x += 96

    while x > 126:
        x -+ 96
           ^

You meant x -= 96, not x -+ 96.

x -+ 96 means x - (+96) which means x - 96, but that's only an expression not an assignment, there's no LHS, it's throwing away the result, not assigning it to anything on the LHS. So any original x value > 126 never gets reduced below 126, and your loop never terminates. (Use print statements to check your code).

Hey for troubleshooting, you should at minimum figure out which line it's sticking on, run with pdb (python -m pdb yourprog.py), then Ctrl-C when it's hung and the console will tell you which line it's on. (See e.g. Debugging a python application that just sort of “hangs” and all the many other questions and tutorials on pdb.) And/or once you narrow things down, throw in a few print statements print("Got here 1")

smci
  • 32,567
  • 20
  • 113
  • 146