decipher.update上的加密错误

时间:2014-09-15 16:09:13

标签: node.js encryption middleware node-crypto

我正在为一个Node项目编写一个javascript模块。

我收到了一个非常含糊的错误

protocol\Parser.js:82
     throw err;

TypeError: error:0000000:lib(0):func(0):reason(0)
    at Decipher.Cipher.update (crypto.js:279:27)

以下是我的javalogin.js文件。 错误似乎发生在decipher.update(rows[i].CC, 'hex', 'utf8');行。

我确实打印了一些控制台。

      console.log('DB UserName:', rows[i].UserName); //returns correct username
      console.log('DB CC: ', rows[i].CC); //returns 2bf9kdk02kdnf3dk (correct CC hash on db)

JavaLogin.js:

var mysql = require('mysql');
var crypto = require('crypto');
var sha = require('sha1');


var algorithm = 'aes256'; // or any other algorithm supported by OpenSSL
var key = 'secret';
var cipher = crypto.createCipher(algorithm, key);
var decipher = crypto.createDecipher(algorithm, key);
decipher.setAutoPadding(auto_padding=true);

var db = mysql.createConnection(
    {
      host     : 'host location',
      user     : 'username',
      password : 'password',
      database : 'databaseName',
    }
);

exports.auth = function(query, callback){

  db.query(query, function(err, rows, fields) {

      if (err) throw err;

      for (var i in rows) {
          console.log('DB UserName:', rows[i].UserName);
          console.log('DB CC: ', rows[i].CC);

          var decrypted = decipher.update(rows[i].CC, 'hex', 'utf8');
          decrypted += decipher.final('utf8');
          console.log(decrypted);

      }

      var info = rows;

      return callback(null,info);
  });
};

exports.add = function(query, callback){

  db.query(query, function(err, rows, fields) {

    var encrypted = cipher.update('ccnumber', 'utf8', 'hex');
    encrypted += cipher.final('hex');

      if (err) throw err;

      // for (var i in rows) {
      //     console.log('DB Info:', rows[i].UserName);
      // }

      var info = rows;

      return callback(null,info);
  });
};

1 个答案:

答案 0 :(得分:1)

感谢您在评论中提供的小而实用的帮助。我因此而弄清楚了。

在原始帖子中,我试图将var decipher = crypto.createDecipher(algorithm,key);设置为全局变量,因此我可以在JavaLogin.js中的多个函数中使用它。

如果有人可以告诉我该怎么做,我会很感激。

下面的工作代码部分:

exports.auth = function(query, callback){

  db.query(query, function(err, rows, fields) {

      if (err) throw err;

      for (var i in rows) {
          console.log('DB UserName:', rows[i].UserName);
          console.log('DB CC: ', rows[i].CC);

var decipher = crypto.createDecipher(algorithm, key); //moved this from the top of the file
          var decrypted = decipher.update(rows[i].CC, 'hex', 'utf8');
          decrypted += decipher.final('utf8');
          console.log("decrypted:" + decrypted);

      }

      var info = rows;

      return callback(null,info);
  });
};