Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP实现GPG加密_Php_Windows_Exec_Gnupg - Fatal编程技术网

用PHP实现GPG加密

用PHP实现GPG加密,php,windows,exec,gnupg,Php,Windows,Exec,Gnupg,我需要帮助加密文件在PHP使用GPG。我做了一些研究,但还没有找到解决办法 在命令行中使用GPG非常有效,但是当我尝试从PHP中使用时,我得到了一个返回值2。 我也尝试过将“--是的--始终信任”作为额外的开关传递给命令,正如在SO上的一个答案中所建议的那样,但没有乐趣 我尝试过使用PHP内置的gnupg函数——我找到的所有示例都演示了如何加密字符串而不是文件。以字符串形式读取文件对我来说不起作用,因为我正在处理15MB的大文件 我需要帮助 环境详细信息 OS: Windows 7 PHP in

我需要帮助加密文件在PHP使用GPG。我做了一些研究,但还没有找到解决办法

在命令行中使用GPG非常有效,但是当我尝试从PHP中使用时,我得到了一个返回值2。 我也尝试过将“--是的--始终信任”作为额外的开关传递给命令,正如在SO上的一个答案中所建议的那样,但没有乐趣

我尝试过使用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;

你混淆了单引号和双引号的用法

$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("C:\\Wamp\\WWW\\gpg --homedir $path --recipient $recipient --output $encrypted_file --encrypt                     $plain_file --yes --always-trust", $answer, $rtn);
当PHP需要解析字符串时,使用双引号(注意转义字符);当字符串不需要解析时,请使用单引号。

尝试更改

exec('gpg --homedir $path --recipient $recipient --output $encrypted_file --encrypt                     $plain_file --yes --always-trust', $answer, $rtn);

注意,我将单引号改为双引号


我已经按照你的建议做了,但输出仍然是一样的,非常感谢你,你让我走上了正确的道路。我能让它工作。问题是开关值必须在双引号(“”)内。exec(“gpg--homedir\“$path\”--recipient\“$recipient\”--output\“$encrypted\u file\”--encrypt\“$plain\u file\”--yes--always trust”,$answer,$rtn);我已经按照你的建议做了,但是输出仍然是相同的。检查执行权限?另外,您应该为
exec()
函数提供GPG的完整路径。执行权限:PHP用户是nt authority\system-我想用户应该有执行权限。GPG的完整路径:我有一个本地版本的GPG运行在C:\Wamp\WWW中,按照建议的步骤()不正确。NT\U AUTHORITY\SYSTEM不一定拥有这些权限。您应该仔细检查文件夹访问权限。例如,在某些情况下,PHP用户对C:\WINDOWS\Temp没有权限
exec('gpg --homedir $path --recipient $recipient --output $encrypted_file --encrypt                     $plain_file --yes --always-trust', $answer, $rtn);
exec("gpg --homedir $path --recipient $recipient --output $encrypted_file --encrypt                     $plain_file --yes --always-trust", $answer, $rtn);