WiseWins - 2 months ago 17

Javascript Question

I have a modulus and private exponent. How can I generate an RSA private Key in JavaScript from that?

Or if these values are not enough to generate a RSA private Key. What are the other values that I might possibly need.

RSAPrivateKeySpec in Java 7 is able to generate RSA private key with mod and private Exp. I'm looking for a equivalent in JavaScript.

I also tried npm ursa module which unfortunately didn't work out.

Answer

JSBN is able to create an RSA private key which can decrypt.

```
var sk = new RSAKey();
sk.setPrivate("<modulus hex>", "<public exponent hex>", "<private exponent hex>");
var plaintext = sk.decrypt("<ciphertext hex>");
```

The public exponent is not used during decryption, so you can simply pass some garbage hex into it, but it's either "03" (3), "11" (17) or "010001" (65537).

You'd need to include jsbn.js, jsbn2.js, rsa.js, and rsa2.js. Keep in mind that decryption will be 4 times slower than with a full RSA key, because the Chinese Remainder Theorem cannot be used.

Source (Stackoverflow)