base64_encode($encryptedWithTag), 'iv' => base64_encode($iv), ]; } /** * Decrypt ciphertext using AES-256-GCM. */ public function decrypt(string $encryptedBase64, string $key, string $ivBase64): string { $encryptedWithTag = base64_decode($encryptedBase64); $iv = base64_decode($ivBase64); $tagLength = 16; $encrypted = substr($encryptedWithTag, 0, -$tagLength); $tag = substr($encryptedWithTag, -$tagLength); $decrypted = openssl_decrypt( $encrypted, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag ); if ($decrypted === false) { throw new \RuntimeException('Decryption failed'); } return $decrypted; } }