Skip to main content

Encrypt Python source files and decrypt them on the fly during runtime

Project description

sourceprotected

Encrypt Python source files and decrypt them on the fly during runtime.

Python versions Code style: Black Ruff

Usage

Installation

Using pip:

pip install sourceprotected

Encrypting the source files

sourceprotected path/to/src(.py)
Using key: fHC79...
Using encryption time: 1694...

Decrypt during runtime

With sourceprotected installed in your current environment, you can run/import encrypted files/modules as long as the SOURCEPROTECTED_KEY environment variable is set:

encrypted.py:

# -*- coding: sourceprotected -*-

-----BEGIN SOURCEPROTECTED FILE-----
gAAAAABk-eJaJarzMWy70GTRkRi6kjaD-zJy
-fHX_0G5VHTZjEKxJUvFhVWfWpX_j3LydmJo
pn7YrAj5CSqqPWgxUY_dSk5ELA==
-----END SOURCEPROTECTED FILE-----

main.py:

import encrypted  # Will decrypt encrypted.py on the fly.

Finally:

SOURCEPROTECTED_KEY=Bk92p... python -B main.py

How does it work?

sourceprotected uses a custom codec that will decrypt the file content using the SOURCEPROTECTED_KEY environment variable.

This codec is registered on startup by using a .pth file.

Credits goes to https://github.com/asottile-archive/future-fstrings for the implementation idea.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sourceprotected-0.1.0.tar.gz (6.0 kB view hashes)

Uploaded Source

Built Distribution

sourceprotected-0.1.0-py3-none-any.whl (6.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page