0

I have read all about asymmetric encryption in PHP. I understand the mcrypt method. I understand the requirement of having two keys (Public and Private). The only thing I could not understand that where and how I generate these key pairs? Can anyone please explain the process of key generation? Thank You!

Nav
  • 105
  • 1
  • 2
  • 10
  • 2
    Where specifically have you read *all about asymmetric encryption* with *mcrypt*? It [only spports symetric](http://stackoverflow.com/questions/2649672/does-mcrypt-support-asymmetric-encryption) ciphers. – mario Aug 18 '14 at 20:30
  • Sorry, I didn't mean to imply that mcrypt requires two keys. I meant that I have read about both symmetric n asymmetric encryption. I have edited the question. – Nav Aug 18 '14 at 20:54
  • Look around [`openssl_pkey_new`](http://php.net/openssl_pkey_new), or use the command line `openssl` tool to generate a key pair. – mario Aug 18 '14 at 20:57

1 Answers1

1

You'll need a PGP library to do what you wish, but it's pretty straightforward. To create your keys, use:

<?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 <test@example.com>');

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

print $m->to_bytes();
hd1
  • 33,938
  • 5
  • 80
  • 91
  • Thank you for your reply. Further question though, do I need to download library files(openpgp.php & openpgp_crypt_rsa.php) from somewhere? Because the code is giving below error for these lines : Failed opening required '/home/content/27/11042427/html/../lib/openpgp.php – Nav Aug 18 '14 at 21:00
  • You'll need to install the requisite PHP files from [here](https://github.com/singpolyma/openpgp-php). Again, best of luck and leave a question if you've further issues – hd1 Aug 18 '14 at 21:35