簡體   English   中英

在php中將openssl密鑰轉換為rsa公鑰

[英]Convert openssl key to rsa public key in php

有什么辦法/什么是轉換在PHP中生成的OpenSSL的關鍵,沒有直接的外殼使用,為RSA公鑰,我可以使用的東西了一起的最好辦法這樣對數據進行加密,我可以加密與我的私人鑰匙。

謝謝

根據 此示例,您可以在使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM