13

Below code is working

var crypto = require('crypto');
var cipher = crypto.createCipher('aes-128-cbc','abcdefghijklmnop')
var http = require('http')

var userStr = 'a134aad';
var crypted = cipher.update(userStr, 'utf8', 'hex');
crypted += cipher.final('hex');
console.log(crypted);

But when put into a server callback it doesn't work, and throws below err when a request arriving, and node is crush:

http.createServer(function(req, res){
    var userStr = 'a134aad';
    var crypted = cipher.update(userStr, 'utf8', 'hex');
    crypted += cipher.final('hex');
    console.log(crypted);

    res.end('hello');
}).listen(9888)

---------------------------------

7364aee753f0568f7e5171add6868b75
crypto.js:170
  var ret = this._handle.update(data, inputEncoding);
                         ^
Error: Trying to add data in unsupported state
    at Cipher.update (crypto.js:170:26)
    at Server.<anonymous> (C:\Users\58\Desktop\sha256.js:12:26)
    at emitTwo (events.js:126:13)
    at Server.emit (events.js:214:7)
    at parserOnIncoming (_http_server.js:602:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:117:23)
jiajianrong
  • 868
  • 8
  • 24

3 Answers3

16

Turns out

var cipher = crypto.createCipher('aes-128-cbc','abcdefghijklmnop')

should not be reused. I put it into the server callback too and the problem is solved.

jiajianrong
  • 868
  • 8
  • 24
  • 2
    I would add that the error is throws due the `final()` call https://nodejs.org/api/crypto.html#crypto_cipher_final_outputencoding This answer should be accepted – Manuel Spigolon Mar 26 '19 at 12:18
8

Check this out

Thats mainly because every time we run the encrypt or decrypt we should repeat crypto.createCipher('aes192', secrateKey); and crypto.createDecipher('aes192', secrateKey);

let secrateKey = "secrateKey";
const crypto = require('crypto');


function encrypt(text) {
    const encryptalgo = crypto.createCipher('aes192', secrateKey);
    let encrypted = encryptalgo.update(text, 'utf8', 'hex');
    encrypted += encryptalgo.final('hex');
    return encrypted;
}

function decrypt(encrypted) {
    const decryptalgo = crypto.createDecipher('aes192', secrateKey);
    let decrypted = decryptalgo.update(encrypted, 'hex', 'utf8');
    decrypted += decryptalgo.final('utf8');
    return decrypted;
}

let encryptedText = encrypt("hello");
console.log(encryptedText);

let decryptedText = decrypt(encryptedText);
console.log(decryptedText);

Hope this helps!

Ashley Davis
  • 9,896
  • 7
  • 69
  • 87
Noushad Ali
  • 167
  • 1
  • 9
  • Although this is a good example, the function `createCipher` is now deprecated in favour of `createCipheriv`. A nice example can be found here: https://stackoverflow.com/a/60370205/25868 – Ashley Davis Mar 05 '23 at 08:16
0

You need to create cipher before using it. like:

let cipher = crypto.createCipher("aes-256-cbc", key, iv);

key = can be any random string. (i prefer to use 32-bit long because you might get an error for not exact 32-bit long key)

iv = initialization vector.

both key and iv need to in utf-8.

checkout documentation: https://nodejs.org/api/crypto.html#crypto_crypto_createcipheriv_algorithm_key_iv_options

so, the final code looks like this:

http.createServer(function(req, res){
var userStr = 'a134aad';
let key = "123456781234567812345678";
let iv= crypto.randomBytes(16);
let cipher = crypto.createCipher("aes-256-cbc", key, iv);
var crypted = cipher.update(userStr, 'utf8', 'hex');
crypted += cipher.final('hex');
console.log(crypted);

res.end('hello');
}).listen(9888)

**Also, if you find any mistakes, feel free to correct me!