Age File Encryption — PHP source
Encrypt and decrypt files with age — a modern, simple alternative to PGP. Password-based encryption runs entirely in your browser.
This is the PHP implementation — the same logic the interactive tool runs, in a shareable, citable form.
<?php
/**
* age-encryption — passphrase-based file encryption in the spirit of the
* age format (age-encryption.org/v1).
*
* Language: PHP 7.4+ (standard library incl. the bundled OpenSSL extension).
* CosmoDev polyglot showcase port of the `age-encryption` tool, ported from
* src/lib/age-encryption.ts (the canonical TypeScript implementation).
*
* Why this file exists: PHP's openssl_encrypt already speaks AES-256-GCM and
* hash_pbkdf2 already speaks PBKDF2-SHA256, so this port delivers the same
* security properties as the TS reference — 100k-iteration key stretch, a
* fresh random salt and IV per encryption, authenticated encryption — and its
* output is byte-for-byte interchangeable with it.
*
* Note on tag placement: Web Crypto appends the GCM tag to the ciphertext;
* openssl_encrypt hands it back separately, so this port appends it
* explicitly — the layout matches the TS implementation exactly.
*
* display source — part of CosmoDev's polyglot tool pages.
*/
declare(strict_types=1);
const AGE_HEADER = 'cosmodev-age-v1';
const SALT_LENGTH = 16;
const IV_LENGTH = 12;
const ITERATIONS = 100000;
const HEADER_LENGTH = 15; // strlen(AGE_HEADER)
// GCM appends a 16-byte auth tag to the ciphertext; the smallest possible
// encrypted payload is therefore header + salt + IV + tag = 59 bytes.
const TAG_LENGTH = 16;
const OVERHEAD = HEADER_LENGTH + SALT_LENGTH + IV_LENGTH + TAG_LENGTH;
/**
* True when $data starts with the cosmodev-age-v1 magic header.
*/
function isAgeEncrypted(string $data): bool
{
return strlen($data) >= HEADER_LENGTH
&& substr($data, 0, HEADER_LENGTH) === AGE_HEADER;
}
/**
* PBKDF2-SHA256 (100k iterations) -> 256-bit AES-GCM key (raw bytes).
*/
function deriveKey(string $passphrase, string $salt): string
{
return hash_pbkdf2('sha256', $passphrase, $salt, ITERATIONS, 32, true);
}
/**
* Encrypt $data under $passphrase.
* Returns header || salt || IV || ciphertext + tag.
*
* @throws InvalidArgumentException on an empty passphrase or empty input.
* @throws RuntimeException when OpenSSL refuses to encrypt.
*/
function ageEncrypt(string $data, string $passphrase): string
{
if ($passphrase === '') {
throw new InvalidArgumentException('Passphrase 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 = deriveKey($passphrase, $salt);
$tag = '';
$ciphertext = openssl_encrypt($data, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag);
if ($ciphertext === false) {
throw new RuntimeException('Encryption failed.');
}
return AGE_HEADER . $salt . $iv . $ciphertext . $tag;
}
/**
* Decrypt a payload produced by ageEncrypt(). Throws when the data lacks the
* cosmodev-age-v1 header, the passphrase is wrong, or the payload was
* corrupted/tampered (GCM auth-tag failure).
*
* @throws InvalidArgumentException on empty passphrase, missing header, or
* a payload too short to carry header + salt + IV + tag.
* @throws RuntimeException on GCM tag failure (wrong passphrase or tamper).
*/
function ageDecrypt(string $data, string $passphrase): string
{
if ($passphrase === '') {
throw new InvalidArgumentException('Passphrase must not be empty.');
}
if (!isAgeEncrypted($data)) {
throw new InvalidArgumentException(
'Not an age-encrypted file (missing cosmodev-age-v1 header).'
);
}
if (strlen($data) < OVERHEAD) {
throw new InvalidArgumentException(sprintf(
'Input is too short to be an age-encrypted file (needs at least %d bytes: header + salt + IV + auth tag).',
OVERHEAD
));
}
$salt = substr($data, HEADER_LENGTH, SALT_LENGTH);
$iv = substr($data, HEADER_LENGTH + SALT_LENGTH, IV_LENGTH);
$body = substr($data, HEADER_LENGTH + SALT_LENGTH + IV_LENGTH);
$tag = substr($body, -TAG_LENGTH);
$ciphertext = substr($body, 0, -TAG_LENGTH);
$key = deriveKey($passphrase, $salt);
// openssl_decrypt returns false when the GCM auth tag fails — the key did
// not match (wrong passphrase) or the payload was modified after
// encryption.
$plain = openssl_decrypt($ciphertext, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag);
if ($plain === false) {
throw new RuntimeException('Decryption failed: wrong passphrase or corrupted file.');
}
return $plain;
}
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 →