Search code examples
phpencryptionamazon-ec2gpgpugnupg

Dynamically load/use private PGP key, use it to decrypt data, then unload it from keyring


This is a continuation of a previous question ( GnuPG: How to encrypt/decrypt files using a certain key? ), I need to:

  1. Dynamically load a private PGP key (that is specific to a particular user) and import it into the keyring
  2. Decrypt some data using this key
  3. Unload this private key & erase any trace of it

This may seem like a complicated way to decrypt data, suffice to say that I have strong reasons to do that. I may be wrong but I can't just decrypt files by supplying the private key via the commandline (this would be perfect but it seems I can't do that), ie.

exec("gpg --use-key=private-key.gpg -o $clear_file_name --decrypt $encr_file_name");

So, how can I dynamically load/use private PGP key, use it to decrypt data, then unload it from keyring

OR (if this is not possible), are there any other similar tool that allows me to achieve this?

PS. The code will be run on a linux EC2 instance


Solution

  • You need to use gpg's --homedir option, i.e. with a temporary dir that you can delete afterwards. See this answer at superuser for an example.