如何使用PHP将PEM格式RSA键转换为XML格式
我正在尝试将键对从PEM格式转换
-----BEGIN PUBLIC KEY-----
-----END PUBLIC KEY-----
为XML格式:
<RSAKeyValue>
<Exponent> </Exponent>
<Modulus> </Modulus>
</RSAKeyValue>
在我通过它生成键时仅使用OpenSSL可以使用?
NB:为了测试目的,我的钥匙存储在$ privkey和$ pubkey变量中,因此我希望能够$ echo $ echo xml格式密钥,而不是暂时不将其存储到文件中。
NB':我尝试使用phpseclib与一个景象一起找到在这里但是 给了我这个错误“未被发现的错误:类“ basecontroller”
它
。
<?php
$config = array
(
'config' => 'C:\xampp\htdocs\crypto\openssl.cnf',
'default_md' => 'sha512',
'private_key_bits' => 4096,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
);
$keypair = openssl_pkey_new($config);
openssl_pkey_export($keypair, $privKey, null, $config);
$publickey = openssl_pkey_get_details($keypair);
$pubKey = $publickey['key'];
use phpseclib3\Crypt\RSA;
echo $pubKey->toString("XML");
echo "$privKey";
?>
I'm trying to convert key pair from PEM format :
-----BEGIN PUBLIC KEY-----
-----END PUBLIC KEY-----
Into XML format :
<RSAKeyValue>
<Exponent> </Exponent>
<Modulus> </Modulus>
</RSAKeyValue>
Is it possible using only openssl as I generate the keys through it ?
nb : my keys are stored into $privKey and $pubKey variable for test purpose, so I want to be able to $echo the XML format key and not store it into a file for the moment.
nb' : I have tried using phpseclib with an exemple found here but it gives me this error "Uncaught Error: Class "BaseController" not found in ..."
Thanks for your help
Here is the PHP code :
<?php
$config = array
(
'config' => 'C:\xampp\htdocs\crypto\openssl.cnf',
'default_md' => 'sha512',
'private_key_bits' => 4096,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
);
$keypair = openssl_pkey_new($config);
openssl_pkey_export($keypair, $privKey, null, $config);
$publickey = openssl_pkey_get_details($keypair);
$pubKey = $publickey['key'];
use phpseclib3\Crypt\RSA;
echo $pubKey->toString("XML");
echo "$privKey";
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以使用phpseclib进行X.509/SPKI格式中的PEM编码键转换为XML格式,如下所示:
输出为:
对于密钥生成,可以像代码中的密钥生成OpenSSL一起使用。但是,必须在phpseclib零件中导入导出的PEM密钥,如上所述(代码中缺少此导入):
另外,也可以使用phpseclib进行密钥生成:
The conversion of a PEM encoded key in X.509/SPKI format to XML format can be done with phpseclib as follows:
The output is:
For key generation OpenSSL can be used as in your code. However, the exported PEM key must be imported in the phpseclib part as shown in the code above (this import is missing in your code):
Alternatively, also key generation can be done with phpseclib: