zoukankan      html  css  js  c++  java
  • 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 ?>
  • 相关阅读:
    用Python实现QQ找茬游戏外挂工具
    Python常用模块
    将Qt 动态链接生成的exe及依赖dll打包方法
    Qt之VLFeat SLIC超像素分割(Cpp版)
    android studio下的NDK开发详解(一)
    条件注释判断浏览器版本<!--[if lt IE 9]>
    人脸识别中的八大难题,何时能解
    人脸识别简史与近期进展
    openCV之头文件分析
    看(学习)代码流程
  • 原文地址:https://www.cnblogs.com/changning0822/p/9964894.html
Copyright © 2011-2022 走看看