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g27mhFy1chmLosfv4V/Hr
rZg2IwKBgQCsngJZuupvJiEVSTe9SNGdqxA8dlxbbaZN7TgjBGfJacgUXAClZCU0
GpSzS9tu0RvsmuNLD1P2dEk1LFR3buo+BzVaer5bByE7PyYmWi1C802D+urDtJcz
dUWvXBOuZBbcHrHFj51pBDl5O5VUR2bbwFHtApj+r06lrBV+9HVeZA==
-----END RSA PRIVATE KEY-----
现在我使用2097152块加密文件,因为文件很大:
// Now read the file!
$fp = fopen($file_name, 'rb');
$fw = fopen($file_name.'.enc', 'ab');
while (!feof($fp)) {
$block_content = fread($fp, 2097152);
$pubKey[] = openssl_pkey_get_public( $public_key );
$sealed = "";
$ekeys = "";
openssl_seal( $dataToEncrypt, $sealed, $ekeys, $pubKey );
fwrite( $fw, $_sealed );
$sent_block_count++;
} // end while
此代码没问题 - 文件已加密并保存....
问题在于解密过程:
$fp = fopen($file_name, 'rb');
$fw = fopen($new_name, 'ab');
if ( $fp && $fw ) {
while (!feof($fp)) {
$block_content = fread($fp, 2097152);
$privKey = openssl_pkey_get_private( $_private );
$open = "";
$ekeys = "";
openssl_open( $sealed, $open, $ekeys, $private_key );
fwrite( $fw, $open );
$sent_block_count++;
} // end while
我没有收到任何错误,但输出文件总是零字节。