Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use the PHP OpenPGP library?

Tags:

There is a PHP extension port of the gnupg PGP library. However, I'm having a hard time finding examples that explain how to use the library.

How do you properly create keys for application users, and then use them to encrypt/decrypt text using the GnuPG library?

like image 714
Xeoncross Avatar asked Sep 13 '12 17:09

Xeoncross


People also ask

What is OpenPGP feature?

With document OpenPGP encryption, you define the set of users that can decrypt the document and you don't need to send passwords through channels which security is unknown. Besides, the OpenPGP application manages the key chain of public keys more efficiently.

What is the difference between PGP and OpenPGP?

So, to recap: PGP is the original encryption solution that allowed pre-internet goers to protect their files on bulletin board systems. OpenPGP is the IETF-approved standard that allows technology companies to make and sell PGP-compatible solutions.

What is an OpenPGP key?

OpenPGP is a public key system, which means that if you have our public key and we sign a mail message (or a software distribution) using our private key, you can have a moderate confidence level that the message or distribution really did come from us. You can learn more about OpenPGP in RFC 2440.


2 Answers

See this URL it is very help full to you. Download example and try it.

https://github.com/singpolyma/openpgp-php

Or Try it:-

You can download lib/openpgp.php and lib/openpgp_crypt_rsa.php files in above the URL.

examples/keygen.php

<?php

require dirname(__FILE__).'/../lib/openpgp.php';
require dirname(__FILE__).'/../lib/openpgp_crypt_rsa.php';

$rsa = new Crypt_RSA();
$k = $rsa->createKey(512);
$rsa->loadKey($k['privatekey']);

$nkey = new OpenPGP_SecretKeyPacket(array(
   'n' => $rsa->modulus->toBytes(),
   'e' => $rsa->publicExponent->toBytes(),
   'd' => $rsa->exponent->toBytes(),
   'p' => $rsa->primes[1]->toBytes(),
   'q' => $rsa->primes[2]->toBytes(),
   'u' => $rsa->coefficients[2]->toBytes()
));

$uid = new OpenPGP_UserIDPacket('Test <[email protected]>');

$wkey = new OpenPGP_Crypt_RSA($nkey);
$m = $wkey->sign_key_userid(array($nkey, $uid));

print $m->to_bytes();

examples/sign.php

<?php

require dirname(__FILE__).'/../lib/openpgp.php';
require dirname(__FILE__).'/../lib/openpgp_crypt_rsa.php';

/* Parse secret key from STDIN, the key must not be password protected */
$wkey = OpenPGP_Message::parse(file_get_contents('php://stdin'));
$wkey = $wkey[0];

/* Create a new literal data packet */
$data = new OpenPGP_LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt'));

/* Create a signer from the key */
$sign = new OpenPGP_Crypt_RSA($wkey);

/* The message is the signed data packet */
$m = $sign->sign($data);

/* Output the raw message bytes to STDOUT */
echo $m->to_bytes();

?>

examples/verify.php

<?php

require dirname(__FILE__).'/../lib/openpgp.php';
require dirname(__FILE__).'/../lib/openpgp_crypt_rsa.php';

/* Parse public key from STDIN */
$wkey = OpenPGP_Message::parse(file_get_contents('php://stdin'));
$wkey = $wkey[0];

/* Parse signed message from file named "t" */
$m = OpenPGP_Message::parse(file_get_contents('t'));

/* Create a verifier for the key */
$verify = new OpenPGP_Crypt_RSA($wkey);

/* Dump verification information to STDOUT */
var_dump($verify->verify($m));

?>
like image 199
Abid Hussain Avatar answered Oct 01 '22 07:10

Abid Hussain


They are very good examples based on PHP extension port has you have requested and we would take a look at some examples

Using GnuPG with PHP -- Full Tutorials

Example

Getting Key Information

putenv('GNUPGHOME=/home/sender/.gnupg');

// create new GnuPG object
$gpg = new gnupg();

// throw exception if error occurs
$gpg->seterrormode(gnupg::ERROR_EXCEPTION); 

// get list of keys containing string 'example'
try {
  $keys = $gpg->keyinfo('example');
  print_r($info);
} catch (Exception $e) {
  echo 'ERROR: ' . $e->getMessage();
}

Encrypt a Simple Mail

// set path to keyring directory
// set path to keyring directory
putenv('GNUPGHOME=/home/sender/.gnupg');

// create new GnuPG object
$gpg = new gnupg();

// throw exception if error occurs
$gpg->seterrormode(gnupg::ERROR_EXCEPTION); 

// recipient's email address
$recipient = '[email protected]';

// plaintext message
$plaintext = 
"Dear Dave,\n
  The answer is 42.\n
John";

// find key matching email address
// encrypt plaintext message
// display and also write to file
try {
  $gpg->addencryptkey($recipient);
  $ciphertext = $gpg->encrypt($plaintext);
  echo '<pre>' . $ciphertext . '</pre>';
  file_put_contents('/tmp/ciphertext.gpg', $ciphertext);
} catch (Exception $e) {
  die('ERROR: ' . $e->getMessage());
}

Decryption The Mail

// set path to keyring directory
putenv('GNUPGHOME=/home/recipient/.gnupg');

// create new GnuPG object
$gpg = new gnupg();

// throw exception if error occurs
$gpg->seterrormode(gnupg::ERROR_EXCEPTION); 

// recipient's email address
$recipient = '[email protected]';

// ciphertext message
$ciphertext = file_get_contents('/tmp/ciphertext.gpg');

// register secret key by providing passphrase
// decrypt ciphertext with secret key
// display plaintext message
try {
  $gpg->adddecryptkey($recipient, 'guessme');
  $plaintext = $gpg->decrypt($ciphertext);
  echo '<pre>' . $plaintext . '</pre>';
} catch (Exception $e) {
  die('ERROR: ' . $e->getMessage());
}

You should also look at the Example

  • http://www.developertutorials.com/pear-manual/package.encryption.crypt-gpg.examples.html
  • http://oregonstate.edu/cws/docs/gpgwrap
  • http://www.phpclasses.org/package/1724-PHP-Encrypt-data-and-manipulate-keys-using-gnuPG.html
like image 43
Baba Avatar answered Oct 01 '22 09:10

Baba