Hi DEV! I'm Miguel, and I've spent the last months building Envelock: an offline encoder and a PHP extension that let you distribute PHP projects without shipping their files as readable text — an open-source (Apache-2.0) take on what ionCube and SourceGuardian do commercially, for PHP 8.2–8.5.

GitHub logo miguelenred / envelock

Envelock: offline encoder and loader that protects PHP source (PHP 8.2-8.5). Version 1.1.2.

Envelock logo

Envelock

Offline encoder and loader extension for PHP 8.2–8.5.
Version 1.1.2 · Python + C + OpenSSL 3 · Apache-2.0 license.
Project page: miguelenred.es/envelock · Source: github.com/miguelenred/envelock

Distribute PHP projects without shipping their files in readable text. A generic extension (with no embedded keys) verifies each file's signature, unwraps the content key with the recipient's private key, decrypts in memory and hands the result to PHP's normal compiler. The private key and the trusted signer are read from an external identity file pointed to by envelock.identity in php.ini. A single host can serve several projects from different folders, each encrypted with different keys.

It is not a compiler to machine code, nor an invulnerable system. Its aim is to keep PHP out of readable text and to stop the public engine, or another developer's keys, from decrypting packages meant for other recipients — subject to the cryptographic assumptions and…

If you sell or deploy PHP software onto servers you don't control, your realistic options have long been two proprietary products with closed loaders you have to trust, priced per server. I wanted the auditable version: a documented binary format, a loader anyone can build from public source, and modern cryptography instead of obfuscation alone. This post is about the decisions that took.

First decision: define what it is NOT

Before writing a line of crypto, I wrote the threat model. Envelock is not a compiler to machine code and not DRM against the server's own operator — someone with root can still read fragments of process memory. What it does do, within cryptographic assumptions and a correct implementation:

  • keep PHP out of readable text on disk and in transit;
  • stop the public engine, or another customer's keys, from decrypting packages meant for other recipients.

Everything else — every feature, every trade-off — flows from that honest scope. Saying it publicly in the README has cost me nothing and has prevented every "unbreakable DRM" argument before it starts.

The crypto, layer by layer

On the packaging machine (keys never leave it; private keys are scrypt-encrypted by default):

  • the source is encrypted with AES-256-GCM, a fresh content key per pack, unique nonces by counter;
  • the content key is wrapped per recipient with X25519 + HKDF-SHA256 — repeat --recipient and the same package is encrypted for several customers;
  • the whole package is signed with Ed25519.

On the server, a generic extension (C + OpenSSL 3, no embedded keys) does things in this strict order: verify the signature → unwrap the content key with the server's private key (from an identity file configured in php.ini) → decrypt in memory → hand the code to PHP's normal compiler. Nothing decrypted is ever written to disk, no external service is called, and explicit buffers are cleared after compiling.

Two details I'm fond of: the loader is generic — the same binary serves any identity, so one host can run several projects encrypted with different keys — and it fails closed: if the OPcache blacklist is missing or wrong, it refuses to run rather than cache decrypted opcodes.

What I learned

1. The real price of encrypted PHP is memory, not CPU. Protected files can't be cached by OPcache, so every request compiles the ones it uses into its own memory, in every PHP worker. Measured on a real Laravel app under PHP-FPM 8.4: protecting only the application code costs about 1 MB per request (the same as unprotected); protecting vendor/ too, 22 MB. The pack command now warns about encrypted vendor directories and states the measured memory ceiling (3–9× the original source) before you ship. Nobody talks about this, and it's the number one thing that decides whether encrypted PHP survives production.

2. Signatures protect you from attackers, not from yourself. An end-to-end audit of the loader, the crypto core and the encoder found no attacker-reachable defect — every finding sat behind the Ed25519 signature verified before any decrypt or deserialize — and still closed real defense-in-depth gaps: the bytecode deserializer now treats a signed image as hostile (it validates that every jump target lands on an aligned opline, rejects NULL interface/trait names, refuses stray pointers), a sealed package refuses to run if its eval() gate was replaced after module init, and the file nonce domain is kept disjoint from the vault-wrap domain. The audit made the format stricter without changing it: 1.1.x packages interoperate and nothing had to be repacked.

3. Fuzz the deserializer as if the signature didn't exist. It's fuzzed continuously under ASan/UBSan on every supported PHP version. The mindset: today the signature guards the image; the day someone finds a signature bypass, the deserializer is already the second line of defense, not an open door.

4. The supply chain is part of the product. Every released loader is attested (gh attestation verify <file> --repo miguelenred/envelock), release artifacts are hash-listed and enforced by script, and CI builds that use demonstration keys are marked DEMO_ONLY in the artifact itself. A protection tool whose own binary you can't trust is worth exactly nothing.

5. Platform honesty is a feature. Linux x86-64 and Windows x64 are built and tested natively across PHP 8.2–8.5 (including ZTS); macOS and ARM64 build and pass their suites in CI, but no prebuilt loader ships for them yet and the README says exactly that. Machine-bound licences are Linux-only. Users forgive "not yet" — they don't forgive surprises.

Try it

docker run ghcr.io/miguelenred/envelock 

Packaging a project is four commands (keys, recipient, project, pack) and there's a windowed assistant for the whole flow:

python encoder/envelock.py init-owner --out private/owner.enc.json --with-recovery python encoder/envelock.py init-recipient --out private/cliente.enc.json --label cliente-001 python encoder/envelock.py export-public --recipient private/cliente.enc.json --out release/cliente-001.pub.json python encoder/envelock.py pack --owner private/owner.enc.json --recipient release/cliente-001.pub.json --project <uuid> --source my-app --output release/cliente-001/my-app 

On each server: pie install envelock/loader, create the identity file, point envelock.identity at it from php.ini.

The honest limits, written down in the README and docs/SEGURIDAD.md: no online revocation yet, no TPM/DPAPI sealing of the server identity, control-flow flattening still unfinished, and PHAR/__halt_compiler() code has no guaranteed compatibility.

How I work, for transparency: I direct the project — architecture, spec, design decisions, review — and the implementation is produced with the help of several AI models under that spec and reviewed by me before shipping. Every trade-off above was decided by a human.

What would you need from a tool like this before shipping it to production — an independent audit, specific platform support, a feature I haven't thought of? Feedback is very welcome, and I'm happy to answer any question about the format or the crypto.
``