Take the 2-minute tour ×
Information Security Stack Exchange is a question and answer site for Information security professionals. It's 100% free, no registration required.

I need to generate a RSA key pair with Javascript. This key pair will be used for encrypting a one time blob of data (20-100kb), then discarded. This data can be reduced to a couple of hundred kb or less if I only encrypt data that needs to be secure and not other resources such as image files for the application.

Should I write my own implementation or are there any libraries that I can trust to be both stable and secure?

share|improve this question
Definitely do not write your own implementation. Just don't. Better to redesign your whole application, than to write your own implementation... –  AviD Jul 26 '11 at 5:40

1 Answer 1

up vote 2 down vote accepted

You could take a look at the GWT Crypto package. Its a port of some of the BouncyCastle stuff into GWT, so you should be able to take the compiled output and massage it a bit so you can call it from your current Javascript. It currently supports RSA keypair generation and encryption/decryption, as well as AES and DES. I think that's about it though.

I personally have not used this library, but since it was ported from BC it should be safer than writing your own implementation. That said, I dont know where they sourced their SecureRandom implementation from. You may need to look to see if its actually a cryptographically secure implementation or not. I took a quick look and they are just doing something with SHA and the seed bytes, but I dont know if that qualifies or not.

One last thing. I have found using RSA to encrypt large amounts of data to be quite slow. Perhaps you may want to use a hybrid solution of some sort where you encrypt an AES256 key using the RSA keypair and then use AES to encrypt the data? Could be faster...

share|improve this answer
I can change my code to just encrypt the parts of the package that differ between clients (ie. the secure stuff). How do I know BouncyCastle is secure? –  Casebash Jul 26 '11 at 5:00
Aside from paying someone you trust to inspect it? I suppose you dont. However, BouncyCastle is the crypto provider in Google's Android. Perhaps if its good enough for them its good enough for you? That's really a tough question and I dont know what right answer is. As far as I know it doesnt have any government certifications or anything... –  senecaso Jul 26 '11 at 5:13
It appears that the Google Web Toolkit is not really designed to support this use case: -groups.google.com/forum/#!topic/google-web-toolkit/2a6HnMJJ0ss. I could use GWT Exporter, but I think its best just to find a javascript library –  Casebash Jul 27 '11 at 5:38
Ya, like I said, you would have to "massage" the output. Still, its almost certainly safer than implementing your own! :) You could try jscryptolib, but I dont know how trusted it is, not to mention there hasnt been any activity on the project for ~2yrs. The other implementations that come up with a google search seem to be personal/student projects, so I dont know how much faith should be put into them being correct. –  senecaso Jul 27 '11 at 6:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.