15 Jan 2023
Using Pyarmor to Obfuscate your Python Application
Pyarmor is a python library that allows your code to be obfuscated. I will provide a general guide on how to use Pyarmor to obfuscate a Django application.
Integrating Pyarmor with your code.
- Install pyarmor with pip.
- Create a
dist
folder. The dist
folder will be our main working directory. - Copy Django application’s source folder to the
dist
folder. - Go to the folder or code you wish to obfuscate in the
src
folder. -
Run the following command to obfuscate the code.
pyarmor obfuscate --src="." -r --output=../../dist/apps/ __init__.py
- Open the output files and you will see that the python files are now obfuscated.
Integration with Cryptography
One of the things that you can also combine with Pyarmor is the usage of the Cryptography module which allows you to encrypt a license key in which you can integrate this into your codebase.
- Create an unecrypted license file which can contain e.g. expiry date.
- Use the cryptography module to encrypt the license file using a user defined key.
- Inside your application, integrate or store the user defined key used to encrypt the license file in a variable.
- Create function that will read the encrypted file and decrypt it using the user defined key.
- Obfuscate the python code using pyarmor to protect the user defined key.
Refer to the following repository for more details.