加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何强制GPG接受STDIN的输入而不是尝试打开文件?

发布时间:2020-12-13 21:42:55 所属栏目:PHP教程 来源:网络整理
导读:我试图在 PHP脚本中的字符串中加入GPG clear-signed of text.我可以使GPG加密字符串中的文本,如下所示: $encrypted = shell_exec("echo '$text' | gpg -e -a -r foo@bar.com --trust-model always"); 这是完美的,加密文本发送到$encrypted变量.这证明GNUPGH
我试图在 PHP脚本中的字符串中加入GPG clear-signed of text.我可以使GPG加密字符串中的文本,如下所示:

$encrypted = shell_exec("echo '$text' | gpg -e -a -r foo@bar.com --trust-model always");

这是完美的,加密文本发送到$encrypted变量.这证明GNUPGHOME和GNUPG设置正确.

但是,当我尝试以相同的方式生成一个明确签名的消息:

$text = "googar";

$signature = exec("echo $passphrase | gpg -v --clearsign --no-tty --passphrase-fd 0 '$text' 2>&1 1> /dev/null",$output);

我收到此错误:

... string(51) "gpg: can't open `googar': No such file or directory"
[3]=>
string(46) "gpg: googar: clearsign failed: file open error"
}

返回此错误,包含或不包含$text变量周围的单引号.

如何强制GPG或shell_exec将$text视为管道而不是查找文件?

我需要以这种方式回应密码(我知道,它’非常不安全’,因为GPG无法在密码中传递密码作为命令行上的变量.

解决方法

您可以使用 proc_open并为您的密码创建单独的文件描述符:

$descriptorspec = array(
    0 => array("pipe","r"),1 => array("pipe","w"),2 => array("pipe",3 => array("pipe",);

$pipes = false;
$process = proc_open("gpg -v --clearsign --no-tty --passphrase-fd 3",$descriptorspec,$pipes);

if(is_resource($process)) {
    fwrite($pipes[3],$passphrase);
    fclose($pipes[3]);

    fwrite($pipes[0],$text);
    fclose($pipes[0]);

    $output = stream_get_contents($pipes[1]);
    $stderr = stream_get_contents($pipes[2]);

    fclose($pipes[1]);
    fclose($pipes[2]);

    $retval = proc_close($process);

    echo "retval = $retvaln";
    echo "output= $outputn";
    echo "err= $stderrn";
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读