简体   繁体   English

是否可以将rsa公钥转换为x509公钥?

[英]Is it possible to convert rsa public key to x509 public key?

I have following public key: 我有以下公共密钥:

-----BEGIN RSA PUBLIC KEY-----
... key ...
-----END RSA PUBLIC KEY-----

PHP can't work with that public key. PHP无法使用该公钥。 I found that it should be in x509 format to be usable in php. 我发现它应该是x509格式才能在php中使用。 Is it possible to convert this key to x509 format? 是否可以将此密钥转换为x509格式? As i understand result should look like: 据我了解,结果应如下所示:

-----BEGIN PUBLIC KEY-----
... changed? key ...
-----END PUBLIC KEY-----

UPD: Exactly it was necessary to generate x509 certificate, not just public key. UPD:确实有必要生成x509证书,而不仅仅是公钥。

phpseclib, a pure PHP RSA library , can work with RSA keys of that format just fine. phpseclib,一个纯PHP RSA库 ,可以与该格式的RSA密钥配合使用。

That said, with regard to openssl, you are right - RSA public keys need to be encapsulated in X.509 certs to work with most openssl_* functions. 就是说,对于openssl,您是对的-RSA公钥需要封装在X.509证书中,才能与大多数openssl_ *函数一起使用。

From the phpseclib interoperability documentation : phpseclib互操作性文档中

// openssl_get_publickey() only creates public key resources from X.509
// certificates hence our creating one
$dn = array();  // use defaults
$res_privkey = openssl_pkey_get_private($privkey);
$res_csr = openssl_csr_new($dn, $res_privkey);
$res_cert = openssl_csr_sign($res_csr, null, $res_privkey, 365);
openssl_x509_export($res_cert, $str_cert);
$res_pubkey = openssl_get_publickey($str_cert);

That said the result shouldn't look like what you posted but rather like this: 就是说结果看起来不应该像您发布的那样,而是这样的:

-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM