pyminifier 方法加密、混淆Python代码

均在 cmd 中操作

1. 安装

pip install pyminifier

如果报错,就参考链接:一文解决pip报错

2. 代码加密 (UnicodeDecodeError解决链接

pyminifier --nonlatin --replacement-length=10 -O xxx.py  注:(10和大写ou)

该方法比较普通,加密效果弱
在这里插入图片描述

3. 另一种方法, 利用Base64对代码进行再次编码,再利用lzma算法进行压缩,但是这种加密方式仅仅用于代码没有调用隐式导入的情况。 (UnicodeDecodeError解决链接

pyminifier --lzma "xxx.py"

在这里插入图片描述

4. 两种方法混合使用,效果最佳
5. 语法介绍

replacement-length 代码的变量的一个混淆长度, int
-O :混淆所有函数/方法名、变量和类。默认是不要混淆。

7. 操作示例:

# 将脚本 demo02.py 按 lzma 加密,并生成新的脚本 demo02_2.py
C:\Users\54867>pyminifier --lzma "demo02.py">demo02_2.py
# 将脚本 demo02.py 加密,并生成新的脚本 new_demo02.py
C:\Users\54867>pyminifier -O --replacement-length=10 ./demo02.py>new_demo02.py
#在 cmd 中执行这个脚本,查看是否正常运行
C:\Users\54867>python new_demo02.py
# 加密后生成新脚本,并自定义路径(已有路径)
C:\Users\54867>pyminifier -O --replacement-length=10 ./demo02.py>D:\输出\new_demo02.py

8. pyminifier 详细命令方法如下:

pyminifier -h
Usage: pyminifier [options] "<input file>"

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -o <file path>, --outfile=<file path>
                        Save output to the given file.
                        
  -d <file path>, --destdir=<file path>
                        Save output to the given directory. This option is
                        required when handling multiple files. Defaults to
                        './minified' and will be created if not present.
                        将输出保存到给定的目录。当处理多个文件时,此选项是必需的。默认为'./minified',如果不存在,将被创建。
                        
  --nominify            Don't bother minifying (only used with --pyz).

  --use-tabs            Use tabs for indentation instead of spaces.
  						使用制表符代替空格来缩进。
  
  --bzip2               bzip2-compress the result into a self-executing python
                        script.  Only works on stand-alone scripts without
                        implicit imports.
                        以bzip2方式压缩将结果到一个自动执行的python脚本中。只能在独立脚本上工作,不需要隐式导入。
                        
  --gzip                gzip-compress the result into a self-executing python
                        script.  Only works on stand-alone scripts without
                        implicit imports.
                        以gzip方式压缩结果到一个自执行的python脚本中。只能在独立脚本上工作,不需要隐式导入。
                        
  --lzma                lzma-compress the result into a self-executing python
                        script.  Only works on stand-alone scripts without
                        implicit imports.
						以lzma方式压缩将结果到一个自动执行的python脚本中。只能在独立脚本上工作,不需要隐式导入。

  --pyz=<name of archive>.pyz
                        zip-compress the result into a self-executing python
                        script. This will create a new file that includes any
                        necessary implicit (local to the script) modules.
                        Will include/process all files given as arguments to
                        pyminifier.py on the command line.
  -O, --obfuscate       Obfuscate all function/method names, variables, and
                        classes.  Default is to NOT obfuscate.
  --obfuscate-classes   Obfuscate class names.
  --obfuscate-functions
                        Obfuscate function and method names.
  --obfuscate-variables
                        Obfuscate variable names.
  --obfuscate-import-methods
                        Obfuscate globally-imported mouled methods (e.g.
                        'Ag=re.compile').
  --obfuscate-builtins  Obfuscate built-ins (i.e. True, False, object,
                        Exception, etc).
  --replacement-length=1
                        The length of the random names that will be used when
                        obfuscating identifiers.
  --nonlatin            Use non-latin (unicode) characters in obfuscation
                        (Python 3 only).  WARNING: This results in some
                        SERIOUSLY hard-to-read code.
  --prepend=<file path>
                        Prepend the text in this file to the top of our
                        output.  e.g. A copyright notice.

感悟:

1 将文件目录保留,然后逐个代码加密,最后报错了,建议把整个文件夹(含文件)拷贝过来,逐个加密,边试用边加密。
2 注意,目前pyminifier只能处理单个文件,而且部分脚本混淆后不可运行,需要手动测试[5]。

参考链接:
[1] 链接1
[2] 链接2
[3] 链接3
[4] 官方文档
[5] Python代码混淆工具,Python源代码保密、加密、混淆 2019.6

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值