【问题标题】:Encrypt files using openssl_encrypt使用 openssl_encrypt 加密文件
【发布时间】:2012-06-27 09:40:43
【问题描述】:

我已经看到了在命令行中使用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));

谢谢

【问题讨论】:

    标签: php openssl


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,我最终使用了第一块代码。就性能而言,它似乎还不错
    猜你喜欢
    • 2019-06-16
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    相关资源
    最近更新 更多