Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To encrypt/decrypt a PGP file in PHP, you can follow these steps:

  1. Install the GnuPG extension for PHP using Composer:
composer require php-gnupg/php-gnupg
  1. Load the GnuPG module:
$gpg = new gnupg();
  1. Set the path to the GnuPG executable:
$gpg->seterrormode(gnupg::ERROR_EXCEPTION);
$gpg->setarmor(1);
$gpg->setbinmode(gnupg::BINMODE_ASCII);
$gpg->addencryptkey($recipient_fingerprint);
$gpg->setpassphrase($passphrase);
$encrypted_data = $gpg->encrypt($data);
  1. Decrypt the file:
$gpg = new gnupg();
$gpg->seterrormode(gnupg::ERROR_EXCEPTION);
$gpg->setarmor(1);
$gpg->setbinmode(gnupg::BINMODE_ASCII);
$gpg->adddecryptkey($recipient_fingerprint, $recipient_passphrase);
$decrypted_data = $gpg->decrypt($encrypted_data);