-2

I'm working on a project that needs encryption for packaging, and following the advice on this question I went for @Pro Chess 's method but when trying to run the code it reports this error:

Exception has occurred: SyntaxError
unterminated triple-quoted string literal (detected at line 6466) (<string>, line 837)
  File "C:\Users\VPalmero\Desktop\python_projects\ChampionsJourney\sources\python\destination_joint_file.py", line 6484, in <module>
    exec(decrypted_message)
SyntaxError: unterminated triple-quoted string literal (detected at line 6466) (<string>, line 837)

However, my bytearray is opened at the beginning and closed at the end and Ctrl+Fing the file i see no other appearance of """. It's a rather long file which I can't share, roughly 6k5 lines of code, could it be that this is affecting? My VS linter isn't telling me where the error could be either.

Adding the file: download link

The problem is reproduced with this simplified test. The encryption cycle isn't necessary to demonstrate, but follows the actions of the full code.

from cryptography.fernet import Fernet

code_a = b"""

"".replace("\"", "")

                            
"""

code = code_a
key = Fernet.generate_key()
encryption_type = Fernet(key)
encrypted_message = encryption_type.encrypt(code)
decrypted_message = encryption_type.decrypt(encrypted_message)
print("EXEC")
exec(decrypted_message)
2
  • 1
    The problem is in the bytes strings where you try to replace quotes whatever..replace("\"",""). When your .py file is read, that turns into whatever.replace(""", "") - the triple quoted string. That's because its a regular escape for a string literal. When you then pass it to exec, you see the error. Escaping quotes it the end of strings in python is notoriously difficult. Instead do whatever.replace('"', "").
    – tdelaney
    Commented Aug 29 at 19:00
  • Done! Solved! Thanks @tdelaney Commented Aug 29 at 19:14

0

Browse other questions tagged or ask your own question.