0

我真的很想安全地解决这个问题,因为涉及到客户数据。

我正在通过命令行使用 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()它往往会弄乱格式。

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

4

2 回答 2

1

我不太了解 php,但是您是否考虑过使用proc_open而不是shell_exec?它似乎比调用 shell 命令来回显输入并将其通过管道传输到gpg.

但如果您更愿意使用proc_open,请考虑使用printf而不是echo -n; 它具有更好的定义行为。例如(未经测试):

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

使用echo,您将面临echo命令(可能是内置的 shell 或/bin/echo命令)可能会将其某些参数解释为要打印的字符串以外的其他内容的风险。

于 2011-09-13T00:03:26.757 回答
0

你试过使用escapeshellarg吗?并echo在输出的字符串末尾添加换行符,因此您可能要使用-nDemo

<?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;
于 2011-09-12T23:19:48.833 回答