# Python Encrypt Code [![Python 3.12+](https://img.shields.io/badge/python-3.12+-blue.svg)](https://www.python.org/downloads/) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE) > _A tool that integrates seamlessly with modern CI and Dockerized edge deployments._ A powerful Python CLI tool for encrypting and decrypting Python modules, enabling secure distribution and execution of Python code. Perfect for protecting intellectual property, distributing commercial Python applications, or creating secure deployment packages. > ⚠️ **SECURITY DISCLAIMER**: This tool is currently in development and **not ready for production use**. In its current version, the tool writes decrypted files to temporary directories on disk during execution, which poses a security risk as sensitive code may remain accessible in the filesystem. This security vulnerability will be addressed in a future version by implementing in-memory execution. Use at your own risk for development and testing purposes only. ## Features - 🔐 **Strong Encryption**: AES-256 encryption using the `cryptography` library - 📦 **Module Packaging**: Compress and encrypt entire Python packages into a single encrypted file - 🚀 **Direct Execution**: Run encrypted Python modules without extracting to disk (coming in a later version) - 🔑 **Secure Password Generation**: Built-in cryptographically secure password generator - 🛠️ **Developer Friendly**: Full type annotations and comprehensive test coverage - 📁 **Preserve Structure**: Maintains package hierarchy and imports when decrypting ## Installation ### Using uv (recommended) ```bash git clone https://gitea.gt-proj.com/brian/python-encrypt-code.git cd python-encrypt-code uv install ``` ### Using pip ```bash git clone https://gitea.gt-proj.com/brian/python-encrypt-code.git cd python-encrypt-code pip install -e . ``` ## Quick Start ### 1. Generate a Secure Password ```bash uv run python-encrypt-code generate-password # Output: Wlg2aFQ2dWhkX0U0TkxOcXBHY09acTR1ZVo5UlNPcWxSamVxOTI5a08tdz0= ``` ### 2. Encrypt a Python Module ```bash export MODULE_PATH=./src/my_package export ENCRYPTED_FILE=my_package.pec export PASSWORD=your-secure-password uv run python-encrypt-code encrypt ${MODULE_PATH} -o ${ENCRYPTED_FILE} -p ${PASSWORD} ``` ### 3. Decrypt an Encrypted File ```bash uv run python-encrypt-code decrypt ${ENCRYPTED_FILE} -o ./decrypted -p ${PASSWORD} ``` ### 4. Run Encrypted Module Directly ```bash uv run python-encrypt-code run-insecure ${ENCRYPTED_FILE} --script main.py ``` ## Commands ### `generate-password` Generate a cryptographically secure password for encryption. ```bash uv run python-encrypt-code generate-password ``` ### `encrypt` Encrypt a Python module or package directory. ```bash uv run python-encrypt-code encrypt -o -p [-aad ] ``` **Options:** - `source_path`: Directory containing Python files to encrypt - `-o, --output`: Output path for encrypted file - `-p, --password`: Password for encryption - `-aad, --additional-authenticated-data`: JSON-formatted string to embed metadata in the encrypted file ### `decrypt` Decrypt a file and extract contents to disk. ```bash uv run python-encrypt-code decrypt -o ``` **Options:** - `source`: Path to encrypted file - `-o, --output`: Directory to extract decrypted files ### `run-insecure` Decrypt and execute a Python script from an encrypted package. **N.B. this writes decrypted files to a temporary folder on the disk before executing!** ```bash uv run python-encrypt-code run-insecure --script