javascriptjavanode.jsencryptionjceks

Decrypting a token in NodeJS with a JCEKS keystore


In this scenario I'm getting token encrypted at origin with the one secret key in a JCEKS keystore. The token goes into a NodeJS server, where it should be unencrypted to use the data in there. As far as I understand, the private key to decrypt the token cannot be extracted from that format, so decrypting it simply using the crypto module won't do. I've played around with node-keytool, and gotten as far as listing the keys, based on the example here: https://github.com/FrankGrimm/node-keytool/blob/master/examples/listcontent.js.

However, I'm at a complete loss as how to use that module to decrypt the token. Any suggestions on how to do so, whether using that module or another, would be greatly appreciated.


Solution

  • It seems like it can't be done, or at least not without a lot of work. Easier to write a decrypter in Java and then make a module in Node, like so:

    const exec = require('child_process').exec; const fs = require('fs');

    const start = function (data, resolve, reject) {
        let child = exec('java -jar java/out/artifacts/decrypter_jar/decrypter.jar ' + data,
        function (error, stdout, stderr){
            console.log('Output -> ' + stdout);
            if(stdout){
                resolve(stdout);
            }
            else {
                reject('Empty response from JAR');
                return;
            }
            if(error !== null){
                console.log("Error -> "+error);
                reject(error);
            }
        });
    
    
    };
    
    module.exports = {start};