繁体   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