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
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;
?>
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?
I am running Centos and php5.
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 theuser
login (because /home/user/.gnupg)