1

I receive a RSA Key from C# server through httprequest,

But so many Encrypt lib need PEM format.

I find a solution at this

XML to PEM in Node.js

But I can't use it in my Javscript H5 Project, Have any other solution?

Farkas Joe
  • 13
  • 1
  • 6

1 Answers1

0

Your XML seems a propietary format

<RSAKeyValue> 
    <Modulus>1znidPBIcMcO7K/53tkTSyKqxlG5Mcws8kVtijS4tyEU4W/FEVWYpOtv+Stnb4Vt</Modulus>
    <Exponent>AQAB</Exponent>
</RSAKeyValue>

You will need a cryptographic library like forge to build a RSA public key from modulus and exponent. Based on this thread , you can use a code similar to this

// parse XML
var rsaKeyValue = ...

var BigInteger = forge.jsbn.BigInteger;
function parseBigInteger(b64) {
     return new BigInteger(forge.util.createBuffer(forge.util.decode64(b64)).toHex(), 16);
 }

 //Create a Forge public key from modulus and exponent
 var publicKey = forge.pki.setRsaPublicKey(
       parseBigInteger(rsaKeyValue.Modulus), // n
       parseBigInteger(rsaKeyValue.Exponent)); // e

 // convert a Forge public key to PEM-format
 var pem = forge.pki.publicKeyToPem(publicKey);
pedrofb
  • 37,271
  • 5
  • 94
  • 142
  • I'm glad to hear from you. Have any other cryptographic library? I can't use this,Because of the License :( – Farkas Joe Aug 15 '17 at 08:40
  • Forge is open source and requires GNU GPL v2 which is not overly restrictive. But if it is not suitable for you, try with [PKI.js](https://github.com/PeculiarVentures/PKI.js) which has its own license model. – pedrofb Aug 15 '17 at 09:21
  • Or use the standard Web Cryptograpy Api which is present in all modern browsers. In this case you need to convert the key to JWK format ( JsonWebKey)to import it, export it as `spki` ( raw data) and encode it as base64 to get the PEM format – pedrofb Aug 15 '17 at 09:28