Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 09-19-2012, 03:37 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Do the rest of the offsets work?
According to the openssl documentation on php.net, there is no guarantee that openssl_pkey_get_details will return data for all of the offsets.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
encryption, openssl, php, rsa

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:15 AM.


Advertisement
Log in to turn off these ads.