0

Is there a way to convert RSA PEM Public key to XML without using 3rd party library.

    -----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEDtIRT57TJAfmub2RsIM32jdo
8ijsds/u1fpY6hwtkC01/LFJkNTXqSwvpaO5tp86o0SlzBHdF0WxPtsKqdc8F7kQ
uHm7hUTLX0zPGRdGCsy9q/PIGlVGAFTBSVXl+grmGGZuS1CHI13L/oulBGENQOxO
8r6D1RyPjt6z0BAndQIDAQAB
-----END PUBLIC KEY-----

output should be in this format

<RSAKeyValue><Modulus>xA7SEU+e0yQH5rm9kbCDN9o3aPIo7HbP7tX6WOocLZAtNfyxSZDU16ksL6WjubafOqNEpcwR3RdFsT7bCqnXPBe5ELh5u4VEy19MzxkXRgrMvavzyBpVRgBUwUlV5foK5hhmbktQhyNdy/6LpQRhDUDsTvK+g9Ucj47es9AQJ3U=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>

i tried to implemented this

$publicKey = -----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEDtIRT57TJAfmub2RsIM32jdo
8ijsds/u1fpY6hwtkC01/LFJkNTXqSwvpaO5tp86o0SlzBHdF0WxPtsKqdc8F7kQ
uHm7hUTLX0zPGRdGCsy9q/PIGlVGAFTBSVXl+grmGGZuS1CHI13L/oulBGENQOxO
8r6D1RyPjt6z0BAndQIDAQAB
-----END PUBLIC KEY-----;
$this->load->dbutil();
$this->output->set_content_type('text/xml');
$dom = new DOMDocument("1.0");
$root = $dom->createElement("publicKey");
$dom->appendChild($root);
$marker = $dom->createElement("marker");
         $root->appendChild($marker);
echo $dom->saveXML();

can anyone know the answer. Thanks in advance

MJB
  • 3
  • 2

1 Answers1

1

Cryptography is complex math and 99% of us are not capable to understand the core. So it's not just a simple XML.

Do not try to implement your own solutions to an already known and solved problem.

Use a proven library

For example phpseclib

use phpseclib3\Crypt\RSA;
class Home extends BaseController
{
    public function index()
    {
        $key = RSA::load(file_get_contents('key.pem'));
        echo $key->toString("XML"); //this gives you Modulus/Exponent XML string
    }
}
Ergec
  • 11,608
  • 7
  • 52
  • 62
  • how to get modulus n from public key...It's provide me Modulus n and also Exponent e together...how to get it individual – MJB Jan 13 '22 at 05:08
  • You can parse XML output using one of these libraries https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php/3577662#3577662 – Ergec Jan 13 '22 at 06:39