Php GNUPG转义shell_exec中的回显数据

Php GNUPG转义shell_exec中的回显数据,php,shell,exec,shell-exec,gnupg,Php,Shell,Exec,Shell Exec,Gnupg,我真的很想安全地进行这项工作,因为涉及到客户数据 我通过命令行使用GNUPG,因为我在共享主机上,而PHP类不可用。因此,我的代码如下: putenv("GNUPGHOME=/home/me/.gnupg"); $gpg = '/usr/bin/gpg'; $gpgrecipient = 'email'; $mailrecp = 'email'; $plain = 'Here is the encrypted Text Here is the encrypted Text Here is th

我真的很想安全地进行这项工作,因为涉及到客户数据

我通过命令行使用GNUPG,因为我在共享主机上,而PHP类不可用。因此,我的代码如下:

putenv("GNUPGHOME=/home/me/.gnupg");

$gpg = '/usr/bin/gpg';
$gpgrecipient = 'email';
$mailrecp = 'email';
$plain = 'Here is the encrypted Text Here is the encrypted Text Here is the
    encrypted Text Here is the encrypted Text Here is the encrypted Text Here is the
    encrypted Text Here is the encrypted Text Here is the encrypted Text Here is the  
    encrypted Text';



$encrypted = shell_exec("echo {$plain} | {$gpg} --no-auto-check-trustdb --lock-never -e -a -r {$gpgrecipient} ");
那么,如何在保持数据完整性的同时转义
$plain

如果我只是使用
escapeshellcmd()
它往往会弄乱格式


我对将任何内容保存到文件有点怀疑,因为它是共享主机上的敏感数据。

您是否尝试过使用
escapeshellarg
?并且
echo
在输出的字符串末尾添加一个换行符,因此您可能需要使用
-n


我对php不太了解,但您是否考虑过使用
proc\u open
而不是
shell\u exec
?这似乎比调用shell命令来回显输入并将其传输到
gpg
更为简洁

但如果您宁愿使用<代码> PROCKOPENG/CODE >,请考虑使用<代码> PROTFF 而不是<代码> ECHO-N ;它有更好的定义行为。例如(未经测试):


使用
echo
,您可能会遇到这样的风险:
echo
命令(可以是一个内置的shell,也可以是
/bin/echo
命令)可能会将其某些参数解释为要打印的字符串以外的内容。

我没有使用proc_open-在管道方面似乎要复杂得多,但希望我能找到答案。谢谢你的信息!
<?php

$gpg = '/usr/bin/gpg';
$gpgrecipient = 'email';
$mailrecp = 'email';
$plain = 'Here is the encrypted Text Here is the encrypted Text Here is the
    encrypted Text Here is the encrypted Text Here is the encrypted Text Here is the
    encrypted Text Here is the encrypted Text Here is the encrypted Text Here is the  
    encrypted Text';


$plain = escapeshellarg($plain);

$cmd = "echo -n {$plain} | {$gpg} --no-auto-check-trustdb --lock-never -e -a -r {$gpgrecipient} ";

echo $cmd;
$encrypted = shell_exec("printf '%s' '{$plain}' | {$gpg} ...`