定义用法
openssl_pkey_get_public() 函数将返回公钥。相关请看 OpenSSL 函数。
描述
函数 openssl_pkey_get_public() 从给定证书返回公钥,以便它可以与其他函数一起使用。
语法
openssl_pkey_get_public ( mixed $certificate ) : resource
参数
参数 | 描述 |
---|---|
certificate |
您可以使用以下证书: 1. 一个 X.509 证书资源2. 文件格式为 file://path/to/file.pem 的文件中的公钥。 3. PEM 格式的公钥。 |
返回值
如果没有错误,PHP openssl_pkey_get_public() 函数会返回一个正的资源标识符。如果失败,它将返回 false。
PHP 版本
此函数将在 PHP 版本高于 5.0.0 的情况下工作。
示例 1
使用 X.509 证书的 openssl_pkey_get_public() 工作 -
<?php
$dn = array(
"countryName" => "CN",
"stateOrProvinceName" => "gouzhou",
"localityName" => "test1",
"organizationName" => "test2",
"organizationalUnitName" => "test3",
"commonName" => "www.qikepu.com",
"emailAddress" => "xyz@qikepu.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($res_cert, $x_509_certificate);
echo $res_pubkey = openssl_pkey_get_public($x_509_certificate);
?>
这将产生以下结果 -
Resource id #5
示例 2
使用 .pem 文件的 openssl_pkey_get_public() 的工作 −
<?php
$dn = array(
"countryName" => "CN",
"stateOrProvinceName" => "gouzhou",
"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:/htdocs/openssl/x_509.pem');
echo $res_pubkey = openssl_pkey_get_public(file_get_contents('C:/htdocs/openssl/x_509.pem'));
?>
这将产生以下结果 -
Resource id #7