I have an AI program written in Python. I want to deploy the script on Ubuntu/Windows machine without exposing the source script. How can I encrypt the Python script so that it is irreversibly encoded but can be used as usual (say by calling python <script_name>.py
from the terminal)?
At first, compile python source file to code objects
Iterate code object, wrap bytecode of each code object as the following format
0 JUMP_ABSOLUTE n = 3 + len(bytecode) 3 ... ... Here it's obfuscated bytecode ... n LOAD_GLOBAL ? (__pyarmor__) n+3 CALL_FUNCTION 0 n+6 POP_TOP n+7 JUMP_ABSOLUTE 0
Those obfuscated file (.pyc or .pyo) can be run and imported as normal way by common python interpreter. But when those code object is called first time, from the wrapped bytecode descripted in above section, we know
First op is JUMP_ABSOLUTE, it will jump to offset n
At offset n, the instruction is to call a PyCFunction. This function will restore those obfuscated bytecode between offset 3 and n, and place the original bytecode at offset 0
After function call, the last instruction is to jump to offset 0. The really bytecode now is executed.
There is a tool Pyarmor to obfuscate python scripts by this way.
Real encryption is not really possible but by obfuscation and compression you can make it pretty hard for anyone to understand- or reuse the source code. You can take a look at this: https://liftoff.github.io/pyminifier/
You can also try compiling your python script to byte-code, but all these processes are still semi-reversible if someone really wants to reverse-engineer your code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.