5种方法,加密你的Python代码 !

作者: 14e61d025165 | 来源:发表于2019-03-30 14:49 被阅读1次

Python越来越热门了,2019年3月TIOBE编程语言排行榜上,Python更是罕见的击败了“霸榜三巨头”之一的C++,挤进前三。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1553928520267" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);"></tt-image>

Python优点很多,比如简单易学,代码量少,能做的事很多等等,和其他语言一样,Pyhton也有一些不可掩盖的缺点,版本不兼容,运行效率不高等等。

其中一个缺点,让不少开发者头疼不已,由于Python解释器开源的关系,导致Python代码无法加密,代码的安全性得不到保障。

当然,想要加密Python代码,也并非无解。最常见的加密方式有4种,还有1种独特的加密方式。

1Python学习群:683380553,有大牛答疑,有资源共享!是一个非常不错的交流基地!欢迎喜欢Python的小伙伴!

Python解释器在执行代码的过程中,会首先生成.pyc文件,然后再解释执行.pyc中的内容,当然,解释器也能直接执行.pyc文件。

.pyc文件是一个二进制的文件,是不具备可读性的。

假如我们发到客户环境时,是.pyc文件,而不是.py,那么是不是就可以保护我们的Python代码?

想要做到这一点,并不难。Python标准库就提供了一个名叫compileall的库,使用它就可以做到。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1553928520271" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);"></tt-image>

执行如下命令,即可将目录下的所有.py文件编译成.pyc文件:

python -m compileall 然后删除 目录下所有 .py 文件就可以了。

$ find -name '*.py' -type f -print -exec rm {} ;

这一方法,可以加密我们的Python代码,胜过代码裸在外面。尴尬的是,因为Python解释器的兼容较差,有些版本并不能运行.pyc文件。而且已经有现成的反编译工具,可以直接破解。

比如python-uncompyle6(「链接」),只要执行以下命令,就可以搞定。

$ uncompyle6 compiled-python-file-pyc-or-pyo

2

代码混淆,也是一种常见的“加密”方式,严格意义上说,这一方法并不是加密,而是上代码的可读性变差。比如删除注释,添加毫无意义的注释,添加无效代码,对变量、函数、类进行重命名等。

内容不可读,代码就受到了保护。

代码混淆的工具很多,一个比较好用的混淆库是pyobfuscate(GitHub - astrand/pyobfuscate: pyobfuscate)。这个库可以对类、函数进行重命名,并且插入无关的代码,甚至自动加空格等等。

这一方法很简单,也提高了破解的门槛。但由于代码结构未发生变化,字节码也能获取,破解难度也不高。

一般而言,使用这一方式较为简单,实用。

3

如果有一款工具,可以将Python脚本打包成在某一平台的可执行文件,最终我们发行的,是一份打包完成的二进制文件,那么程序就更难被破解了?

py2exe(FrontPage - py2exe.orgFrontPage - py2exe.org)就是一款很好的打包工具,可以将Python脚本打包成可在Windows上运行的文件。

这一方式的优点是进一步提高了破解门槛。遗憾的是,你只能在windows上运行它。

4

Python运行速度慢何解?用Cython就可以带来性能的提升。实际上,Cython也可以用来加密Python代码。

Cython的原理是,将.py编译为.c文件,再将.c文件编译为.so或者.pyd,这样一来,文件就变得难以破解了。

这样做的好处是,Python代码很难被破解,缺点是有时候,Cython可能不支持一小部分代码,完善起来就比较麻烦了。

5

最后一种方法,做得比较绝。

由于Python是解释型语言,因此在发行Python程序的时候,就必须包含一个Python解释器,如果我们修改这个解释器,代码不就被保护起来了吗?

如果我们能对最原始的Python代码进行加密,加密后的代码被发行后。哪怕被别人看到了,但因为不晓得算法是怎样的,就破解不了了。

这是因为Python解释器本身是一个二进制文件,自然也就无法获得关键性的数据,进而保护了源码。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1553928520274" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);"></tt-image>

虽然这一方法最为安全,可操作难度较高。你必须掌握基本的加解密算法,还要探究Python执行代码的方式,从而了解到从什么地方进行加解密。最后禁用字节码,以防通过.pyc反编译即可。

以上五种加密方式,有利有弊,有难有易,根据需求选择就可以了。

相关文章

  • 5种方法,加密你的Python代码 !

    Python越来越热门了,2019年3月TIOBE编程语言排行榜上,Python更是罕见的击败了“霸榜三巨头”之一...

  • 5种方法,加密你的Python代码 !

    Python越来越热门了,2019年3月TIOBE编程语言排行榜上,Python更是罕见的击败了“霸榜三巨头”之一...

  • Idea python运行环境准备

    1.语言比较 2.python代码加密 python代码无法加密,发布代码即为源码 3.版本问题 python 2...

  • 代码片段--加密

    MD5加密代码 SHA-1加密代码 测试

  • iOS代码加密常用加密方式

    iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密、AES加密、BASE64加密,三大...

  • iOS常用加密方式

    iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密、AES加密、BASE64加密,三大...

  • python 加密代码

    1.compileall Python解释器在执行代码的过程中,会首先生成.pyc文件,然后再解释执行.pyc中的...

  • iOS加密

    iOS代码常见的加密方式包括MD5加密、AES加密、BASE64加密,RSA加密。 MD5加密 MD5是不可逆的只...

  • python hashlib md5加密

    MD5模块在python3被移除,所以使用hashlib进行MD5加密 python2 使用MD5模块进行加密

  • Python3中的md5加密

    Python3中的md5加密

网友评论

    本文标题:5种方法,加密你的Python代码 !

    本文链接:https://www.haomeiwen.com/subject/fcajbqtx.html