Page 1 of 1
php and pgp
Posted: Thu Dec 28, 2006 3:42 am
by kreoton
Hi, great coders i need some help, recently i working with one project i need to encrypt some data with pgp.
Client give me a public key i uploaded it to server, now i need to encrypt data with this key and email it. But i didn't find any info on google how to do this.
Posted: Thu Dec 28, 2006 4:27 am
by timvw
Simply call the pgp executable... and pass on the right arguments.. A good place to start:
http://be.php.net/Exec
Posted: Tue Jan 02, 2007 3:38 am
by kreoton
I made script by tutorial but, it don't work line print_r($result); always prints '2'. GnuPg is installed on server and key are added in keyring. Where problem is?
Code: Select all
<?php
// create short variable names
$from = "kreoton@gmail.com";
$title = "Some title";
$body = "Some text";
$to_email = 'irmantas@gmail.com';
// Tell gpg where to find the key ring
// On this system, user nobody's home directory is /tmp/
putenv('GNUPGHOME='.$_SERVER['DOCUMENT_ROOT'].'/.gnupg');
// create a unique file name
$infile = tempnam('/tmp/', 'pgp');
$outfile = $infile.'.asc';
// write the user's text to the file
$fp = fopen($infile, 'w');
fwrite($fp, $body);
fclose($fp);
// set up our command
$command = "/usr/bin/gpg -a --recipient 'Vardenis Pavardenis <vardenis@pavardenis.lt>' --encrypt -o $outfile $infile";
system($command, $result);
echo '<br />';
print_r($result);
echo '<br />';
// delete the unencrypted temp file
unlink($infile);
if($result==0)
{
$fp = fopen($outfile, 'r');
if(!$fp||filesize ($outfile)==0)
{
$result = -1;
}
else
{
// read encrypted file
$contents = fread ($fp, filesize ($outfile)) ;
// delete the encrypted temp file
unlink($outfile);
mail($to_email, $title, $contents, "From: $from");
echo '<h1>Message Sent</h1>
<p>Your message was encrypted and sent.
<p>thank you.';
}
}
if($result!=0)
{
echo '<h1>Error:</h1>
<p>Your mesage could not be encrypted, so has not been sent.
<p>Sorry.';
}
?>
Posted: Tue Jan 02, 2007 4:13 am
by Chris Corbyn
You didn't read the manual page correctly

$result is the exit code (status) of the command. The function itself returns a string. You may want to use exec() instead.
Posted: Tue Jan 02, 2007 7:00 am
by kreoton
i changed this one:
Code: Select all
$command = "/usr/bin/gpg -a --recipient 'Vardenis Pavardenis <vardenis@pavardenis.lt>' --encrypt -o $outfile $infile";
system($command, $result);
echo '<br />';
print_r($result);
echo '<br />';
to :
Code: Select all
$command = "/usr/bin/gpg -a --recipient 'Vardenis Pavardenis <vardenis@pavardenis.lt>' --encrypt -o $outfile $infile";
$result = exec($command);
echo '<br />';
print_r($result);
echo '<br />';
but there is no effect

Posted: Tue Jan 02, 2007 11:47 am
by Kieran Huggins
Try:
Code: Select all
$success = exec($command,$result);
Posted: Wed Jan 03, 2007 2:00 am
by kreoton
Kieran Huggins wrote:Try:
Code: Select all
$success = exec($command,$result);
Thanks, I tried this but $result still gives empty array, also $outfile is not created

Posted: Thu Jan 04, 2007 3:59 am
by kreoton
sorry for second post but i realy need help with this
I started every thing from start. Started testing gpg commands with little script:
Code: Select all
putenv('GNUPGHOME=.gnupg');
$output = shell_exec("/usr/bin/gpg --list-keys");
print('<pre>');print_r($output);print('</pre>');
the strangest thing is that command /usr/bin/gpg --list-keys works but /usr/bin/gpg --encrypt -r
name@isp.com file.txt don't
Posted: Thu Jan 04, 2007 10:33 am
by Kieran Huggins
when you run the second command in the shell, does it output to the screen?
Posted: Fri Jan 05, 2007 1:24 am
by kreoton
Kieran Huggins wrote:when you run the second command in the shell, does it output to the screen?
No nothing, but i have found code that helped me:
Code: Select all
$encryptme = "secret text";
$gpg_path = "/usr/bin/gpg";
$home_dir = "/home/virtual/domain.com/";
$user_env = "apache";
$recipient = "name@isp.com";
$cmd = "echo $encryptme | HOME=$home_dir USER=$user_env $gpg_path " . '--quiet --no-secmem-warning --encrypt --armor --always-trust ' . "--recipient $recipient";
$output = shell_exec($cmd);