1

I tried to implement rsa public key system. the server was implemented with node-js and using node-rsa library to encrypt/decrypt rsa.

and the client was implemented with java.

in authentification part, the client(java) encrypt client's id and password with the public key of the server, and post to server with http post.

so server could receive them perfectly, but in node-rsa decrypt function, nothing occurs and http request denied with 500

client code below

public static boolean REQ_AUTH(String user_id, String user_pw )
{
    InputStreamReader ret = null;
    try
    {
        JSONObject obj = new JSONObject();
        obj.put("user_id", "waps12b");
        obj.put("user_pw", "password");

        String cipher = VoteUtility.EncryptRSA(obj.toString());
        ret = PostHTTP(API_URL.AUTH, "cipher=" + cipher);
        JSONParser parser = new JSONParser();
        JSONObject json = (JSONObject)parser.parse(ret);
        ret.close();


        String result = (String)json.get(JSON_KEY.Result);
        if(result.equals("FALSE"))
            return false;

        VoteUtility.Setting((String)json.get(JSON_KEY.Kp));
        RN = (String)json.get(JSON_KEY.RN);
        return true;
    }catch(Exception ex)
    {
        ex.printStackTrace();
    }
    return false;
}

server code below

router.all('/auth', function(req, res){
    var cipher = req.body.cipher;
    console.log('[cipher] : ' + cipher);

    var buf = new Buffer(cipher,'hex');
    console.log('[buf] : ' + buf.toString('hex'));

    var decrypted = key.decrypt(buf);
    console.log('[plain] : ' + decrypted);
}

and server log below

[cipher] : 46641844ffffff8d18ffffffb6ffffff8d6fffffffcf37ffffffd3ffffffa520721407ffffffbf7810ffffffc87d7925ffffffae16ffffffc9620f356872ffffff892828ffffffb533ffffffb324ffffffffffffffeefffffffa6b78ffffff8effffffb1ffffffb3ffffffdd681affffffae405d105affffff9626ffffff85fffffff8ffffffc9fffffff22c69ffffffa87efffffff8ffffffe64e082fffffffd247500f176dffffffedffffffcc6c5affffffc712ffffff9136ffffffbe26672b206cffffffa56dffffffa4ffffff85ffffffc0ffffffff0b6936fffffffb61ffffff8a0f3effffff8effffff965d5851ffffffaeffffff9dffffffb1417c57ffffffbfffffffee5affffff80ffffff9bffffffac0bffffff9cffffffaf6377327d
[buf] : 46641844ffffff8d18ffffffb6ffffff8d6fffffffcf37ffffffd3ffffffa520721407ffffffbf7810ffffffc87d7925ffffffae16ffffffc9620f356872ffffff892828ffffffb533ffffffb324ffffffffffffffeefffffffa6b78ffffff8effffffb1ffffffb3ffffffdd681affffffae405d105affffff9626ffffff85fffffff8ffffffc9fffffff22c69ffffffa87efffffff8ffffffe64e082fffffffd247500f176dffffffedffffffcc6c5affffffc712ffffff9136ffffffbe26672b206cffffffa56dffffffa4ffffff85ffffffc0ffffffff0b6936fffffffb61ffffff8a0f3effffff8effffff965d5851ffffffaeffffff9dffffffb1417c57ffffffbfffffffee5affffff80ffffff9bffffffac0bffffff9cffffffaf6377327d
POST /api/auth 500 421.025 ms - 1158    

how can i fix it?

neubert
  • 15,947
  • 24
  • 120
  • 212
user3867261
  • 109
  • 1
  • 2
  • 7
  • Can you surround your server code in a `try-catch` block and tell us what's the error exactly is? I think you may broke the consistency of key-pair, which means you are not using the same private key from the key-pair to decrypt the cipher text who's(the key-pair) public key was used to encrypt the text. – mazhar islam Jun 08 '15 at 18:29

1 Answers1

0

try it in node js

const NodeRSA   = require('node-rsa');
const key       = new NodeRSA('-----BEGIN RSA PRIVATE KEY-----PRIVATE_key----- 
END RSA PRIVATE KEY-----");
const text = 'helo or array';
const encrypted = key.encrypt(text, 'base64');
console.log('encrypted: ', encrypted);
hamedkke
  • 47
  • 1
  • 3