php解密so文件,php - 如何使用php(mcrypt)加密/解密文件? - SO中文参考 - www.soinside.com...

如何仅使用mcrypt和php标准函数来加密/解密文件?

使用钠(PHP 7.2 +,PECL ext / sodium或sodium_compat)加密文件crypto_secretstream

解密看起来像这样:const CUSTOM_CHUNK_SIZE = 8192;

/**

* @ref https://stackoverflow.com/q/11716047

*/

function encryptFile(string $inputFilename, string $outputFilename, string $key): bool

{

$iFP = fopen($inputFilename, 'rb');

$oFP = fopen($outputFilename, 'wb');

[$state, $header] = sodium_crypto_secretstream_xchacha20poly1305_init_push($key);

fwrite($oFP, $header, 24); // Write the header first:

$size = fstat($iFP)['size'];

for ($pos = 0; $pos < $size; $pos += CUSTOM_CHUNK_SIZE) {

$chunk = fread($iFP, CUSTOM_CHUNK_SIZE);

$encrypted = sodium_crypto_secretstream_xchacha20poly1305_push($state, $chunk);

fwrite($oFP, $encrypted, CUSTOM_CHUNK_SIZE + 17);

sodium_memzero($chunk);

}

fclose($iFP);

fclose($oFP);

return true;

}

同时使用两个功能:/**

* @ref https://stackoverflow.com/q/11716047

*/

function decryptFile(string $inputFilename, string $outputFilename, string $key): bool

{

$iFP = fopen($inputFilename, 'rb');

$oFP = fopen($outputFilename, 'wb');

$header = fread($iFP, 24);

$state = sodium_crypto_secretstream_xchacha20poly1305_init_pull($header, $key);

$size = fstat($iFP)['size'];

$readChunkSize = CUSTOM_CHUNK_SIZE + 17;

for ($pos = 24; $pos < $size; $pos += $readChunkSize) {

$chunk = fread($iFP, $readChunkSize);

[$plain, $tag] = sodium_crypto_secretstream_xchacha20poly1305_pull($state, $chunk);

fwrite($oFP, $plain, CUSTOM_CHUNK_SIZE);

sodium_memzero($plain);

}

fclose($iFP);

fclose($oFP);

return true;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值