Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
52
rated 0 times [  59] [ 7]  / answers: 1 / hits: 15637  / 9 Years ago, fri, february 27, 2015, 12:00:00

Using nodejs and only javascript, how do I extract a public key from a private key pem?



The private key that I have in hand is a PEM formatted private key; I'd like to extract the public key so I can distribute it to collaborators.



I regularly use the pure javascript node-forge module but have not yet discovered how to extract the public key from the private key.



I am also aware of, and presently use the ursa module to accomplish this; but I would like a pure javascript or pure nodejs solution if available.


More From » node.js

 Answers
15

from the node-forge documentation



pem = '-----PRIVATE KEY ----- [...]'
pki = require('node-forge').pki
privateKey = pki.privateKeyFromPem(pem)
publicKey = pki.setRsaPublicKey(privateKey.n, privateKey.e)
console.log(pki.publicKeyToPem(publicKey))

[#67642] Wednesday, February 25, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
citlalia

Total Points: 138
Total Questions: 104
Total Answers: 87

Location: Iceland
Member since Sat, Sep 17, 2022
2 Years ago
;