python原始碼的加密方案有很多,現在我們來了解PyArmor如何為python加密。
(tt) PS C:test> pyarmor -h usage: pyarmor [-h] [-v] [-q] [-d] [--home HOME] [--boot BOOT]... optional arguments: -h, --help show this help message and exit -v, --versionshow program's version number and exit -q, --silent Suppress all normal output -d, --debugPrint exception traceback and debugging message --home HOMEChange pyarmor home path --boot BOOTChange boot platform The most commonly used pyarmor commands are: obfuscate (o) Obfuscate python scripts licenses (l) Generate new licenses for obfuscated scripts pack (p) Pack obfuscated scripts to one bundle init (i) Create a project to manage obfuscated scripts config (c) Update project settings build (b)Obfuscate all the scripts in the project info Show project information checkCheck consistency of project hdinfo Show all available hardware information benchmarkRun benchmark test in current machine register Make registration keyfile work download Download platform-dependent dynamic libraries runtimeGenerate runtime package separately help Display online documentation See "pyarmor <command> -h" for more information on a specific command. More usage refer to https://pyarmor.readthedocs.io
pip install pyarmor -i https://pypi.douban.com/simple/ -i https://pypi.douban.com/simple/ 是使用国内的豆瓣源,提升安装速度,具体可参见我的其他文章。
# module1.py内容如下 def module1_func1(): print("I'm module1.py") return # main.py内容如下 import module1 print("I'm main.py") module1.module1_func1()。
執行指令進行加密:pyarmor o main.py。
PyArmor會加密main.py和相同目錄下面的所有*.py 文件,會產生dist資料夾,其包含執行加密腳本所需的全部文件,基本流程:
驗證:到dist目錄下,python main.py。
加密後的檔案是這樣的:
from pytransform import pyarmor_runtime pyarmor_runtime() __pyarmor__(__name__, __file__, b'x50x59x41x52x4dx4fx52x00x00x03x08x00x55x0dx0dx0ax09x33xe0x02x00x00x00x00x01x00x00x00x40x00x00x00x63x01x00x00x00x00x00x18x26xa4x75x12x0cx32x8fxd9xa6xf8x0ax0bx17x1exc7xfex00x00x00x00x00x00xx22x0cxa0x75x0cx1ax13x8ax26xb4x02x46x1bx8cx5dxafxd0x81xc2x22x59xc0x1dxd2x83x99x01x09xb1x78x4cxdcx58x9bxdfx17xd9xe9x07xa4xa7xc6x51xdexaax20xbfx7cx43xbbx83x87xafx82x9ex65x2dxaexb9x5bx14xfcxf8x1dxc7x09xe5x65xa7x8dx5ax62x9dx78xa3x82x4cx53x17xc9x3ax15xa4xe7x66xdax3fxf2x9d', 2) 还会根据不同平台生成相关依赖:在pytransform文件夹下,例如windows生成相关.dll,linux生成.so等。
1.生成新的许可文件 pyarmor l -e 2022-09-09 test01 执行这条命令会生成一个带有效期的认证文件: 创建license.lic与license.lic.txt,保存在licenses/test01目录下 2.使用新生成的许可文件加密脚本 pyarmor o --with-license .licensestest01license.lic main.py 3.同样可以在dist目录下找到相关,此时如果过期了,则会提示:License is expired
1.在该机器上运行命令获取硬件信息 pyarmor hdinfo 2.然后生成绑定的固定机器的许可文件 pyarmor l --bind-disk "100304PBN2081SF3NJ5T" --bind-mac "20:c1:d2:2f:a0:96" code-002 3.使用这个许可文件加密脚本,加密脚本就只能在指定机器上运行 pyarmor o --with-license licenses/code-002/license.lic main.py
其他更多使用方式可以參考其官網:
https://pyarmor.readthedocs.io/en/latest/。
以上是加密Python源碼方案 PyArmor的詳細內容。更多資訊請關注PHP中文網其他相關文章!