Home > Backend Development > Python Tutorial > Encrypted Python source code solution PyArmor

Encrypted Python source code solution PyArmor

WBOY
Release: 2023-05-05 15:01:06
forward
2445 people have browsed it

加密Python源码方案 PyArmor

#There are many encryption schemes for python source code. Now let’s learn how PyArmor encrypts python.

1. Introduction to PyArmor

  • PyArmor is a command line tool used to obfuscate python scripts. Bind the obfuscated script to a fixed machine, or At a certain time, the obfuscated script will expire and cannot be used.
  • You can check its usage through pyarmor -h. Only part of it is intercepted and explained below:
(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
Copy after login

(1) Main functions of pyarmor

  1. Use the command obfuscate to encrypt the script.
  2. Use the command licenses to generate a new license file license.lic for the encrypted script. If you need to set the usage period of the encrypted script or restrict the use of the script on a specific machine, you need to generate a new license file. , and encrypt the script using the new license file.
  3. Can encrypt the entire python package.
  4. You can cooperate with pyinstaller to package the python project into an independently runnable installation package.
  5. You can also use some of the solutions it provides to further improve the security of encrypted scripts. When needed for actual projects, just check its official website.

2. Usage examples

(1) Installation

pip install pyarmor -i https://pypi.douban.com/simple/
-i https://pypi.douban.com/simple/ 是使用国内的豆瓣源,提升安装速度,具体可参见我的其他文章。
Copy after login

(2)General encryption

# module1.py内容如下
def module1_func1():
print("I'm module1.py")
return 
# main.py内容如下
import module1
print("I'm main.py")
module1.module1_func1()。
Copy after login

Run command Encrypt: pyarmor main.py.

PyArmor will encrypt main.py and all *.py files in the same directory, and will generate a dist folder, which contains all the files needed to run the encryption script. The basic process:

  1. Create the output subdirectory dist.
  2. Generate the encrypted main script main.py and save it in the output directory dist.
  3. Encrypt all other *.py files in the same directory and save them to the output directory dist.
  4. Generate all auxiliary files needed to run the encryption script and save them to the output directory dist.

Verification: Go to the dist directory, python main.py.

The encrypted file looks like this:

from pytransform import pyarmor_runtime
pyarmor_runtime()
__pyarmor__(__name__, __file__, b'x50x59x41x52x4dx4fx52x00x00x03x08x00x55x0dx0dx0ax09x33xe0x02x00x00x00x00x01x00x00x00x40x00x00x00x63x01x00x00x00x00x00x18x26xa4x75x12x0cx32x8fxd9xa6xf8x0ax0bx17x1exc7xfex00x00x00x00x00x00xx22x0cxa0x75x0cx1ax13x8ax26xb4x02x46x1bx8cx5dxafxd0x81xc2x22x59xc0x1dxd2x83x99x01x09xb1x78x4cxdcx58x9bxdfx17xd9xe9x07xa4xa7xc6x51xdexaax20xbfx7cx43xbbx83x87xafx82x9ex65x2dxaexb9x5bx14xfcxf8x1dxc7x09xe5x65xa7x8dx5ax62x9dx78xa3x82x4cx53x17xc9x3ax15xa4xe7x66xdax3fxf2x9d', 2)
还会根据不同平台生成相关依赖:在pytransform文件夹下,例如windows生成相关.dll,linux生成.so等。
Copy after login

(3) Encryption with licenses

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
Copy after login

(4) It can also be bound Set on a fixed machine

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
Copy after login

For other usage methods, please refer to its official website:
https://pyarmor.readthedocs.io/en/latest/.

The above is the detailed content of Encrypted Python source code solution PyArmor. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template