4

I am using gnupg php functions to decrypt a file.

With the help of another post on here I managed to get it to list the keys (gnupg_keyinfo) by using gnupg_import and changing the owner of the gnupg directory and chmod permissions.

I can also successfully encrypt a file using gnupg_encrypt.

But when it comes to decrypting a file using gnupg_decrypt it doesn't work with an error message that reads:

Warning: gnupg::adddecryptkey(): get_key failed

This is the code I am using to encrypt and then decrypt the file:

$CONFIG['gnupg_home'] = '{{PATH}}';
$CONFIG['gnupg_fingerprint'] = '{{FINGERPRINT}}';

$data = 'Info to encrypt/decrypt';

$gpg = new gnupg();
putenv("GNUPGHOME={$CONFIG['gnupg_home']}");
$gpg->seterrormode(GNUPG_ERROR_WARNING);
$gpg->addencryptkey($CONFIG['gnupg_fingerprint']);

$encrypted = $gpg->encrypt($data);
echo "Encrypted text: \n<pre>$encrypted</pre>\n";


$plain_text = '';
$passphrase = '';
$gpg->adddecryptkey($CONFIG['gnupg_fingerprint'], $passphrase);
$decrypted = $gpg->decrypt($encrypted);

echo "Decrypted text: $decrypted";

I have tried it with a passphrase and it didn't work and then read that it doesn't use a passphrase anymore so I have tried it without one.

I have chmod 700 to ~/.gnupg and chmod 600 to ~/.gnupg/*

The path to the gnupghome is correct as it's the same as the path I use for the gnupg_keyinfo and there are 2 fingerprints that get returned from it and I have tried it with both.

I have googled it and I have seen people with the same problem but can't find an answer to the problem.

Thank you

Dan
  • 103
  • 1
  • 13
  • Can you decrypt the message when using GnuPG manually from the command line under the web server user's account? What version of GnuPG are you using? GnuPG starting with 2.1 doesn't support setting a passphrase directly any more, you'll have to configure `gpg-agent` to allow presetting a passphrase instead. – Jens Erat Dec 04 '16 at 10:57

0 Answers0