search
HomeBackend DevelopmentPython TutorialEncrypted Python source code solution PyArmor

加密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

(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/ 是使用国内的豆瓣源,提升安装速度,具体可参见我的其他文章。

(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()。

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等。

(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

(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

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!

Statement
This article is reproduced at:51CTO.COM. If there is any infringement, please contact admin@php.cn delete
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.