OK,
I'm not the worlds best PHP programmer but I've been having real problems decrypting a GPG message. Encryption works fine it's just getting me head around decrypting it in PHP.
Has anyone managed to do it. I'd really appreciate an example of some working code (for Linux).
Anyway here's my code so far. It's just a test program that encrypts, displays the encrytion then is suppose to decrypt it and display the encrypted result. Although it doesn't do the last bit! I just get:
gpg: encrypted with 2048-bit ELG-E key, ID 3CE28CF7, created 2003-11-24
"me (me) <me@mydomain.com>"
CODE:
<?php
#!/usr/bin/php
/* set up some strings */
$gnupghome = "/home/myusername/key";
$uid = "me@mydomain.com";
$msg = "This is a test message.";
$path_to_gpg ="/usr/bin/gpg";
putenv("GNUPGHOME=$gnupghome");
/* OK now let's encrypt the $msg */
$cmd = "/usr/bin/gpg --textmode --always-trust ";
$cmd .= "--armor --batch --no-secmem-warning --homedir '$gnupghome' ";
$cmd .= " --recipient '$uid' --encrypt
";
$pp = popen($cmd, "w");
fputs($pp, $msg);
pclose($pp);
$encrypted = `$cmd`;
print "<form><textarea NAME=txt ROWS=20 COLS=75 WRAP=VIRTUAL>";
print $encrypted;
print "</textarea>";
/* Now the decrypt part */
$passphrase = 'mysecretpassphrase';
global $gnupghome;
global $path_to_gpg;
$body = escapeshellarg ($encrypted);
$pre_pass =" | $path_to_gpg --passphrase-fd 0 --batch --no-tty --decrypt --homedir $gnupghome 2>&1";
if ($passphrase) {
echo "<br>Got passphrase.\n";
}
echo "<br>Command string: echo [PassPhrase][Body]$pre_pass<br>\n";
$passphrase = escapeshellarg($passphrase . "\n");
$command = "echo $passphrase$body$pre_pass";
exec($command, $plaintext, $returnval);
// make the result a string
if (is_array($plaintext)) {
$plaintext_str = implode($plaintext,"\n");
};
echo " <textarea cols=80 rows=20 name=plaintext>$plaintext_str</textarea>";
?>