File Encryptor — PHP source
Encrypt or decrypt any file with AES-256-GCM in your browser. Password-based, zero server contact. Drag, drop, done.
This is the PHP implementation — the same logic the interactive tool runs, in a shareable, citable form.
<?php
/**
* file-encryptor — password-based AES-256-GCM file encryption.
*
* Language: PHP 7.4+ (bundled OpenSSL extension — standard in every PHP build).
* CosmoDev polyglot showcase port of the `file-encryptor` tool, ported from
* src/lib/file-encryptor.ts (the canonical TypeScript implementation).
*
* PHP strings are raw byte buffers, and the bundled OpenSSL extension supplies
* everything the TS lib gets from Web Crypto: openssl_pbkdf2 for the 100k
* key stretch, openssl_encrypt/openssl_decrypt in aes-256-gcm mode for the
* AEAD. Same wire format, byte-for-byte interchangeable with the original.
*
* display source — part of CosmoDev's polyglot tool pages.
*/
declare(strict_types=1);
const SALT_LENGTH = 16;
const IV_LENGTH = 12;
const ITERATIONS = 100000;
// GCM appends a 16-byte auth tag to the ciphertext; the smallest possible
// encrypted payload is therefore salt + IV + tag = 44 bytes.
const TAG_LENGTH = 16;
const MIN_LENGTH = SALT_LENGTH + IV_LENGTH + TAG_LENGTH;
/**
* PBKDF2-SHA256 (100k iterations) over the raw password bytes -> the 32-byte
* AES-256 key. openssl_encrypt keys are raw binary, so no further encoding.
*/
function fe_derive_key(string $password, string $salt): string
{
return openssl_pbkdf2($password, $salt, 32, ITERATIONS, 'sha256');
}
/**
* Encrypt $data (raw bytes) under $password.
* Returns salt || IV || ciphertext+tag as a raw binary string.
*
* @throws InvalidArgumentException on empty password or empty input.
*/
function fe_encrypt_file(string $data, string $password): string
{
if ($password === '') {
throw new InvalidArgumentException('Password must not be empty.');
}
if ($data === '') {
throw new InvalidArgumentException('Input data is empty - nothing to encrypt.');
}
$salt = random_bytes(SALT_LENGTH);
$iv = random_bytes(IV_LENGTH);
$key = fe_derive_key($password, $salt);
// $tag is filled by reference: the 16-byte GCM auth tag that Web Crypto
// appends to the ciphertext. OPENSSL_RAW_DATA = no base64 wrapping.
$ciphertext = openssl_encrypt($data, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag);
if ($ciphertext === false) {
throw new RuntimeException('Encryption failed.');
}
return $salt . $iv . $ciphertext . $tag;
}
/**
* Decrypt a payload produced by fe_encrypt_file(). Throws when the password
* is wrong or the payload was corrupted/tampered (GCM auth-tag failure).
*
* @throws InvalidArgumentException on empty password or too-short input.
* @throws RuntimeException when authentication fails.
*/
function fe_decrypt_file(string $data, string $password): string
{
if ($password === '') {
throw new InvalidArgumentException('Password must not be empty.');
}
if (strlen($data) < MIN_LENGTH) {
throw new InvalidArgumentException(sprintf(
'Input is too short to be an encrypted file (needs at least %d bytes: salt + IV + auth tag).',
MIN_LENGTH
));
}
$salt = substr($data, 0, SALT_LENGTH);
$iv = substr($data, SALT_LENGTH, IV_LENGTH);
$body = substr($data, SALT_LENGTH + IV_LENGTH);
$tag = substr($body, -TAG_LENGTH);
$ciphertext = substr($body, 0, -TAG_LENGTH);
$key = fe_derive_key($password, $salt);
// Passing the tag makes openssl_decrypt verify it; on mismatch (wrong
// password or modified payload) it returns false.
$plaintext = openssl_decrypt($ciphertext, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag);
if ($plaintext === false) {
throw new RuntimeException('Decryption failed: wrong password or corrupted file.');
}
return $plaintext;
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →