Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2008
    Location
    @ God's Own Country
    Posts
    57
    Thanks
    3
    Thanked 5 Times in 5 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •