views:

621

answers:

2

I want to find the RSA code in both Javascript and Java code. And they can return the same result in encrypt and decrypt.

My purpose is: I can encrypt a message in the user's browser using Javascript (with the public key). After I can decrypt that message in my server (with private key).

I found on internet but Javascript and Java return difference result: if I encrypt using Javascript, I cannot decrypt using Java.

+6  A: 

This is not a good idea.

RSA public key encryption is suitable for encrypting a session key, not the entire message. It's too slow and it's susceptible to a man-in-the-middle attack when used directly.

Just use SSL and be done with it.

Steven Sudit
A: 

I am curious why the javascript and java had different results, as RSA isn't platform dependent, but, converting the key to a byte array can differ, so that could be your difficulty.

If you are encrypting a password then it may make sense to use RSA, as the number of bytes that can be encrypted/decrypted is related to the length of the key.

Where you found the source code for Java and Javascript would be useful to see, or at least to know how the keys were turned into byte arrays, and then the private or public keys were created from those.

James Black
James, you might want to take a look at this person's track record.
Steven Sudit
I dont know the reason. Maybe in JS and Java have the difference in the code. For example in java generating the public is only return one String result like: "MIGfMA0GCSqGSIb3DQEBAQUAA4GNAD.....". But in javascript, the key need 3 parameters for key: m,e,d. Like the following example. m = 1d7777c38863aec21ba2d91ee0faf51e = 5abbd = 1146bd07f0b74c086df00b37c602a0b(Please see the example: http://ohdave.com/rsa/ )
haicnpmk44
If you look at http://bouncycastle.org/docs/docs1.6/index.html, look at PublicKeyFactory and RSAPublicKeyStructure, and see that the encoding is ASN.1 encoded. The encoding is what is important, as that is how a key will be converted into something that can be sent over the Internet. In order to have the three things listed you can generate the public and private key, so javascript should never get it. You need to find a library using an encoded public key and then find a library that will use that encoding.
James Black