2

openssl_encrypt我已经看到了使用命令行加密文件的解决方案,如下所示:

file_put_contents ('./file.encrypted',openssl_encrypt ($source, $method, $pass, true, $iv));

$exec = "openssl enc -".$method." -d -in file.encrypted -nosalt -nopad -K ".strtohex($pass)." -iv ".strtohex($iv);

或者,openssl_encrypt用于加密字符串,如下所示:

$string = 'It works ? Or not it works ?';
$pass = '1234';
$method = 'aes128';
file_put_contents ('./file.encrypted', openssl_encrypt ($string, $method, $pass));

但不是直接从代码中加密文件的东西。这是为什么 ?

如果我想从代码中加密 2-10mb 的文件,而不是像使用命令行那样为每个文件打开单独的进程,该怎么办?

我正在寻找可以为我做到这一点的代码示例,例如:

$myfile = 'files/myfile.doc';
$pass = '1234';
$method = 'aes128';
file_put_contents ('./file.encrypted', openssl_encrypt ($myfile, $method, $pass));

谢谢

4

1 回答 1

0

没有理由不能使用 file_get_contents() 将文件读入变量,使用 openssl_public_encrypt() 对其进行加密,然后使用 fopen() fwrite() 和 fclose() 将其保存到磁盘,但大概有一个限制使用这种方法可以将多大的文件加载到内存中。

在脚本中进行文件加密也可能会产生性能开销,但您应该能够很容易地对此进行测试。

于 2012-07-26T20:31:34.617 回答