Obfuscate Python Code

Obfuscate Python Code

  1. Use Base64 Encoding to Obfuscate Python Code
  2. Use PyArmor to Obfuscate Python Code

Code obfuscation means encrypting or converting the code into a format that is difficult to understand by humans. We do this task knowingly to avoid an attacker’s reverse engineering and protect intellectual property and trade secrets.

Python offers multiple ways to obfuscate code.

Use Base64 Encoding to Obfuscate Python Code

The base64 is a module in Python that encodes and decodes data once the data is transformed into byte-like objects. Encoding Python’s code can be a helpful trick to obfuscate the code to prevent it from being read and understood by humans for security.

In the following code, we have imported the base64 module and encoded the statement print('Delftstack') using the method b64encode(). We must pass the string in the UTF-8 format to the method.

We can also decrypt the code using the method b64.decode() by passing the encoded text to this method. Finally, we can run the decrypted code using the eval() and compile() methods.

Example Code:

#Python 3.x
import base64
code = "print('Delftstack')"
print('Code:', code)
encrypted_code = base64.b64encode(code.encode('utf-8'))
print("Obfuscated code:", encrypted_code)
mydecode = base64.b64decode(encrypted_code)
print('Decrypted Code:')
eval(compile(mydecode,'<string>','exec'))

Output:

#Python 3.x
Code: print('Delftstack')
Obfuscated code: b'cHJpbnQoJ0RlbGZ0c3RhY2snKQ=='
Decrypted Code:
Delftstack

We can also import a complete .py file containing the code and encode it using the base64 technique in the following way. The My_Script.py file contains the code print('hello world').

#Python 3.x
import base64
file = open('My_Script.py')
encrypted_code = base64.b64encode(file.read().encode('utf-8'))
print('Encrypted Code:', encrypted_code)

Output:

#Python 3.x
Encrypted Code: b'cHJpbnQoJ2hlbGxvIHdvcmxkJyk='

Use PyArmor to Obfuscate Python Code

Python programs can also be encrypted with PyArmor. It is a command-line tool that obfuscates code, binds obfuscated scripts to fixed machines, and can even expire obfuscated scripts.

To use the PyArmor, we should install it first using the following command.

#Python 3.x
pip install pyarmor

Here, we will obfuscate the following code in a file named MyScript.py. We have to put the .py file inside a separate folder.

#Python 3.x
print('Hello World')

The command pyarmor obfuscate MyFolder/MyScript.py will obfuscate the above .py file. We can see the output file inside the dist folder with the same file name.

Output:

#Python 3.x
from pytransform import pyarmor_runtime
pyarmor_runtime()
__pyarmor__(__name__, __file__, b'\x50\x59\x41\x52\x4d\x4f\x52\x00\x00\x03\x06\x00\x33\x0d\x0d\x0a\x09\x30\xe0\x02\x00\x00\x00\x00\x01\x00\x00\x00\x40\x00\x00\x00\xc2\x00\x00\x00\x00\x00\x00\x18\x58\xc1\xa9\xb3\xd3\xa5\x4a\x59\xc7\xef\x90\xbd\x03\x26\xa0\x44\x00\x00\x00\x00\x00\x00\x00\x00\x75\x29\x30\x1b\x83\xa8\x1a\x8c\x19\x5d\xf5\xfb\x82\xd2\xde\xf9\x3e\x55\x2a\x65\x05\x6c\xd5\x40\xf1\x45\xaf\x5a\x5e\xce\x5b\x94\xe6\xb5\x2d\xbf\x91\x08\x47\x5a\x41\x2a\x8e\x86\x28\x08\x76\x35\x77\xbf\x3f\x52\x28\x03\xbb\xcf\xd4\xac\xa5\x1e\x8a\x4d\xfe\xc4\x48\x04\xb7\x8f\xfb\x10\xa7\x25\x05\x97\x80\xfa\x74\x96\x4b\x61\x88\x8f\x98\x96\x1e\xa5\x3d\x98\x2e\xe0\x1e\x19\xa2\x15\x94\x8b\xc2\x5c\x2a\x1e\x2c\x6f\x46\x28\x08\x85\x3f\x0d\x8a\xd8\xb5\x9c\xcc\xee\x8e\xff\x6f\x31\xb3\x02\x53\xbd\x88\x4a\x98\x84\x61\xd5\xe9\xea\x66\x75\x69\x1c\xd2\x5d\x47\x5e\x5b\xc8\x2d\x5f\x01\x74\xaa\xf6\x3b\xfc\xd0\x9f\xfd\x9c\x27\x35\x2b\xbe\x41\xa6\xc1\x88\x79\xfc\xb3\xe8\xa7\x65\x19\xed\x8d\x85\xb1\x07\x35\x96\x4d\xea\x32\x71\xba\x63\xb0\x11\xaf\x31\x05\xe9\xa0\xba\xaa\x77\x96\x43\xeb\x4d', 2)
Author: Fariba Laiq
Fariba Laiq avatar Fariba Laiq avatar

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.

LinkedIn