0

I am encrypting text in node JS by using node-RSA and passing it to client(javascript), in which JSEncrypt library is using,but all the time the decrypted message is coming null. Public key and private Key is developing on nodeJS server, encrypting with Public key and decrypting on javascript side with Private Key .

This is not happening right!!!!

Can anyone tell which library i should use in javascript to decrypt the message coming from nodejs(using Node-RSA).OR any other IDEA!!

We are already using HTTPS but our use case is such that we have a broker between it.. and its not trusted broker, and we are forced to use it.. so we would like to use encryption decryption.. Although we have trusted people in our client side, so we are decrypting at client side.

11
  • Why do not you just use HTTPS? Your solution implies security risks: How are you going to send/store the private key in the browser? Are you going to use plain HTTP ? Commented Mar 21, 2017 at 13:19
  • We are already using HTTPS but our use case is such that we have a broker between it.. and its not trusted broker, and we are forced to use it.. so we would like to use encryption decryption.. Although we have trusted people in our client side, so we are decrypting at client side. Commented Mar 21, 2017 at 13:23
  • This is an important clarification. But if the broker is not trusted, you can not send the private key to client because broker could sniff the network. is that so? Commented Mar 21, 2017 at 13:32
  • 1
    I see your point. 1) to solve the decryption issue we need to see the code and the errors and probably the key format 2) if you are not sure if your library is suitable, you can check forge,jsrasign or the built-in WebCryptographyApi 3) i suggest to use the asymmetric encryption to exchange an AES symmetric key. Commented Mar 21, 2017 at 17:59
  • 1
    As pedrofb said, go with forge. It provides the same API for JS and node. This question is really off-topic by the way. Commented Mar 21, 2017 at 18:52

2 Answers 2

1

I used CryptoBrowserify to encrypt at javascript (client side)

import CryptoBrowserify from 'crypto-browserify';
 public encryptStringWithRsaPublicKey(data: string, publicKey: string): string {
       var encrypted = CryptoBrowserify.publicEncrypt( publicKey,new Buffer(data));
       return encrypted.toString('Base64');
   }

And crypto to dedcrypt at Nodejs

decrypt = function(privateKey, data) {
      var crypto = require('crypto');
        var buffer = new Buffer(data, 'base64');
        var decrypted = crypto.privateDecrypt(privateKey, buffer);
        return decrypted.toString('utf8')

  };
Sign up to request clarification or add additional context in comments.

3 Comments

I'm trying to achieve similar thing, but not working.I'm encrypting Pswd on client end, then the decrypt on NodeJS not working. Javascript: var user_pswd = CryptoJS.AES.encrypt('encryptMe', 'my secret'); NodeJS var crypto = require('crypto'); console.log("post req.body: ", req.body.un); // this shows encrypted message var decipher = crypto.createDecipher('aes', 'my secret'); var dec = decipher.update(req.body.un, 'hex', 'utf8'); dec += decipher.final('utf8'); console.log('Decrypted Username: ', dec); No output appears and programs just jams.
You are using AES not RsA. For AES, I think CryptoJS should work at client and server side. These sample code just work RSA
Yes you are right. I already have sorted out in same way (because I had no other option). Your answer is correct.
-1

Nodejs has its builtin cryto library,it is optimized and tested, recommend to use that: https://nodejs.org/api/crypto.html

1 Comment

The issue is that OP wants to interop with plain JavaScript which doesn't have the crypto module. I also doubt that the crypto module provides all the functionality that OP needs such as key generation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.