Python程序加密步骤:
-
安装pyinstaller、pycrypto和cython库。
pip install pyinstaller pip install pycrypto pip install cython
-
创建cython编译脚本build_pyd.py,该脚本将 .py文件编译为 .pyd(Windows)或 .so(Linux)文件。cython编译脚本代码为:
from distutils.core import setup from Cython.Build import cythonize setup( name='xxx', ext_modules=cythonize(["xxx.py"], language_level=3), )
-
运行cython编译脚本,生成xxx.c和xxx.so文件。
python build_pyd.py build_ext --inplace
-
删除xxx.py、xxx.pyc和xxx.c文件。
rm -rf xxx.py rm -rf xxx.pyc rm -rf xxx.c
-
使用pyinstaller命令将程序打包生成可执行文件。密钥为xxxxxxxxxxxxxxxxxx。
pyinstaller --key xxxxxxxxxxxxxxxxxx main.py
-
若步骤5报错,错误信息为ModuleNotFoundError: No module named ‘xxx’,则修改main.spec文件中的hiddenimports=[],将其修改为hiddenimports=[“xxx”],再重新运行pyinstaller命令。
vim main.spec pyinstaller --key xxxxxxxxxxxxxxxxxx main.spec
-
删除程序源代码和打包生成的中间文件。
rm -rf *.py rm -rf *.pyc rm -rf main.spec rm -rf build/
-
将资源文件移动至dist/main/目录下。
-
删除命令记录。
rm -rf .bash_history