最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Extract public key from certificate x509 - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 3
var 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"}));
发布评论

评论列表(0)

  1. 暂无评论