I am looking for a way to extract public key from certificate x509 (PEM format) in javascript like this one:
openssl x509 -in cert.cer -pubkey -noout > pub.txt
I am looking for a way to extract public key from certificate x509 (PEM format) in javascript like this one:
openssl x509 -in cert.cer -pubkey -noout > pub.txt
Share
Improve this question
edited Jun 21, 2017 at 16:01
user7605325
asked Jun 21, 2017 at 11:52
halloulaguesmihalloulaguesmi
931 gold badge1 silver badge8 bronze badges
3 Answers
Reset to default 3var cert = forge.pki.certificateFromPem(pem);
var pem =
forge.pki.publicKeyToPem(cert.publicKey)
Thanks halloulaguesmi. This seems to be working perfectly.
You need something that can parse ASN.1 structure. You could use pkijs.
Demo can be found here
After Node.js v15.6.0, you could use publicKey of X509Certificate
from crypto
module to retrieve the public key.
If you want to export publicKey, the export
could be used
For public keys, the following encoding options can be used:
- type: Must be one of 'pkcs1' (RSA only) or 'spki'.
- format: Must be 'pem', 'der', or 'jwk'.
Sample codes
const crypto = require("crypto")
const cert = new crypto.X509Certificate("pem file content"))
console.log(cert.publicKey.export({"type": "pkcs1", "format": "jwk"}));