簡體   English   中英

使用openssl_encrypt加密文件

[英]Encrypt files using openssl_encrypt

openssl_encrypt看過使用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));

謝謝

沒有理由不能使用file_get_contents()將文件讀取到變量中,無法使用openssl_public_encrypt()對其進行加密,然后使用fopen()fwrite()和fclose()將其保存到磁盤上,但是大概有一個限制您可以通過這種方法將多少大小的文件加載到內存中。

在腳本中進行文件加密可能還會有性能開銷,但是您應該能夠足夠輕松地對其進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM