使用 PHP 通过 GPG 进行加密
我需要使用 GPG 在 PHP 中加密文件的帮助。我已经做了一些研究,但还找不到解决方案。
在命令行中使用 GPG 效果很好,但是当我尝试使用 PHP 时,我得到返回值 2。 我还尝试过将“--yes --always-trust”作为额外的开关传递给命令,正如答案之一所建议的那样,但没有喜悦。
我尝试过使用 PHP 中内置的 gnupg 函数 - 我发现的所有示例都显示了如何加密字符串而不是文件。将文件作为字符串读取对我来说不起作用,因为我正在处理大至 15MB 的大文件。
我需要帮助!
环境详细信息
OS: Windows 7
PHP installation: WAMP Server 2.1
代码
$path = "c:\wamp\www";
$recipient = "Test user";
$encrypted_file = "c:\wamp\www\test.txt.gpg";
$decrypted_file = "c:\wamp\www\decrypted_test.txt";
$plain_file = "c:\wamp\www\test.txt";
exec('gpg --homedir $path --recipient $recipient --output $encrypted_file --encrypt $plain_file --yes --always-trust', $answer, $rtn);
var_dump($answer);
var_dump($rtn);
echo "<br />ANSWER: ".$answer;
echo "<br />RTN: ".$rtn;
输出
array(0) { } int(2)
ANSWER: Array
RTN: 2
PHP User: nt authority\system
I need help encrypting files in PHP using GPG. I have done some research but I can't find a solution yet.
Using GPG in command line works perfectly but when I try from PHP I get a return value 2.
I have also tried passing '--yes --always-trust' as extra switches to command as suggested in one of the answers on SO but no joy.
I have tried using the gnupg function built into PHP - all the examples I've found show how to encrypt strings and not files. reading the file as a string will not work for me because I'm working on large files as big as 15MB.
I need help!
Environment Details
OS: Windows 7
PHP installation: WAMP Server 2.1
Code
$path = "c:\wamp\www";
$recipient = "Test user";
$encrypted_file = "c:\wamp\www\test.txt.gpg";
$decrypted_file = "c:\wamp\www\decrypted_test.txt";
$plain_file = "c:\wamp\www\test.txt";
exec('gpg --homedir $path --recipient $recipient --output $encrypted_file --encrypt $plain_file --yes --always-trust', $answer, $rtn);
var_dump($answer);
var_dump($rtn);
echo "<br />ANSWER: ".$answer;
echo "<br />RTN: ".$rtn;
Output
array(0) { } int(2)
ANSWER: Array
RTN: 2
PHP User: nt authority\system
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试更改
为
注意我将单引号更改为双
http://php.net/manual /en/language.types.string.php
Try changing
To
Notice I changed single quotes to double
http://php.net/manual/en/language.types.string.php
您混淆了单引号和单引号的使用双引号。
并在这一行中:
当字符串需要由 PHP 解析时使用双引号(注意转义字符);当不需要解析字符串时使用单引号。
You mix up the use of single quote & double quote.
And in this line:
Use double quote when the string is needed to be parsed by PHP (take a note of Escape characters); use single quote when the string does not need to be parsed.