View Single Post
Old 09-19-2012, 06:35 AM   PM User | #1
sreejithpm
New Coder

 
Join Date: Feb 2008
Location: @ God's Own Country
Posts: 57
Thanks: 3
Thanked 5 Times in 5 Posts
sreejithpm is an unknown quantity at this point
Question openssl key generation - php. how to get all the values?

hi All,

I am trying to implement the rsa encryption & decryption by using OPENSSL. Please have a look on the code given below and let me know, if it has any problem.

Code:
function pr($val){
	echo '<pre>'; 
	print_r($val);
	echo '</pre>';
}

function strtohex($string)
{
  $string = str_split($string);
  foreach($string as &$char)
    $char = dechex(ord($char));
  return implode('',$string);
}

$res=openssl_pkey_new(array('digest_alg' => 'sha1', 'private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_bits' => 1024));

// Get private key
openssl_pkey_export($res, $privkey);
$data='test';
echo 'prvt<br/>'.$privkey.'<br/> ends<br/><br/>';
// Get public key
$pubkey=openssl_pkey_get_details($res);

pr($pubkey["key"]);
echo 'prvt<br/><br/>';
//var_dump($pubkey["rsa"][""]);
pr($pubkey["rsa"]);
echo 'prvt<br/><br/>';
$n=strtohex($pubkey["rsa"]["n"]);
echo 'n<br/><br/>';
pr($n);
echo 'e<br/><br/>';
$e=$pubkey["rsa"]["e"];
pr($e);
//$binarydata = pack($pubkey["rsa"]["e"], 0x1234, 0x5678, 65, 66);
//pr($binarydata);
echo 'd<br/><br/>';
$d=strtohex($pubkey["rsa"]["d"]);
pr($d);
echo 'p<br/><br/>';
$p=strtohex($pubkey["rsa"]["p"]);
pr($p);
echo 'q<br/><br/>';
//var_dump($pubkey["rsa"]["q"]);
$q=strtohex($pubkey["rsa"]["q"]);
pr($q);

echo 'dmp1<br/><br/>';
//pr($pubkey["rsa"]["dmp1"]);
$dmp1=strtohex($pubkey["rsa"]["dmp1"]);
pr($dmp1);

echo 'dmq1<br/><br/>';
//pr($pubkey["rsa"]["dmq1"]);
$dmq1=strtohex($pubkey["rsa"]["dmq1"]);
pr($dmp1);

echo 'iqmp<br/><br/>';
//pr($pubkey["rsa"]["iqmp"]);
$iqmp=strtohex($pubkey["rsa"]["iqmp"]);
pr($iqmp);
Here I am not getting the value for the index $pubkey["rsa"]["e"].

Any help would be appreciated...
Advance thanks...
__________________
---
By
Sreejith.P.M
sreejithpm is offline   Reply With Quote