php ssl生成密钥和证书

<?php

/*生成公钥和私钥*/
function exportOpenSSLFile(){
$opensslConfigPath = "D:/phpStudy/Apache/conf/openssl.cnf";
  $config = array(
    "digest_alg"    => "sha512",
    "private_key_bits" => 4096,           //字节数  512 1024 2048  4096 等
    "private_key_type" => OPENSSL_KEYTYPE_RSA,   //加密类型
    "config"           => $opensslConfigPath,
  );

  $res = openssl_pkey_new($config);

  if($res == false) return false;
  openssl_pkey_export($res, $private_key,null,$config);
  $public_key = openssl_pkey_get_details($res);
  $public_key = $public_key["key"];

  file_put_contents("/cert_public.key",$public_key);
  file_put_contents("/cert_private.pem",$private_key);


}

exportOpenSSLFile();

/**生成证书http://wiki.uniformserver.com/index.php/SSL_PHP_Server_Key_and_Certificate_generation**/

$opensslConfigPath = "D:/phpStudy/Apache/conf/openssl.cnf";
$dn = array(   

        "countryName" => 'XX', //所在国家名称    
        "stateOrProvinceName" => 'State', //所在省份名称    
        "localityName" => 'SomewhereCity', //所在城市名称    
        "organizationName" => 'MySelf',   //注册人姓名    
        "organizationalUnitName" => 'Whatever', //组织名称    
        "commonName" => 'mySelf', //公共名称    
        "emailAddress" => '[email protected]' //邮箱    
    );    
  $config = array(
    "digest_alg"    => "sha512",
    "private_key_bits" => 4096,           //字节数  512 1024 2048  4096 等
    "private_key_type" => OPENSSL_KEYTYPE_RSA,   //加密类型
    "config"           => $opensslConfigPath,
  );         
    $privkeypass = '111111'; //私钥密码    
    $numberofdays = 365;     //有效时长    
    $cerpath = "./test.cer"; //生成证书路径    
    $pfxpath = "./test.pfx"; //密钥文件路径    


    //生成证书    
    $privkey = openssl_pkey_new($config);    
    $csr = openssl_csr_new($dn, $privkey,$config); 
    // $sscert = openssl_csr_sign($csr, null, $privkey, 365, $config);   
    $sscert = openssl_csr_sign($csr,null, $privkey, $numberofdays,$config); 
    openssl_pkey_export_to_file($privkey,"server.key",NULL, $config);

    openssl_x509_export_to_file($sscert,"server.crt");
    openssl_csr_export_to_file($csr, "server.csr");

猜你喜欢

转载自blog.csdn.net/qiuqiuLovecode/article/details/78456730
今日推荐