z-hao-wang / react-native-rsa

React native rsa crypto
MIT License
68 stars 19 forks source link

Feature request: private key for encryption and public key for decryption #2

Closed xing-zheng closed 8 years ago

xing-zheng commented 8 years ago

Hi @z-hao-wang , I'm very interesting in and appreciate your project. But my scenario is:

I need encrypt some data on server, and decrypt them on client. So, the private key should be kept on server for encryption, and the public key will be distribute to any client for decryption.

But I found that, This project can only use public key for encryption and private key for decryption. It will not fit this scenario. Could you help to support this case.

z-hao-wang commented 8 years ago

Hi @xing-zheng, Looks like you need to store the private key on client. RSA works like this, you keep private key to yourself, and open you public key to everyone. So any one can send you encrypted message using public key, however this message can only be decrypted using your private key.

My solution is every client has it's own private key and public key. I store each client's public key in server database. Then if I need to send an encrypted message to a client, I need to look up its public key, then encrypt using its public key.

Another note is RSA can only encrypt certain size data, if you need to encrypt larger data, you must use something like AES. I use RSA to encrypt the AES cipher(password), then use AES to encrypt/decrypt the actual data blob.

xing-zheng commented 8 years ago

Thank you @z-hao-wang .

Thank you for your explanation. May be I haven't declare my requirement clearly.

I want to keep the private key on my server, and use the server to broadcast encrypted message to any client which hold the public key.

z-hao-wang commented 8 years ago

@xing-zheng

From my understanding, that is not possible with RSA. Encryption using private key is only for signing purpose. The message is still not encrypted but client can verify using public key that this message is truly from the server who holds the private key. see http://crypto.stackexchange.com/questions/2123/rsa-encryption-with-private-key-and-decryption-with-a-public-key

If you want to broadcast the same encrypted message, you would have to go with symmetric encryption method. Or using the method I purposed, you just encrypt the same message for each client (each client would get a special encrypted message using its public key). Then client use its private key to decrypt the message.

xing-zheng commented 8 years ago

Thank you @z-hao-wang . I've change the code on my side, Then it works for my scenario.

xing-zheng/react-native-rsa@6ebeaaee2379c51aaf119d9dac1180c4e1166175

kevinvn1709 commented 8 years ago

@xing-zheng: How can you use this library with .pem file?