phppublic-key-encryptionpgp

Encrypt large data using public key .ASC in php


I have a public key with extension .asc that I need to use to encrypt data and to send it in a plain text file. I am trying to accomplish this in a windows machine using php. Any ideas? Thanks,


Solution

  • Make sure you installed the gnupg extension for PHP.

    $gpg = new gnupg();
    $publicData = file_get_contents('public.asc');
    $publicKey = $gpg->import($publicData);
    $gpg->addencryptkey($publicKey['fingerprint']);
    echo $gpg->encrypt('Data to encrypt');
    

    Instead of encrypting a constant, replace by the file contents, and store the encrypted message wherever you want.