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