I'm need to to sign MD5-hash with previosly generated private key (private.pgp) and passphrase. (for example 123456abc) within php script that running on apache2. I'm using gnupg also.
This is how i'm doing it now:
<?php
$keyring = "/pubkeys/.gnupg"; //this direcrtory owned by www-data
putenv("GNUPGHOME=$keyring");
$res = gnupg_init();
var_dump($res); //for debug
$info = gnupg_import($res,'private.pgp');
var_dump($info); //for debug
?>
So, gnupg_import() returns me false. Why this is happening? I've also tried to read key from a file in the same dir with this php-script, but had the same error. Please, help.
Thank you.
Assuming that you are on Ubuntu/Debian based operating system this is how I would approach the situation: Install dependencies.
Steps for creating a simple test script.
After executing steps 4 & 5 above you should have two files private_key.asc and public_key.asc Now create pgp_example.php file on the same folder and add the following lines of code:
<?php
$gpg = new gnupg();
$privateAsciiKey = file_get_contents('private_key.asc');
$publicAsciiKey = file_get_contents('public_key.asc');
/**
* import private and public keys
*/
$privateKey = $gpg->import($privateAsciiKey);
$publicKey = $gpg->import($publicAsciiKey);
$fingerprint = $publicKey['fingerprint'];
$passphrase = ''; // empty string because we didn't set a passphrase.
$plain_text = "Put Some text to encrypt here";
// catch errors
$gpg->seterrormode(gnupg::ERROR_EXCEPTION);
// encrypt plain text
try{
$gpg->addencryptKey($fingerprint);
$ciphertext = $gpg->encrypt($plain_text);
echo "\n". $ciphertext ."\n";
}
catch(Exception $e){
die('Error '.$e->getMessage());
}
// decrypt text
try{
$gpg->adddecryptkey($fingerprint, $passphrase);
$plain_text = $gpg->decrypt($ciphertext);
echo "\n". $plain_text ."\n";
}
catch(Exception $e){
die('Error: '. $e->getMessage());
}
To execute this code open terminal and run php pgp_example.php
The manual is your friend: php.net/manual/en/function.gnupg-import.php The second argument is supposed to be the data, not the filename. – Sammitch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With