我正在用PHP编写一个小脚本来备份我的文件.在我从服务器传输文件之前,我想加密它们.
我在我的脚本的早期版本中通过在我的Linux服务器上使用exec()和OpenSSL来做到这一点.现在我正在寻找一个本机PHP函数来完成这项工作,主要是为了更好的错误处理.
问题是我的文件可能变大(如20gb).此外,我必须在shell上使用命令再次解密文件.
有谁知道如何加密PHP中的大文件,然后在命令行解密?
我现在正在使用PHP的mcrypt函数来加密:
// IV: $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND); // Create new random Key: $key = openssl_random_pseudo_bytes(32); // Encrypt: $fileStream = fopen($file, "r"); $encFileStream = fopen($file . ".enc.data", "w"); $opts = [ 'iv' => $iv, 'key' => $key, 'mode' => 'cbc' ]; stream_filter_append($encFileStream, 'mcrypt.rijndael-256', STREAM_FILTER_WRITE, $opts); stream_copy_to_stream($fileStream, $encFileStream); fclose($fileStream); fclose($encFileStream); // Encrypt random generated key and save it: $encryptedKey = null; openssl_public_encrypt($key, $encryptedKey, $publickey); file_put_contents($file . ".enc.key", $encryptedKey); // Save Initial Vetor: file_put_contents($file . ".enc.iv", $iv); // Delete unencrypted file: unlink($file);
现在要在linux命令行上解密我也尝试使用mcrypt.但我遇到的最大问题是我不知道如何添加IV mdecrypt
.到目前为止,我的命令是:
mdecrypt --decrypt -m CBC -f key.key archive_hallo.tar.gz.enc.data
这当然不起作用,因为缺少IV.那么,有没有人知道如何将IV添加到我的mdecrypt命令中?