2

I want to convert DER content to PEM format using Nodejs.

I couldn't find any suitable library or logic. How can I achieve this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Gnik
  • 7,120
  • 20
  • 79
  • 129
  • If you find bindings to OpenSSL (or can make a shell/system call to run it) you can do a search on conversions using that. – Dave S Oct 26 '21 at 05:52
  • Why not [call the command](https://www.poftut.com/convert-der-pem-pem-der-certificate-format-openssl/) with [node](https://stackoverflow.com/questions/20643470/execute-a-command-line-binary-with-node-js) directly? – Fernando Bravo Diaz Oct 28 '21 at 22:04

1 Answers1

0

This is (now?) built directly into the Node.js crypto library.

const crypto = require( 'crypto' );

You can import a private key in DER format:

const key = crypto.createPrivateKey( { key: privkeyInDerFormat, format: 'der', type: 'pkcs8' } );

And then export it in PEM format:

const privkeyInPemFormat = key.export( { format: 'pem', type: 'pkcs8' } );

Done! :)

Jared Brandt
  • 203
  • 1
  • 7