应用开发

加密Python源码方案 PyArmor

时间:2010-12-5 17:23:32  作者:IT科技类资讯   来源:IT科技类资讯  查看:  评论:0
内容摘要:python源码的加密方案有很多,现在我们来了解PyArmor如何给python加密。1、PyArmor介绍PyArmor是一个用来混淆python脚本的命令行工具,将混淆脚本绑定到固定的机器上,或到

python源码的加密加密方案有很多,现在我们来了解PyArmor如何给python加密。源码

1、加密PyArmor介绍

PyArmor是源码一个用来混淆python脚本的命令行工具,将混淆脚本绑定到固定的加密机器上,或到一定的源码时间让混淆的脚本过期不能用。可通过pyarmor -h查看其用法,加密以下只截取部分进行说明:(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, --version show programs version number and exit

-q, --silent Suppress all normal output

-d, --debug Print exception traceback and debugging message

--home HOME Change pyarmor home path

--boot BOOT Change 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

check Check consistency of project

hdinfo Show all available hardware information

benchmark Run benchmark test in current machine

register Make registration keyfile work

download Download platform-dependent dynamic libraries

runtime Generate runtime package separately

help Display online documentation

See "pyarmor -h" for more information on a specific command.

More usage refer to https://pyarmor.readthedocs.io

(1)pyarmor主要功能

使用命令 obfuscate 来加密脚本。使用命令 licenses 为加密脚本生成新的加密许可文件 license.lic,如果需要设置加密脚本的源码使用期限或者限制脚本在特定的机器使用,需要生成新的加密许可文件, 并使用新的源码许可文件加密脚本。可以加密整个python包。加密可以配合pyinstaller对python工程打包成一个独立可运行的香港云服务器源码安装包。还可以利用其提供的加密一些方案,进一步提升加密脚本的安全性,具体在实际项目需要时,再查阅其官网即可。

2、使用示例

(1)安装

pip install pyarmor -i https://pypi.douban.com/simple/

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

(2)一般加密

# module1.py内容如下

def module1_func1():

print("Im module1.py")

return

# main.py内容如下

import module1

print("Im main.py")

module1.module1_func1()。

运行命令进行加密:pyarmor o main.py。

PyArmor会加密main.py和相同目录下面的所有*.py 文件,会生成dist文件夹,其包含运行加密脚本所需要的全部文件,基本过程:

创建输出子目录 dist。生成加密的主脚本 main.py 保存在输出目录 dist。云南idc服务商加密相同目录下其他所有 *.py 文件,保存到输出目录 dist。生成运行加密脚本所需要的全部辅助文件,保存到输出目录 dist。

验证:到dist目录下,python main.py。

加密后的文件是这样的:

from pytransform import pyarmor_runtime

pyarmor_runtime()

__pyarmor__(__name__, __file__, b\x50\x59\x41\x52\x4d\x4f\x52\x00\x00\x03\x08\x00\x55\x0d\x0d\x0a\x09\x33\xe0\x02\x00\x00\x00\x00\x01\x00\x00\x00\x40\x00\x00\x00\x63\x01\x00\x00\x00\x00\x00\x18\x26\xa4\x75\x12\x0c\x32\x8f\xd9\xa6\xf8\x0a\x0b\x17\x1e\xc7\xfe\x00\x00\x00\x00\x00\x00\x\x22\x0c\xa0\x75\x0c\x1a\x13\x8a\x26\xb4\x02\x46\x1b\x8c\x5d\xaf\xd0\x81\xc2\x22\x59\xc0\x1d\xd2\x83\x99\x01\x09\xb1\x78\x4c\xdc\x58\x9b\xdf\x17\xd9\xe9\x07\xa4\xa7\xc6\x51\xde\xaa\x20\xbf\x7c\x43\xbb\x83\x87\xaf\x82\x9e\x65\x2d\xae\xb9\x5b\x14\xfc\xf8\x1d\xc7\x09\xe5\x65\xa7\x8d\x5a\x62\x9d\x78\xa3\x82\x4c\x53\x17\xc9\x3a\x15\xa4\xe7\x66\xda\x3f\xf2\x9d, 2)

还会根据不同平台生成相关依赖:在pytransform文件夹下,例如windows生成相关.dll,linux生成.so等。

(3)带licenses的加密

1.生成新的许可文件

pyarmor l -e 2022-09-09 test01

执行这条命令会生成一个带有效期的认证文件: 创建license.lic与license.lic.txt,保存在licenses/test01目录下

2.使用新生成的许可文件加密脚本

pyarmor o --with-license .\licenses\test01\license.lic main.py

3.同样可以在dist目录下找到相关,此时如果过期了,则会提示:License is expired

(4)也可以绑定在固定机器上

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/。云服务器提供商
copyright © 2025 powered by 益强资讯全景  滇ICP备2023006006号-31sitemap