1

I want to be able to generate private/public RSA keys with javascript only...

So far I found http://www.hanewin.net/encrypt/rsa/rsa.htm, it includes the only Javascript RSA key generator I found. I created wrappers (modifying the code from the demo page (see link)), here's the one for the keys:

function genKeys() {
 rsaKeys(1024);
 var p=rsa_p;
 var q=rsa_q;
 var d=rsa_d;
 var u=rsa_u;
 var e=rsa_e;
 var pq=rsa_pq;

 return {"e": e, "p": p, "q": q, "pq": pq, "d": d, "u": u};
}

The problem with it is that the output is given as the actual numbers (exponent, modulus, etc.), but I need the key in the OpenSSL format (the base64 encoded thing). Can someone tell me what the format of the OpenSSL key data is, or even better - how do I convert a key generated with the code from that site into a OpenSSL key?

I want to use the result with pidcrypt (a js crypto library) and ruby/openssl...

Thank you very much for any suggestion...

neubert
  • 15,947
  • 24
  • 120
  • 212
apirogov
  • 1,296
  • 1
  • 12
  • 22

2 Answers2

1

The OpenSSL public key format is that defined by PKCS#1. The Base64 encoded version is a PEM format file, which is a Base64-encoded ASN.1 BER format.

ASN.1 isn't a particularly nice format to deal with, but it should be possible with enough elbow grease...

caf
  • 233,326
  • 40
  • 323
  • 462
  • This is one way to do it using openssl alone: https://stackoverflow.com/questions/34911103/generate-rsa-private-key-from-n-e-d-p-q-values-in-bash-with-openssl – mr.spuratic Jun 13 '18 at 09:48
1

Checkout the JavaScript opensource Forge project. It has methods for generating RSA keys and exporting them to PEM (the base64-encoded DER format you're talking about that OpenSSL uses).

http://github.com/digitalbazaar/forge/blob/master/README

dlongley
  • 2,078
  • 14
  • 17