php and pgp

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kreoton
Forum Commoner
Posts: 42
Joined: Fri Mar 03, 2006 7:27 pm

php and pgp

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Simply call the pgp executable... and pass on the right arguments.. A good place to start: http://be.php.net/Exec
User avatar
kreoton
Forum Commoner
Posts: 42
Joined: Fri Mar 03, 2006 7:27 pm

Post 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.';
}
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
kreoton
Forum Commoner
Posts: 42
Joined: Fri Mar 03, 2006 7:27 pm

Post 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 :(
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Try:

Code: Select all

$success = exec($command,$result);
User avatar
kreoton
Forum Commoner
Posts: 42
Joined: Fri Mar 03, 2006 7:27 pm

Post 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 :(
User avatar
kreoton
Forum Commoner
Posts: 42
Joined: Fri Mar 03, 2006 7:27 pm

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

when you run the second command in the shell, does it output to the screen?
User avatar
kreoton
Forum Commoner
Posts: 42
Joined: Fri Mar 03, 2006 7:27 pm

Post 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);
Post Reply