In writing PHP code to do PGP encryption, I've come across three general methods of invoking PGP after receiving the data by SSL (sample code is below):
- use popen,
- use a pipe in an exec statement,
- write the plaintext to disk then encrypt.
To me, the third option seems the most undesirable (least secure).
What about options 1 or 2? Are there any issues that would favour 1 method over the other? Would the unencrypted data be accessible to others using either option 1 or 2 (as it is with option 3)?
For those who are interested, we are currently using a perl script on our site to encrypted communications to a lawyer (although I know this method is also used for an inexpensive method of send credit card numbers).
Thank you.
Rob
<?
putenv("PGPPATH=/home2/csandw/.pgp");
echo date("Y-m-d h:i:s")."<br>start<br>";
$msg = date("Y-m-d h:i:s",time()+36060)."\n"."Test data to encrypt";
echo "original: <br>".$msg."<br><br>";
// UNCOMMENT ONE OF THE THREE BLOCKS BELOW
//0x7552A4F7 is the KeyID
/ 1
$pp = popen("/usr/local/bin/pgpe -r 0x7552A4F7 -o /home2/csandw/pgp_enc.txt", w);
fwrite($pp, $msg);
pclose($pp);
/
/ 2
$command = "echo '$msg' | /usr/local/bin/pgpe -r 0x7552A4F7 -o /home2/csandw/pgp_enc.txt";
$result = exec($command, $msg_crypted, $errorcode);
/
/ 3
$fp = fopen("/home2/csandw/pgp_plain.txt", "w+");
fputs($fp, $msg);
fclose($fp);
system("/usr/local/bin/pgpe -r 0x7552A4F7 -o /home2/csandw/pgp_enc.txt -a /home2/csandw/pgp_plain.txt");
unlink("/home2/csandw/pgp_plain.txt");
/
// retrieve and display the data
$fp = fopen("/home2/csandw/pgp_enc.txt", r);
$msg_crypted = fread($fp, filesize("/home2/csandw/pgp_enc.txt"));
fclose($fp);
echo "<br>crypted: <br>".$msg_crypted."<br><br>";
unlink("/home2/csandw/pgp_enc.txt");
mail("webmaster@CSandW.on.ca","pgp test",$msg_crypted);
?>