How to Hide/Encrypt/Obfuscate any Python Program

Balakrishnakumar V
Towards Data Science
4 min readJan 29, 2021

--

Photo by John Salvino on Unsplash

For the purpose of obfuscation, we will be using a python package called pyarmor.

We might sometimes face a situation where we need to provide code directly to a client for obvious reasons, but by doing so, we will lose control of the code. In such cases, we might we encrypt the codes to protect it, retain control and add some fallback condition to control our dependency, just like if we provided code for using only for a certain amount of time.

To address the above issues, I will be demonstrating a simple function with the above capabilities. We will be making use of the python library called “pyarmor”.

Table of Contents:

  1. Creating a simple function
  2. Using pyarmor to encrypt it
  3. Importing our function / Inference

1. Creating a simple function

For our implementation, I will be using the utils.py with inference function definition and will be encrypting it later.

The inference function is pretty straight forward and doesn’t need any explanation.

Save it in a folder.

2. Using pyarmor to encrypt it

Now we will encrypt it, by running the following 2 commands.

pip3 install pyarmor # pip/pip3# Syntax : pyarmor obfuscate --restrict=0 <filename>pyarmor obfuscate --restrict=0 utils.py
1. Install the package
2. Encrypt the utils.py function

Now if you see the folder containing our actual utils.py file, a new subfolder will be created called dist.

on the left utils.py (original file) and on the right inside the dist folder, we have the (encrypted) utils.py file

Let’s see the contents inside them.

Encrypted utils.py file inside dist subfolder

3. Importing our function / Inference

Once we are done, until this point, now let’s try to import this encrypted utils.py in a new python file called main.py, which is created inside the dist folder.

The necessary keys for decrypting the utils.py at run-time is taken care of by pyarmor and it's present in the pytransform folder, thus making our code completely non-readable to other's eyes.

But if you wish to modify the original utils.py code, then you have to start from step 1 and follow the same steps to do so.

Contents inside main.py file,

# Import the inference function definition inside the utils.py filefrom utils import inference_ = inference(name="Balakrishna")

And upon running the above line, we get the output based on our utils.py file’s configuration.

The final output.

Conclusion:

This is merely the beginning. If you want to really protect and sell your code, consider adding an actual license with an embedded virtual date on it. Upon passing the date, it will no longer run the program and throws a license expiry error. Luckily pyarmor has that too. I have added the license-related documentation in the references below. You can even set up a provision to email automatically once the license is expired and you can get in touch with your clients using the SMTP services in python.

Also if you have a lot of .py files to import or as dependency, you must use the — recursive command line instead of — restrict command for your code to work.

Also, this implementation can be done in both Windows and Ubuntu-based OS.

References:

Complete Code Implementation is available at,

@ GitHub

pyarmor:

  1. https://pypi.org/project/pyarmor/
  2. https://github.com/dashingsoft/pyarmor
  3. https://pyarmor.readthedocs.io/en/latest/
  4. https://pyarmor.readthedocs.io/en/latest/usage.html#generating-license-for-obfuscated-scripts
  5. https://docs.python.org/3/library/smtplib.html

--

--