Home > Software design >  How to convert PEM format RSA key into XML format using PHP
How to convert PEM format RSA key into XML format using PHP

Time:06-01

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

?>

CodePudding user response:

The conversion of a PEM encoded key in X.509/SPKI format to XML format can be done with phpseclib as follows:

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. 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):

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

$res = openssl_pkey_new($config);

openssl_pkey_export($res, $privKey);

$pubKeyDetails = openssl_pkey_get_details($res);
$x509pem = $pubKeyDetails["key"];

// Key conversion with php
$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:

use phpseclib3\Crypt\RSA;

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