It's the first time I need to do this.
I have to code a script in php to run with cli, the script is passed a .zip file which I have to decompress, the files inside are .pgp files and I need to decrypt these files, I have the following code.
function decryptPGPFiles($pgpFiles, $passphrase){
$res = gnupg_init();
foreach($pgpFiles as $filename){
gnupg_adddecryptkey($res,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",$passphrase);
$cipher = file_get_contents($filename);
$plain = gnupg_decrypt($res,$cipher);
var_dump($plain);
die;
}
}
Of course the 'var_dump($plain);' and 'die;' are just for testing. Anyway the $plain is always false. Assume that $pgpFiles is an array of filenames for the .pgp files in the current dir.
I do have a private key with name 'private.gpg' in the current dir and also I know the $passphrase.
I know that the 2nd parameter for the function 'gnupg_adddecryptkey' is a $fingerprint according to php.net, when I run 'gpg --import private.gpg' and then 'gpg --fingerprint' it appears the respective fingerprint and that's the one I'm passing as parameter, anyway the fingerprint appears with several blankspaces and I trim them.
I'm sure I'm not doing the right steps, but I can't find a documentation clear enough for me. It is even possible to decrypt a .pgp file from php cli?
Thanks in advance.
gpg --list-secret-keys
, is the key referenced by the fingerprint listed? – Jens Erat Nov 12 '17 at 9:25gpg --list-secret-keys
I do see the imported secret key. Anyway I tried so much without functionality that I gave up on it and instead I usedexec()
to use the linux command line from php and that was it. – Migdress Nov 13 '17 at 13:34