<?php
$key = "test_key_1234567890123456789012";
$b64 = "dGhpU2sqKyI3RLC8J3xF3I/HSex1XS5qPUrIL8xjeaDr/kYiqUxEG5IC18aVVUN+r7L4JnPSoTh+HYdZBovmpD9hVQ5vKe1BsFNHHy3yNNEHBg==";

$raw = base64_decode($b64, true);
echo "raw_len=" . strlen($raw) . "\n";
$nonce = substr($raw, 0, 12);
$tag = substr($raw, -16);
$ct = substr($raw, 12, -16);
echo "nonce_hex=" . bin2hex($nonce) . "\n";
echo "tag_hex=" . bin2hex($tag) . "\n";
echo "ct_len=" . strlen($ct) . "\n";

$aes_key = '';
for ($i = 0; $i < 32; $i++) {
    $aes_key .= chr(ord($key[$i % strlen($key)]) ^ (($i * 7) & 0xFF));
}
echo "key_hex=" . bin2hex($aes_key) . "\n";

$result = openssl_decrypt($ct, 'aes-256-gcm', $aes_key, OPENSSL_RAW_DATA, $nonce, $tag);
echo "result=" . ($result !== false ? "OK:" . substr($result, 0, 50) : "FAIL: " . openssl_error_string()) . "\n";
