问题描述
我正在尝试使用PHP中的PGP(gnupg)解密一条简单消息.但是我总是收到此错误:get_key failed
.在堆栈溢出时,我看到很多人遇到此问题,但我无法解决它.我遵循了本教程,其中包括如何设置文件的权限: http://46dogs.blogspot.nl/2007/11/setting-up-gnupg-gpg-for-use-with-php.html
I am trying to decrypt a simple message with PGP(gnupg) in PHP.But I always get this error: get_key failed
.On stack-overflow I see many people with this problem but I can't fix it.I followed this tutorial that includes how to set the permissions for the files: http://46dogs.blogspot.nl/2007/11/setting-up-gnupg-gpg-for-use-with-php.html
这是我正在使用的脚本.我的PGP密钥不包含相位短语:
This is the script that I am using. My PGP key does not contain a phase phrase:
<?php
putenv("GNUPGHOME=/home/user/.gnupg/");
$gpg = new gnupg();
$gpg -> addencryptkey("2ADA21BDC9C96556EA0758F04A935AE0010AE203");
$encrypted_text = $gpg -> encrypt("just a test");
//echo $encrypted_text;
$gpg = new gnupg();
$gpg->seterrormode(gnupg::ERROR_EXCEPTION);
try{
$gpg -> adddecryptkey("2ADA21BDC9C96556EA0758F04A935AE0010AE203","");
$decrypted_text = $gpg -> decrypt($encrypted_text);
} catch (Exception $e) {
echo $e;
}
echo $decrypted_text;
?>
文件(pubring.gpg,trustdb.gpg和secring.gpg)的权限与教程完全相同(仅用户更改为nginx).
加密部分起作用.但是解密是行不通的.
The permissions of the files(pubring.gpg, trustdb.gpg and secring.gpg) are exactly the same as the tutorial(Only user changed to nginx).
The encrypting part works. But decrypting doesn't work.
有人知道哪里出了问题吗?
Does anyone know what is wrong?
我正在运行Centos和php5.
I am running Centos and php5.
推荐答案
该线程中的代码很好,但是请记住,您的nginx用户与user
不同,并且nginx在访问其gnupg键时会遇到问题,因为(默认值)/home/user/.gnupg
仅可访问OWNER(user
登录名).最简单的方法是为Nginx用户设置主目录,并为此创建自己的gnupg密钥.否则,仅当您使用user
登录名登录到终端(因为/home/user/.gnupg)
The code in the thread is fine, but have in mind your nginx user is different than user
and nginx will have issues accessing its gnupg keys because (by default) /home/user/.gnupg
is only OWNER (user
login) accessible.Easiest thing would be to set home directory for the nginx user and create its own gnupg keys for this purpose. Otherwise running this php will only succeed when you are logged in into terminal using the user
login (because /home/user/.gnupg)
这篇关于PHP gnupg get_key失败错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!