openssl_pkey_get_public()函數(shù)用于從證書中解析公鑰,以供使用。
openssl_pkey_get_public()函數(shù)將返回您的公鑰。
函數(shù)openssl_pkey_get_public()從給定證書中返回公鑰,以便可以與其他函數(shù)一起使用。
openssl_pkey_get_public ( mixed $certificate ) : resource
序號 | 參數(shù) | 描述 |
---|---|---|
1 | certificate | 您可以使用以下證書: 1. X.509證書資源 2.來自文件的公鑰,格式為file://path/to/file.pem。 3. PEM格式的公鑰。 |
如果沒有錯誤,PHP openssl_pkey_get_public()函數(shù)將返回一個正資源標(biāo)識符。如果失敗,它將返回false。
此函數(shù)將從大于5.0.0的PHP版本開始工作。
使用X.509證書的openssl_pkey_get_public()的工作-
<?php $dn = array( "countryName" => "IN", "stateOrProvinceName" => "Karnataka", "localityName" => "test1", "organizationName" => "test2", "organizationalUnitName" => "test3", "commonName" => "www.test.com", "emailAddress" => "xyz@test.com" ); // 生成新的私鑰/公鑰對 $privkey = openssl_pkey_new(); // Generate a certificate $csr = openssl_csr_new($dn, $privkey, array('digest_alg' => 'sha256')); $res_cert = openssl_csr_sign($csr, null, $privkey, 365); openssl_x509_export($res_cert, $x_509_certificate); echo $res_pubkey = openssl_pkey_get_public($x_509_certificate); ?>
輸出結(jié)果
Resource id #5
使用.pem文件的openssl_pkey_get_public()工作-
<?php $dn = array( "countryName" => "IN", "stateOrProvinceName" => "Karnataka", "localityName" => "test1", "organizationName" => "test2", "organizationalUnitName" => "test3", "commonName" => "www.test.com", "emailAddress" => "xyz@test.com" ); //生成新的私鑰/公鑰對 $privkey = openssl_pkey_new(); //生成證書 $csr = openssl_csr_new($dn, $privkey, array('digest_alg' => 'sha256')); $res_cert = openssl_csr_sign($csr, null, $privkey, 365); openssl_x509_export_to_file($res_cert, 'C:/xampp/htdocs/modules/openssl/x_509.pem'); echo $res_pubkey = openssl_pkey_get_public(file_get_contents('C:/xampp/htdocs/modules/openssl/x_509.pem')); ?>
輸出結(jié)果
Resource id #7