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)
whatever..replace("\"","")
. When your .py file is read, that turns intowhatever.replace(""", "")
- the triple quoted string. That's because its a regular escape for a string literal. When you then pass it toexec
, you see the error. Escaping quotes it the end of strings in python is notoriously difficult. Instead dowhatever.replace('"', "")
.