【发布时间】:2014-04-15 03:56:59
【问题描述】:
我正在用 PHP 编写一个小脚本来备份我的文件。在从服务器传输文件之前,我想对它们进行加密。
我在以前版本的脚本中通过在我的 Linux 服务器上使用 exec() 和 OpenSSL 来做到这一点。现在我正在寻找一个原生 PHP-Function 来完成这项工作,主要是为了更好地处理错误。
问题是我的文件可能会变大(比如 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 命令中?
【问题讨论】:
标签: php linux encryption cryptography mcrypt