如何将 RSA PlblicKey XML 转换为 PEM 格式
How can I convert RSA PlblicKey XML to PEM format
我通过 httprequest 从 C# 服务器收到一个 RSA 密钥,
但是这么多加密库需要PEM格式。
我在这里找到解决方案
XML to PEM in Node.js
但是我不能在我的 Javscript H5 项目中使用它,
还有其他解决办法吗?
您的 XML 似乎是专有格式
<RSAKeyValue>
<Modulus>1znidPBIcMcO7K/53tkTSyKqxlG5Mcws8kVtijS4tyEU4W/FEVWYpOtv+Stnb4Vt</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
您将需要像forge to build a RSA public key from modulus and exponent. Based on this thread 这样的密码库,您可以使用类似于此的代码
// 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);
我通过 httprequest 从 C# 服务器收到一个 RSA 密钥,
但是这么多加密库需要PEM格式。
我在这里找到解决方案
XML to PEM in Node.js
但是我不能在我的 Javscript H5 项目中使用它, 还有其他解决办法吗?
您的 XML 似乎是专有格式
<RSAKeyValue>
<Modulus>1znidPBIcMcO7K/53tkTSyKqxlG5Mcws8kVtijS4tyEU4W/FEVWYpOtv+Stnb4Vt</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
您将需要像forge to build a RSA public key from modulus and exponent. Based on this thread 这样的密码库,您可以使用类似于此的代码
// 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);