How to convert RSA key numbers into OpenSSL-compatible keys?

1.9k views Asked by At

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...

2

There are 2 answers

1
caf On BEST ANSWER

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...

0
dlongley On

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