如何有效地混淆 Python 代码? [英] How to obfuscate Python code effectively?

查看:110
本文介绍了如何有效地混淆 Python 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何隐藏我的 Python 源代码.

I am looking for how to hide my Python source code.

print "Hello World!" 

我怎样才能对这个例子进行编码,使它不是人类可读的?有人告诉我使用 base64,但我不确定如何使用.

How can I encode this example so that it isn't human-readable? I've been told to use base64 but I'm not sure how.

推荐答案

这只是一个有限的一级混淆解决方案,但它是内置的:Python 有一个编译器来字节码:

This is only a limited, first-level obfuscation solution, but it is built-in: Python has a compiler to byte-code:

python -OO -m py_compile <your program.py>

生成一个包含字节码的 .pyo 文件,其中文档字符串被删除等.您可以使用 .py 重命名 .pyo 文件 扩展名,并且 python 像您的程序一样运行,但不包含您的源代码.

produces a .pyo file that contains byte-code, and where docstrings are removed, etc. You can rename the .pyo file with a .py extension, and python <your program.py> runs like your program but does not contain your source code.

PS:您获得的有限"混淆级别可以恢复代码(使用一些变量名称,但没有注释和文档字符串).请参阅第一条评论,了解如何操作.但是,在某些情况下,这种混淆程度可能就足够了.

PS: the "limited" level of obfuscation that you get is such that one can recover the code (with some of the variable names, but without comments and docstrings). See the first comment, for how to do it. However, in some cases, this level of obfuscation might be deemed sufficient.

PPS:如果您的程序导入像这样混淆的模块,那么您需要使用 .pyc 后缀重命名它们(我不确定这不会有一天休息),或者您可以使用 .pyo 并使用 python -O ....pyo 运行它们(导入应该可以工作).这将允许 Python 找到您的模块(否则,Python 会查找 .py 模块).

PPS: If your program imports modules obfuscated like this, then you need to rename them with a .pyc suffix instead (I'm not sure this won't break one day), or you can work with the .pyo and run them with python -O ….pyo (the imports should work). This will allow Python to find your modules (otherwise, Python looks for .py modules).

这篇关于如何有效地混淆 Python 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆