cython混淆加密

    python代码是一种解释型的语言,有了代码和环境就可以执行,它无需编译。如果需要对代码进行混淆,可以借助cython这个库。它的安装很简单,直接运行pip install cython就可以安装。

cython混淆加密_第1张图片

    进行代码混淆之前,还需要一个环境,就是msvc,其实就是visual studio 2017环境安装。

    使用cython其实最麻烦的在于vc环境的安装,有了这一步,后面才可以把py文件生成pyd文件,达到加密的目的。

    可以准备一个简单的py文件:

    hallo.py 

def hello(name):
    return "hello,%s" % name


def add(a, b):
    return a + b

    在hallo.py同目录下新建一个setup.py文件

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("hallo.py"))

    接着就可以执行如下命令:

python setup.py build_ext

    不出意外,运行如下:

C:\Users\buejee\PycharmProjects\pythonProject>python setup.py build_ext
Compiling hallo.py because it changed.
[1/1] Cythonizing hallo.py
C:\Users\buejee\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\Cython\Compiler\Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: C:\Users\buejee\PycharmProjects\pythonProject\hallo.py
  tree = Parsing.p_module(s, pxd, full_module_name)
running build_ext
building 'hallo' extension
"D:\tools\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD "-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\include" "-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\Include" "-ID:\tools\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-ID:\Windows Kits\10\include\10.0.14393.0\ucrt" "-ID:\Windows Kits\10\include\10.0.14393.0\shared" "-ID:\Windows Kits\10\include\10.0.14393.0\um" "-ID:\Windows Kits\10\include\10.0.14393.0\winrt" "-ID:\Windows Kits\10\include\10.0.14393.0\cppwinrt" /Tchallo.c /Fobuild\temp.win-amd64-cpython-310\Release\hallo.obj
hallo.c
"D:\tools\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\link.exe" /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO "/LIBPATH:C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\libs" "/LIBPATH:C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0" "/LIBPATH:C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\PCbuild\amd64" "/LIBPATH:D:\tools\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\lib\um\x64" "/LIBPATH:D:\Windows Kits\10\lib\10.0.14393.0\ucrt\x64" "/LIBPATH:D:\Windows Kits\10\lib\10.0.14393.0\um\x64" /EXPORT:PyInit_hallo build\temp.win-amd64-cpython-310\Release\hallo.obj /OUT:build\lib.win-amd64-cpython-310\hallo.cp310-win_amd64.pyd /IMPLIB:build\temp.win-amd64-cpython-310\Release\hallo.cp310-win_amd64.lib
  正在创建库 build\temp.win-amd64-cpython-310\Release\hallo.cp310-win_amd64.lib 和对象 build\temp.win-amd64-cpython-310\Release\hallo.cp310-win_amd64.exp
正在生成代码
已完成代码的生成

    构成成功,生成的pyd文件在build目录下:

cython混淆加密_第2张图片 

    这个文件虽然名字叫hallo.cp310-win_amd64.pyd,但是你可以像hallo.py文件一样调用。

 cython混淆加密_第3张图片

     而这个pyd文件就是经过加密之后的文件,类似dll,可以反汇编,但是不能反编译。

你可能感兴趣的:(python,python,cython,setup,extension,混淆加密)