简体   繁体   English

使用PHP使用GPG加密

[英]Encrypting with GPG using PHP

I need help encrypting files in PHP using GPG. 我需要使用GPG在PHP中加密文件的帮助。 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. 在命令行中使用GPG可以很好地工作,但是当我从PHP尝试时,我得到一个返回值2。我还尝试传递“ --yes --always-trust”作为对命令的额外切换,这是对SO的回答之一。不开心

I have tried using the gnupg function built into PHP - all the examples I've found show how to encrypt strings and not files. 我尝试使用PHP内置的gnupg函数-我找到的所有示例都显示了如何加密字符串而不是文件。 reading the file as a string will not work for me because I'm working on large files as big as 15MB. 以字符串形式读取文件对我来说不起作用,因为我正在处理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

Try changing 尝试改变

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

To

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

Notice I changed single quotes to double 注意我将单引号更改为双引号

http://php.net/manual/en/language.types.string.php http://php.net/manual/en/language.types.string.php

You mix up the use of single quote & double quote. 您混合使用单引号和双引号。

$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';

And in this line: 在这一行:

exec("C:\\Wamp\\WWW\\gpg --homedir $path --recipient $recipient --output $encrypted_file --encrypt                     $plain_file --yes --always-trust", $answer, $rtn);

Use double quote when the string is needed to be parsed by PHP (take a note of Escape characters); 当需要通过PHP解析字符串时,请使用双引号(请注意转义字符); use single quote when the string does not need to be parsed. 当不需要解析字符串时,请使用单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM