简体   繁体   English

GnuPG无法在Nginx的PHP中工作

[英]GnuPG isn't working in PHP with nginx

I think that probably I'm missing something, but I don't see it right now. 我认为可能是我缺少了一些东西,但现在看不到。 I want create a simple form where users can encrypt automatically messages between them (form message to user2 -> encrypt(message) -> user2 receive it and decrypt). 我想创建一个简单的表单,用户可以在其中自动加密它们之间的消息(发送给user2的表单消息-> crypto(message)-> user2接收并解密)。 I'm using nginx, I installed gnupg following their instructions and add it to my php.ini (now it shows that GnuPG is enabled with GPGME Version 1.4.3 and Extension Version 1.3.6) I want use a specific keyring located at /usr/share/nginx/.gnupg I tried the following code: 我正在使用nginx,按照他们的说明安装了gnupg并将其添加到我的php.ini中(现在它显示GnuPG已通过GPGME版本1.4.3和扩展版本1.3.6启用),我想使用位于/usr/share/nginx/.gnupg的特定密钥环/usr/share/nginx/.gnupg我尝试了以下代码:

$iterator = new gnupg_keylistiterator("developer");
foreach($iterator as $fingerprint => $userid) {
    echo $fingerprint." -> " . $userid . "\n";
}
var_dump($iterator);

And I just obtain the following response from var_dump: 我只是从var_dump获得以下响应:

object(gnupg_keylistiterator)#1 (0) { } object(gnupg_keylistiterator)#1(0){}

Maybe my question is an idiot question, but I never used gnupg in php and I want learning, but I'm stunk since yesterday and I don't understand why it doesn't work... Thanks for your time 也许我的问题是个白痴问题,但是我从未在php中使用过gnupg,我想学习,但是我从昨天开始就很迷惑,我不明白为什么它不起作用...谢谢您的时间

The most common issue is that you imported the keys to another keyring than later is searched for keys. 最常见的问题是,您将密钥导入了另一个密钥环,然后才搜索密钥。 GnuPG uses a per-(system)-user "GnuPG home directory", each containing individual keyrings. GnuPG使用每个(系统)用户的“ GnuPG主目录”,每个目录都包含单独的密钥环。 If you import a key as the administrator or a developer you import the key to your own keyring, while usually the web server running the PHP application is executed in another user context and will not find this key, resulting in an empty result when listing the keys from within PHP. 如果您以管理员或开发人员的身份导入密钥,则将该密钥导入到自己的密钥环中,而运行PHP应用程序的Web服务器通常是在另一个用户上下文中执行的,并且找不到该密钥,因此列出该密钥时会导致结果为空。 PHP中的密钥。

You can set this by setting up an environment variable before initializing the GnuPG binding. 您可以通过在初始化GnuPG绑定之前设置环境变量来进行设置。

putenv("GNUPGHOME=/tmp");  // Set GnuPG home directory to the temp folder
$res = gnupg_init();       // Initialize GnuPG

Obviously /tmp does not actually qualify as a reasonable directory, choose something where your application stores application data anyway. 显然, /tmp实际上并不适合作为合理的目录,因此请选择在您的应用程序中存储应用程序数据的位置。 It should not be a directory accessible through HTTP. 它不应是可通过HTTP访问的目录。

As an alternative, gnupg_import($res, $pubkey) the key before using it (but this will result in some performance penalty for importing the key). 作为替代方案,在使用gnupg_import($res, $pubkey)密钥之前,但这会导致导入密钥的性能gnupg_import($res, $pubkey)

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

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