dongtang3155 2019-04-06 11:35
浏览 168

是否可以使用php-openssl加密图像文件?

I'm making a web server which can upload and download all kind of file with php.(server os is windows) And when file is uploaded and downloaded, it is encrypted and decrypted using AES. I had searched about it, and I have made it. But it works only .pdf, .csv, .txt, .pptx file.

When I try to open .jpg, .png, .zip, .xlsx file which is decrypted, it doesn't open properly. I cannot see the image.

// File encryption code
$filename = $_FILES['user_file']["name"];
$key = '1234';
$iv = '12dasdq3g5b2434b';
$file = fopen('filePath', 'rb');
$fileData = fread($file, filesize('filePath'));
fclose($file);
$encryptedData = openssl_encrypt($fileData, "AES-256-CBC", $key, 0, $iv);
$fileToWrite = fopen('filePath', 'wb');
fwrite($fileToWrite, $encryptedData);
fclose($fileToWrite);

// File decryption code
$key = '1234';
$iv = '12dasdq3g5b2434b';
$file = fopen('filePath', 'rb');
$fileData = fread($file, filesize('filePath'));
fclose($file);
$decryptedData = openssl_decrypt($fileData, "AES-256-CBC", $key, 0,  $iv);
header("Pragma: public");
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Expires: 0");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . $filename . "\";");
$size = strlen($decryptedData);
header("Content-Length: " . $size);
echo $decryptedData;    
die;

I don't know why it doesn't work at image file. Is there any problem with my code?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 eclipse开启服务后,网页无法打开
    • ¥30 雷达辐射源信号参考模型
    • ¥15 html+css+js如何实现这样子的效果?
    • ¥15 STM32单片机自主设计
    • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
    • ¥15 不小心不正规的开发公司导致不给我们y码,
    • ¥15 我的代码无法在vc++中运行呀,错误很多
    • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
    • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
    • ¥30 ppOCRLabel导出识别结果失败