PHP Openssl 生成公鑰私鑰


 1 <?php
 2 //配置信息
 3 $dn = array(
 4     "countryName" => "GB",
 5     "stateOrProvinceName" => "Somerset",
 6     "localityName" => "Glastonbury",
 7     "organizationName" => "The Brain Room Limited",
 8     "organizationalUnitName" => "PHP Documentation Team",
 9     "commonName" => "Wez Furlong",
10     "emailAddress" => "wez@example.com"
11 );
12 
13 $config = array(
14     "private_key_bits" => 512, //指定應該使用多少位來生成私鑰  512 1024  2048  4096等
15     "private_key_type" => OPENSSL_KEYTYPE_RSA, //選擇在創建CSR時應該使用哪些擴展。可選值有 OPENSSL_KEYTYPE_DSA, OPENSSL_KEYTYPE_DH, OPENSSL_KEYTYPE_RSA 或 OPENSSL_KEYTYPE_EC. 默認值是 OPENSSL_KEYTYPE_RSA.
16 );
17  //    生成證書
18  $privkey = openssl_pkey_new($config);
19  $csr = openssl_csr_new($dn, $privkey);
20  $sscert = openssl_csr_sign($csr, null, $privkey, $numberofdays);              
21  //    導出證書$csrkey
22  openssl_x509_export($sscert, $csrkey);
23  //    導出密鑰$privatekey
24  openssl_pkcs12_export($sscert, $privatekey, $privkey, $privkeypass);
25  //    獲取私鑰
26  openssl_pkcs12_read($privatekey, $certs, $privkeypass);
27  if (empty($certs['pkey'])) {
28           $arr = array();
29     }
30  $prikeyid = $certs['pkey'];
31  //    獲取公鑰
32   $pub_key = openssl_pkey_get_public($csrkey);
33   $keyData = openssl_pkey_get_details($pub_key);
34   if (empty($keyData['key'])) {
35          $arr = array();
36     }
37   $public = $keyData['key'];
38   $arr =  array('publicKey'=>$public,'privateKey'=>$prikeyid);
39   print_r($arr);
40 ?>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM