Home / Docs / Encryption

Encryption

Understand the cryptographic foundations of PurePHPEncryptor. Learn how AES-256-GCM, ECDH key exchange, and ECDSA signatures protect your PHP source code.

Encryption Overview

PurePHPEncryptor uses industry-standard cryptographic algorithms to protect your source code. There's no "security through obscurity"—the security comes from well-vetted algorithms and proper key management.

Component Algorithm Purpose
File encryption AES-256-GCM Encrypts PHP source code
Key agreement ECDH P-256 Derives shared secret between license and encryptor
Key derivation HKDF-SHA256 Derives encryption keys from shared secret
License signing ECDSA P-256 Ensures license authenticity

How Encryption Works

1. Key Generation

When you generate a license, an ECDH key pair is created:

text
License Generation
├── Generate ECDH P-256 key pair
│   ├── Private key (stored in license file)
│   └── Public key (embedded in encrypted files)
└── Sign license with ECDSA P-256

2. File Encryption

Each PHP file is encrypted with a unique key derived from the license:

text
For each PHP file:
├── Derive file-specific key using HKDF
│   └── Input: shared secret + file path + salt
├── Compress source code (optional, with zlib)
├── Encrypt with AES-256-GCM
│   ├── 256-bit key
│   ├── 96-bit random nonce
│   └── 128-bit authentication tag
└── Encode as base64 and wrap in PHP loader

3. Runtime Decryption

When an encrypted file is executed, the runtime:

text
Runtime Decryption
├── Load and validate license file
│   ├── Verify ECDSA signature
│   ├── Check expiration date
│   └── Validate hardware constraints
├── Perform ECDH key agreement
├── Derive file-specific key
├── Decrypt with AES-256-GCM
│   └── Verify authentication tag (tamper detection)
├── Decompress if needed
└── Execute decrypted PHP code in memory

AES-256-GCM

AES-256-GCM (Galois/Counter Mode) is an authenticated encryption algorithm that provides both confidentiality and integrity:

Brute Force Resistance: AES-256 has 2^256 possible keys. Even if you could try one trillion keys per second, it would take longer than the age of the universe to try them all.

Tamper Protection

The authentication tag in AES-GCM ensures that any modification to the encrypted data is detected:

This means attackers cannot:

The Runtime

The runtime is a small PHP file (ppe-runtime.php) that handles license validation and decryption. It's automatically bundled with your encrypted files.

Runtime Features

Runtime Files

After encryption, your output directory contains:

text
dist/
├── ppe-runtime.php      # The decryption runtime
├── license.ppe          # Your license file (copy manually)
├── index.php            # Encrypted entry point
├── src/
│   ├── Controller.php   # Encrypted
│   ├── Model.php        # Encrypted
│   └── ...
└── vendor/              # Third-party code (not encrypted by default)

How the Runtime Loads

Each encrypted file includes a small loader that bootstraps the runtime:

php
<?php /* PPE:AES256GCM:v1 */
require_once __DIR__.'/ppe-runtime.php';
__ppe_exec('base64-encoded-encrypted-data...');

The __ppe_exec() function:

  1. Locates and validates the license file
  2. Derives the decryption key
  3. Decrypts the base64-encoded data
  4. Executes the decrypted PHP code using eval()
  5. Clears sensitive variables from memory

Performance Considerations

Decryption adds a small overhead to each file load. In practice, this is negligible:

Operation Typical Time Notes
License validation < 1ms Once per request
Key derivation < 0.5ms Cached after first file
File decryption 0.1-2ms Depends on file size

OPcache Compatibility: PHP's OPcache can cache the compiled bytecode of decrypted files, eliminating the decryption overhead on subsequent requests. Enable OPcache in production for best performance.

Security Considerations

What PurePHPEncryptor Protects Against

Limitations

Like all software protection, PurePHPEncryptor is not unbreakable:

Defense in Depth: Encryption is one layer of protection. Combine it with legal agreements, license tracking, and online validation for comprehensive protection.