Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP gnupg enrypt works but decrypt not

i'm about to use gnupg to encrypt and decrypt files. The strange thing is, encrypt works fine, but decrypt always returns false.

Here a simple php script encrypting and decrypting content:

$content = 'test text';
putenv("GNUPGHOME=/PATH_TO_GPG_PATH");
$gpg = new gnupg();
$gpg->addencryptkey("FINGERPRINT");
$enc = $gpg->encrypt($content);

var_dump($enc);

$gpgD = new gnupg();
$gpgD->adddecryptkey("FINGERPRINT","PASSPHRASE");
$plain = $gpgD->decrypt($enc);
var_dump($plain);

Versions

Debian packages

gpgv 1.4.18-7

libgpgme11:amd64 1.5.1-6

pecl

Package Version State gnupg 1.4.0 stable

PHP version: PHP 7.1.11-1+0~20171027135825.10+jessie~1.gbp2e638d

Anyone experienced this problem already? I'm out of ideas. Thank you in advance.

like image 431
Sandor Farkas Avatar asked Oct 28 '22 23:10

Sandor Farkas


1 Answers

Have you tried invoking gnupg_geterror() after gnupg_adddecryptkey()? I suspect your private key is not actually getting accepted. I assume it needs to be in PHP user's GPG keyring? Also in a couple of brief tests I ran, I kept getting prompted for the passphrase on the terminal, but that could be because of my paranoid gpg config (I disable passphrase caching completely).

Another way to trap errors would be to set gnupg_seterrormode() to ERROR_EXCEPTION or similar to see what's actually happening...

like image 137
Rouben Tchakhmakhtchian Avatar answered Nov 15 '22 06:11

Rouben Tchakhmakhtchian