Hides Python code in a C++ binary. The Python code is encrypted using AES (Advanced Encryption Standard). It offers some form of reverse engineering protection.
Sometimes you want to hide your precious Python code when distributing your program. But the interpreted Python language does not allow to "hide" your code.
Compiled Python files (.pyc/.pyo) files do show parts of the Python code in plain text and can be easily decompiled.
- Python 3.4+
- Python Development headers (
python3-dev
package) - CMake 3.14+
- C++ compiler such as GNU G++
- C++ AES implementation from https://github.com/SergeyBel/AES
- Python Obfuscator (see below)
- initialize git submodule:
git submodule init && git submodule update --recursive
- cmake initialization
mkdir build && pushd build && cmake ..
- compile:
make clean all
- optionally, obfuscate your code
- encrypt Python code:
./gendatah KEY PYTHONFILE.py
- KEY must have a length of 16 characters!
- rebuild:
make clean all
- run:
./runner
After each change to your PYTHONFILE.py
you have to do:
./gendatah KEY PYTHONFILE.py
generates newdata.h
make clean all
recompilesrunner
binary; make sure to also do amake clean
!
This PythonHiderCpp consists of the two command line interface (CLI) C++ programs gendatah.cpp
and runner.cpp
which have to compiled to binaries.
-
gendatah.cpp
CLI program- AES encryptes Python file with given key (using AES EBC mode)
- generates the
data.h
header which includes- the key (as a C++ macro)
- the encrypted data
-
runner.cpp
CLI program- The encrypted data (by
gendatah
is compiled into this binary. - Attention: this private key is also stored in the binary! It is somewhat hidden, not visible as plain text, but can be retrieved, e.g., memory analysis.
- It uses the Python/C API and its high level layer to run the Python code. Specifically it uses PyRun_SimpleString to run the Python code from an in-memory string.
- The encrypted data (by
The private key is also stored inside the compiled binary. This is not safe because the key could be found! PythonHiderCpp does make some efforts to hide the key, but nonetheless it can be found.
- C++ AES implementation by Sergey Bel, https://github.com/SergeyBel/AES, MIT license
There are otehr solutions which do compile Python code to binaries, but most of them first extract the code to a temporary folder where it is visible again.
- py2app
- cx_freeze
- pyinstaller
- pyminifier, https://github.com/liftoff/pyminifier, does work without obfuscation, works just with minifying (stripping comments etc.).
- Emojify, https://github.com/chris-rands/emojify, does work, but beware: the comments are not stripped!
These did not work:
- PyArmor, https://github.com/dashingsoft/pyarmor, does not work because of needed runtime package
- PyObfx, https://github.com/PyObfx/PyObfx, did not work (
SyntaxError: invalid syntax
). - Opy, https://github.com/BuvinJT/Opy, did not work (
NameError: name 'l11l11l_opy_' is not defined
). - Python Code Obfuscator, https://github.com/brandonasuncion/Python-Code-Obfuscator,
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Copyright (C) 2020 by Ixtalo. AGPL3 license.