当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP openssl_pkcs12_export()用法及代码示例


opensl_pkcs12_export()函数是PHP中的内置函数,用于以PKCS#12文件格式存储在名为x509的字符串中。证书存储文件对应于PKCS#12变量。

用法:

bool openssl_pkcs12_export( mixed $x509, string &$out, 
        mixed $priv_key, string $pass [, array $args] )

参数:该函数接受上述和以下所述的五个参数:

  • $x509:此参数是定义公钥证书格式的标准。
  • $out:这是通过引用传递的变量,该变量在成功执行以上函数时将保持PKCS#12。
  • $priv_key:此参数用于与甜点“priv_key”匹配的私钥。私钥是PKCS#12文件的组成部分。
  • $pass:此参数用于解锁PKCS#12文件的加密密码。
  • $args:“$args”代表参数,变量是代表其他内容的值。

返回值:如果成功,则此函数返回TRUE;如果失败,则返回FALSE。

以下示例程序旨在说明PHP中的openssl_pkcs12_export()函数:



程序:

PHP

<?php 
  
$dn = array( 
    "countryName" => 'xx', 
    "stateOrProvinceName" => 'uttar prradesh', 
    "localityName" => 'varanasi', 
    "organizationName" => 'geeksforgeeks', 
    "organizationalUnitName" => 'geeks team', 
    "commonName" => 'people', 
    "emailAddress" => 'user@geeks.com'
); 
   
$privateKeyPass = 'dummyPassword'; 
$numberOfDays = 108; 
   
$privateKey = openssl_pkey_new(); 
$csr = openssl_csr_new($dn, $privateKey); 
   
// Create a csr file, change null 
// to a filename to save 
$sscert = openssl_csr_sign($csr,  
    null, $privateKey, $numberOfDays); 
   
// On success $publicKey will  
// hold the PEM content  
openssl_x509_export($sscert, $publicKey); 
   
// Export the privateKey as a PEM content 
openssl_pkey_export($privateKey,  
    $privateKey, $privateKeyPass); 
   
// Parses the $privateKey and used  
// by openssl_pkcs12_export_to_file. 
$key = openssl_pkey_get_private( 
    $privateKey, $privateKeyPass); 
   
$certificateOutput = null; 
  
// Save the pfx file to $certificateOutput 
openssl_pkcs12_export($sscert,  
    $certificateOutput, $key, $privateKeyPass); 
   
// openssl_pkcs12_read to read the pkcs12 
// certificate and store into array 
openssl_pkcs12_read ( $certificateOutput,  
    $readableOutput,  $privateKeyPass ); 
      
var_dump(($readableOutput)); 
?>

输出:

 array(2) {
 ["cert"]=>
 string(1444) "-----BEGIN CERTIFICATE-----
MIID/DCCAuSgAwIBAgIBADANBgkqhkiG9w0BAQUFADCBljELMAkGA1UEBhMCeHgx
FzAVBgNVBAgMDnV0dGFyIHBycmFkZXNoMREwDwYDVQQHDAh2YXJhbmFzaTEWMBQG
A1UECgwNZ2Vla3Nmb3JnZWVrczETMBEGA1UECwwKZ2Vla3MgdGVhbTEPMA0GA1UE
AwwGcGVvcGxlMR0wGwYJKoZIhvcNAQkBFg51c2VyQGdlZWtzLmNvbTAeFw0yMDA4
MzExODI2MjRaFw0yMDEyMTcxODI2MjRaMIGWMQswCQYDVQQGEwJ4eDEXMBUGA1UE
CAwOdXR0YXIgcHJyYWRlc2gxETAPBgNVBAcMCHZhcmFuYXNpMRYwFAYDVQQKDA1n
ZWVrc2ZvcmdlZWtzMRMwEQYDVQQLDApnZWVrcyB0ZWFtMQ8wDQYDVQQDDAZwZW9w
bGUxHTAbBgkqhkiG9w0BCQEWDnVzZXJAZ2Vla3MuY29tMIIBIjANBgkqhkiG9w0B
AQEFAAOCAQ8AMIIBCgKCAQEA6LqzqnM6jmxF3vK7tBQo10rnJnLaox5U2MCds2WF
tnQY3NTdcFA/nLWkis+yacymBEXVFyBk9wWk91gTMnDG5vRgE/k47x2YjRinbnuC
zlFtozXYNaZE44oT3r1pTrJ7EvK2QyAV8iKtvSwzWm4AJz+/Fbs69FKpPzbGbRX8
aYalfD/D2m/OBFQRyYJNKKCJT/BTleXrk9gfEtGo9OWz5cMiqgRZMdoZ9ZqmwAUf
mShOWs63KyA37ncpS3LdMy2bz5sypBdrNVRGcoGIX7QqHGQVYd2YcDpJVdOk/or2
xTfTLhUtVKNrT9C11i95FK4C+qUma5f/BmTFfjJYwvC2mwIDAQABo1MwUTAdBgNV
HQ4EFgQU2czXSXWN7NfuYuTmhoQ4mwLuIf0wHwYDVR0jB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KEY-----
"
}

参考: https://www.php.net/manual/en/function.openssl-pkcs12-export.php




相关用法


注:本文由纯净天空筛选整理自shubham_singh大神的英文原创作品 PHP openssl_pkcs12_export() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。