Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error handling in using Crypto in Node.js

I am using Crypto library of Node.js for encryption/decryption as follows :

    encrypt = function(text, passPhrase){
        var cipher = crypto.createCipher('AES-128-CBC-HMAC-SHA1', passPhrase);
        var crypted = cipher.update(text,'utf8','hex');
        crypted += cipher.final('hex');
        return crypted;
    } ,

    decrypt = function(text, passPhrase){
        var decipher = crypto.createDecipher('AES-128-CBC-HMAC-SHA1', passPhrase)
        var dec = decipher.update(text,'hex','utf8')
        dec += decipher.final('utf8');
        return dec;
    }

There is no problem with encryption part. and if I send the correct passPhrase for decryption there is also no problem. My problem is,if I send 'wrong' passPhrase for decryption, code breaks and throw an error :

TypeError: Bad input string
    at Decipher.Cipher.update (crypto.js:279:27)
    at module.exports.decrypt (/xxxx/yyyyy/jjj/ssss/encryptionService.js:19:28)
    at Object.module.exports.passwordDecryptor (/xxxx/yyyyy/jjj/ssss/encryptionService.js:59:56)
    at Object.<anonymous> (/xxxx/yyyyy/jjj/ssss/test.js:32:33)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)

I dont want it happen. I want for example decrypt function returns 'Passpharse is wrong' sentence. According to the documentation enter link description here createDecipher function doesn't accept a callback function.

like image 984
Danial Avatar asked Sep 15 '14 13:09

Danial


People also ask

How would you handle errors for async code in node JS?

If we want to handle the error for asynchronous code in Node. js then we can do it in the following two manners. Handle error using callback: A callback function is to perform some operation after the function execution is completed. We can call our callback function after an asynchronous operation is completed.

What are the best practice of error handling in js?

The most fundamental way of handling errors that have been thrown either manually or by the runtime is to catch them. Like most other languages, JavaScript offers a set of keywords to handle errors. It's essential to know each of them in-depth before you set down to handle errors in your JavaScript app.


1 Answers

I solved the problem with try and catch. (The callback function doesn't work.)

 decrypt = function(text, passPhrase){
        var decipher = crypto.createDecipher('AES-128-CBC-HMAC-SHA1', passPhrase);
        try {
            var dec = decipher.update(text,'hex','utf8');
            dec += decipher.final('utf8');
            return dec;
        } catch (ex) {
            console.log('failed');
            return;
        }
    }
like image 136
Danial Avatar answered Sep 25 '22 20:09

Danial