Search code examples
phpencryptiongnupg

gnupg encrypt PHP


I am doing a small project using my RaspberryPi to send encrypted message via Web contact form. I configured everything using msmtp and PHP and am able to send emails. Now I would like to encrypt the message and attach public key so that the receiver client can decrypt the message.

I installed gnupg and also php extensions. I have the fingerprint, public key and key. I added extension=gnupg.so to php.ini.

I am following the following example code and I am not successful with encryption.

<?php
putenv("GNUPGHOME=/home/pi/.gnupg/");
$fingerprint = "fingerprint";
$publickey = file_get_contents('public.key');
echo $publickey;

 $gpg = new gnupg();
    $gpg->seterrormode(gnupg::ERROR_EXCEPTION);
    $info = $gpg->import($publicKey);
    $gpg->addencryptkey($fingerprint);

    $test = 'test'; 
    $enc = $gpg->encrypt($test);
    echo $enc;
?>

I would like to know if i am missing something? It simply doesnt do anything! Do i have to configure anything else that php understands encrypt function.

Here is the article that i followed.

Any help would be really appreciated.


Solution

  • Openssl sealed the deal and its working like charm!I found the help here.