![](/img/trans.png)
[英]Converting an OpenSSL generated RSA public key to OpenSSH format (PHP)
[英]Convert openssl key to rsa public key in php
根據 此示例,您可以在使用 openssl_pkey_new() 函數創建密鑰時直接獲取公鑰:
$config = array(
"digest_alg" => "es256",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$res = openssl_pkey_new($config);
openssl_pkey_export($res, $privKey, 'mypassphrase', $config);
$pubKey = openssl_pkey_get_details($res);
$pubKey = $pubKey["key"];
var_dump($privKey, $pubKey);
如果您直接擁有私鑰,則可以使用openssl_pkey_get_private()檢索公鑰:
$privKey = file_get_contents('/path/to/private.pem');
$res = openssl_pkey_get_private($privKey, 'mypassphrase');
$pubKey = openssl_pkey_get_details($res);
$pubKey = $pubKey["key"];
var_dump($privKey, $pubKey);
注意:如果 $res 為 false,您可能會收到openssl_error_string錯誤:
$sslError = '';
while ($msg = trim(openssl_error_string(), " \n\r\t\0\x0B\"")) {
if (substr($msg, 0, 6) === 'error:') {
$msg = substr($msg, 6);
}
$sslError .= "\n ".$msg;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.