phpnode.jsencryptionrsajcryption

node.js implementation of jCryption 3.0 server-side part


Did anyone implement server-side part of jCryption 3.0 plugin with node.js? Author uses PHP for server, here's code on GitHub. I'm struggling with handshake handler, somehow I cannot decrypt request's base64 key with my private PEM key (I use 'ursa' module for RSA). Here's my handler (it's not completely finished in challenge part):

var ursa = require('ursa');
...
// write public key to HTML with EJS 
exports.getPublicKeyMiddleware = function(req, res, next){
    res.publicKey = req.app.get('publicKey'); // stores result of readFile(PUBLIC_KEY.PEM)
    next();
};

exports.handshake = function(req, res, next) {
    var base64key = req.body.key;
    var privateKey;
    var challenge;

    if (!!base64key) {
        myPrivateKey = ursa.createPrivateKey(req.app.get('privateKey'));

        try {
            challenge = privateKey.decrypt(base64key, 'base64', 'utf8');
            res.json({challenge: challenge});

        }
        catch (e) {
            res.json({error: 'Error decoding key'});
            console.log(e.message);
        }

     }
     else {
        res.json({error: 'No key in request'})
     }
}

Now it's always an error when decrypt. Like this:

Error: error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error

Can you please look at PHP workflow (link above) and point me what I am doing wrong maybe? Thanks

EDIT: Like HazA said padding was the case:

myPrivateKey.decrypt(base64key, 'base64', 'utf8', ursa.RSA_PKCS1_PADDING);

Solution

  • Did you try using the other Padding

    RSA_PKCS1_PADDING instead of RSA_PKCS1_OAEP_PADDING

    Like described in the documentation

    https://github.com/Obvious/ursa#decryptbuf-bufencoding-outencoding-padding