alistairphillips.com

I’m : a web and mobile developer based in the Australia.


PHP's gnupg not importing keys

If you're needing to encrypt some data within a PHP page served by Apache you might find that the following code snippet doesn't seem to import anything and $info lacks the all-important fingerprint key:

<?php
    $gpg  = new gnupg();
    $info = $gpg->import(file_get_contents('key.asc'));
?>

This occurs as the .gnupg folder underneath the web server user cannot be created or written to (see http://reinke.co/post/7896434331/gnupg-import-failure). Perform the following steps to relocate the .gnupg folder:

sudo mkdir /pubkeys/.gnupg
sudo chown -R www-data /pubkeys
sudo chgrp -R www-data /pubkeys

and add the following just before you instantiate the GPG object:

<?php
    $keyring = "/pubkeys/.gnupg";
    putenv("GNUPGHOME=$keyring");
?>