使用Node.JS加密/解密消息的正确方法?

时间:2014-08-13 14:44:47

标签: javascript node.js encryption aes

我用它来加密消息:

const CIPHER_ALGORITHM = 'aes256';

var cipherMessage = function(data, key) {
  try {
    var cipher       = crypto.createCipher(CIPHER_ALGORITHM, key);
    var cipheredData = cipher.update(data, "binary", "hex");
    cipheredData += cipher.final("hex");
    return cipheredData;
  } catch(e) {
    return null;
  }
}

这可以解读一条消息:

var decipherMessage = function(msg, key) {
  var ret = {};

  try {
    var decipher = crypto.createDecipher(CIPHER_ALGORITHM, key);
    var decipheredMessage = decipher.update(msg, 'hex', 'binary');
    decipheredMessage += decipher.final("binary");
    ret = JSON.parse(decipheredMessage);
  } catch(e) {
    return null;
  }

  return ret;
}

我必须多次打电话给他们(每秒一次),这是正确的方法吗? 是否存在内存泄漏的可能性?

0 个答案:

没有答案