There are libraries written for both browser and node.js
works very good. You can find them here.
Include the files and try this func,
function RSAModulusAndExponent(pubkey) {
var unarmor = /-----BEGIN PUBLIC KEY-----([A-Za-z0-9+\/=\s]+)-----END PUBLIC KEY-----/;
try{
var pubkeyAsn1 = ASN1.decode(Base64.decode(unarmor.exec(pubkey)[1]));
var modulusRaw = pubkeyAsn1.sub[1].sub[0].sub[0];
var modulusStart = modulusRaw.header + modulusRaw.stream.pos + 1;
var modulusEnd = modulusRaw.length + modulusRaw.stream.pos + modulusRaw.header;
var modulusHex = modulusRaw.stream.hexDump(modulusStart, modulusEnd);
var modulus = Hex.decode(modulusHex);
var exponentRaw = pubkeyAsn1.sub[1].sub[0].sub[1];
var exponentStart = exponentRaw.header + exponentRaw.stream.pos;
var exponentEnd = exponentRaw.length + exponentRaw.stream.pos + exponentRaw.header;
var exponentHex = exponentRaw.stream.hexDump(exponentStart, exponentEnd);
var exponent = Hex.decode(exponentHex);
return { success:true, msg:{moduls: modulus, exponent: exponent}};
}
catch(err){ console.log(err)
return { success:false, msg:"Failed validating RSA public key."};
}
}
There you have your modulus and exponent.
For public key validation on node, I would recommend
node-jose
(or)
NodeRSA
const key = new NodeRSA(pubKey);
if(key.isPublic && !key.isEmpty() && key.getKeySize() > 0)
return {error: false, msg : "RSA Public key validation success! (Reason, isPublic?"+key.isPublic()+", isEmpty?"+key.isEmpty()+", Key length : " + key.getKeySize()+" )"}
else
return {error: false, msg : "RSA Public key validation failed! (Reason, isPublic?"+key.isPublic()+", isEmpty?"+key.isEmpty()+", Key length : " + key.getKeySize()+" )"}
I just laid out an idea. There are other functions in the library which is sufficient enough to make sure the public key is okay.
Also, you have node on command line. you can see a raw format using openssl. see more here,OpenSSL Manual
openssl asn1parse -in your_public.key