簡體   English   中英

內存不足,使用phpseclib解密PHP中的大文件

[英]Out of memory decrypting large files in PHP with phpseclib

我正在嘗試編寫一個使用phpseclib動態解密AES文件的程序。 文件很大,因此如果我使用file_get_contents($ f)或fread(filesize($ f))來讀取輸入文件,則會出現內存不足錯誤。

由於某種原因,像這樣的循環會創建損壞的輸出文件。 為什么!? =(

例如,大小為296,155,408字節的輸入文件輸出為18,805,826字節。 注意:如果可以在循環的一次迭代中讀取整個文件,則該方法有效。

define('MY_BUFFER_SIZE', 128 * 1024);
$sessionKey = '....';

$filenameIn = $argv[1];
$fileIn = fopen($filenameIn, 'rb');
$filenameOut = dirname($argv[1]) . DIRECTORY_SEPARATOR . basename($argv[1], '.tar.gz') . '-decrypted.tar.gz';
$fileOut = fopen($filenameOut, 'wb');

// Setup cipher for continuous buffering and copy between files.
$aesCipher = new Crypt_AES(CRYPT_AES_MODE_CBC);
$aesCipher->setKey($sessionKey);
$aesCipher->setIV("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
$aesCipher->enableContinuousBuffer();
while (!feof($fileIn)) {
    $packet = fread($fileIn, MY_BUFFER_SIZE); // @TODO: Streaming not working.
    fwrite($fileOut, $aesCipher->decrypt($packet));
}

fclose($fileIn);
fclose($fileOut);

感謝@neubert! 需要添加的是:

$aesCipher->disablePadding()

這有效:

// Setup cipher for continuous buffering and copy between files.
$aesCipher = new Crypt_AES(CRYPT_AES_MODE_CBC);
$aesCipher->setKey($sessionKey);
$aesCipher->setIV("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
$aesCipher->enableContinuousBuffer();
$aesCipher->disablePadding();
while (!feof($fileIn)) {
    fwrite($fileOut, $aesCipher->decrypt(fread($fileIn, MY_BUFFER_SIZE)));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM