php and pgp
Moderator: General Moderators
php and pgp
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.
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.
Simply call the pgp executable... and pass on the right arguments.. A good place to start: http://be.php.net/Exec
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.';
}
?>- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
i changed this one:
to :
but there is no effect 
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 />';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 />';- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
Try:
Code: Select all
$success = exec($command,$result);Thanks, I tried this but $result still gives empty array, also $outfile is not createdKieran Huggins wrote:Try:Code: Select all
$success = exec($command,$result);
sorry for second post but i realy need help with this
I started every thing from start. Started testing gpg commands with little script:
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
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>');- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
No nothing, but i have found code that helped me:Kieran Huggins wrote:when you run the second command in the shell, does it output to the screen?
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);