I want to encrypt a file in PHP serverside and decrypt it on the client using javascript.
So far I have been able to do this in PHP:
ENCRYPT
function encryptFile($path, $key)
{
$iv = openssl_random_pseudo_bytes(16);
$encrypted = openssl_encrypt(file_get_contents($path), 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
return base64_encode($iv . $encrypted);
}
public function downloadEncryptedFile()
{
$key = 'fee-fi-fum';
$path = storage_path('app/public/nicesong.mp3');
$encrypted = encryptFile($path, $key);
return response($encrypted, 200, [
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="your-file.encrypted"'
]);
}
The server endpoint calls the downloadEncryptedFile() method which in turn returns a response with an application/octet-stream Content-Type header.
How do I implement decryption on the client side.