简体   繁体   English

如何使用 PHP 将 PEM 格式的 RSA 密钥转换为 XML 格式

[英]How to convert PEM format RSA key into XML format using PHP

I'm trying to convert key pair from PEM format :我正在尝试从 PEM 格式转换密钥对:

-----BEGIN PUBLIC KEY----- 

-----END PUBLIC KEY-----

Into XML format :转成 XML 格式:

<RSAKeyValue>
<Exponent> </Exponent>
<Modulus> </Modulus>
</RSAKeyValue>

Is it possible using only openssl as I generate the keys through it ?当我通过它生成密钥时,是否可以只使用 openssl ?

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.注意:我的密钥存储在 $privKey 和 $pubKey 变量中用于测试目的,所以我希望能够 $echo XML 格式的密钥,而不是将其存储到文件中。

nb' : I have tried using phpseclib with an exemple found here but it gives me this error "Uncaught Error: Class "BaseController" not found in ..." nb':我曾尝试使用 phpseclib 并在此处找到一个示例,但它给了我这个错误“未捕获的错误:在...中找不到类“BaseController””

Thanks for your help谢谢你的帮助

Here is the PHP code :这是PHP代码:

<?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";

?>

The conversion of a PEM encoded key in X.509/SPKI format to XML format can be done with phpseclib as follows:可以使用 phpseclib 将 X.509/SPKI 格式的 PEM 编码密钥转换为 XML 格式,如下所示:

use phpseclib3\Crypt\PublicKeyLoader;

$x509pem = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAunF5aDa6HCfLMMI/MZLT
5hDk304CU+ypFMFiBjowQdUMQKYHZ+fklB7GpLxCatxYJ/hZ7rjfHH3Klq20/Y1E
bYDRopyTSfkrTzPzwsX4Ur/l25CtdQldhHCTMgwf/Ev/buBNobfzdZE+Dhdv5lQw
KtjI43lDKvAi5kEet2TFwfJcJrBiRJeEcLfVgWTXGRQn7gngWKykUu5rS83eAU1x
H9FLojQfyia89/EykiOO7/3UWwd+MATZ9HLjSx2/Lf3g2jr81eifEmYDlri/OZp4
OhZu+0Bo1LXloCTe+vmIQ2YCX7EatUOuyQMt2Vwx4uV+d/A3DP6PtMGBKpF8St4i
GwIDAQAB
-----END PUBLIC KEY-----';

$publicKey = PublicKeyLoader::load($x509pem);   // import public PEM key
$xmlFormattedKey = $publicKey->toString("XML"); // export public XML key
print($xmlFormattedKey);

The output is:输出是:

<RSAKeyValue>     
    <Modulus>unF5aDa6HCfLMMI/MZLT5hDk304CU+ypFMFiBjowQdUMQKYHZ+fklB7GpLxCatxYJ/hZ7rjfHH3Klq20/Y1EbYDRopyTSfkrTzPzwsX4Ur/l25CtdQldhHCTMgwf/Ev/buBNobfzdZE+Dhdv5lQwKtjI43lDKvAi5kEet2TFwfJcJrBiRJeEcLfVgWTXGRQn7gngWKykUu5rS83eAU1xH9FLojQfyia89/EykiOO7/3UWwd+MATZ9HLjSx2/Lf3g2jr81eifEmYDlri/OZp4OhZu+0Bo1LXloCTe+vmIQ2YCX7EatUOuyQMt2Vwx4uV+d/A3DP6PtMGBKpF8St4iGw==</Modulus>
    <Exponent>AQAB</Exponent>
</RSAKeyValue>

For key generation OpenSSL can be used as in your code.对于密钥生成,OpenSSL 可以在您的代码中使用。 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):但是,导出的 PEM 密钥必须在 phpseclib 部分中导入,如上面的代码所示(您的代码中缺少此导入):

// Key generation with OpenSSL
$config = array(
    "private_key_bits" => 2048,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
);

$res = openssl_pkey_new($config);                // create key resource using $config 
//openssl_pkey_export($res, $privKey);           // export private key (PEM encoded, PKCS#8 format); not required for this example
$pubKeyDetails = openssl_pkey_get_details($res);
$x509pem = $pubKeyDetails["key"];                // export public key (PEM encoded, X.509 format)

// Key conversion with phpseclib
$publicKey = PublicKeyLoader::load($x509pem);   // import public PEM key generated with OpenSSL
$xmlFormattedKey = $publicKey->toString("XML"); // export public XML key 
print($xmlFormattedKey);

Alternatively, also key generation can be done with phpseclib:或者,也可以使用 phpseclib 生成密钥:

use phpseclib3\Crypt\RSA;

$privateKey = RSA::createKey(2048);                              // generate private key
$xmlFormattedKey = $privateKey->getPublicKey()->toString("XML"); // export public XML key
print($xmlFormattedKey);

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

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