Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert x509 DER encoded to PEM #246

Closed
jfromaniello opened this issue Apr 24, 2015 · 3 comments
Closed

Convert x509 DER encoded to PEM #246

jfromaniello opened this issue Apr 24, 2015 · 3 comments

Comments

@jfromaniello
Copy link

Hi, I am looking for a way to convert from DER to PEM, the equivalent of

openssl x509 -in test.der -inform DER -pubkey -subject
@dlongley
Copy link
Member

Using the public key specific API (which will set the appropriate PEM headers for you), the code looks about like this:

var forge = require('forge');
var fs = require('fs');

var der = fs.readFileSync('/publickey.der', {encoding: 'binary'});
var asn1 = forge.asn1.fromDer(der);
var publicKey = forge.pki.publicKeyFromAsn1(asn1);
var pem = forge.pki.publicKeyToPem(publicKey);

Doing generic DER to PEM, where you must set the PEM headers, etc.:

var forge = require('forge');
var fs = require('fs');

var der = fs.readFileSync('/publickey.der', {encoding: 'binary'});

var msg = {
  type: 'PUBLIC KEY', // you specify this header for the data
  body: der
};
var pem = forge.pem.encode(msg);

@jfromaniello
Copy link
Author

Thank you very much, it works like a charm!

@dlongley
Copy link
Member

Sure :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants