Node.js 使用加密时,NodeJS解密失败。问题是什么?

Node.js 使用加密时,NodeJS解密失败。问题是什么?,node.js,encryption,aes,Node.js,Encryption,Aes,这就是我得到的错误。我哪里出错了?如果我们尝试另一种方法?如本例所示: var decipher = Crypto.createDecipheriv('aes-256-cfb', 'testtesttesttesttesttest', 'testtesttesttest') Error: Invalid key length at new Decipheriv (crypto.js:267:16) at Object.createDecipheriv (crypto.js:62

这就是我得到的错误。我哪里出错了?如果我们尝试另一种方法?如本例所示:

var decipher = Crypto.createDecipheriv('aes-256-cfb', 'testtesttesttesttesttest', 'testtesttesttest')

Error: Invalid key length
    at new Decipheriv (crypto.js:267:16)
    at Object.createDecipheriv (crypto.js:627:10)

密钥的长度必须正好为32字节。 请尝试下面的代码,看看它是否工作

function encryptdata(key, text) {

  const hash = crypto.createHash('sha256');
                   hash.update(key);
  const keyBytes = hash.digest();

  const iv = crypto.randomBytes(16);
  const cipher = crypto.createCipheriv('aes-256-cfb', keyBytes, iv);
  let enc = [iv, cipher.update(text, 'utf8')];
  enc.push(cipher.final());
  return Buffer.concat(enc).toString('base64');
}

使用不受支持/无效长度的密钥-根据“无效密钥长度”?(是否必须首先扩展密钥?@user2864740 iv密钥长度?密钥本身必须为256位,但这是可以实现的。这是内部完成的吗?@user2864740是的。问题在于IVEven,即使我使用var iv=crypto.randomBytes(16),我仍然会得到相同的错误。仍然会得到相同的问题。
var crypto = require('crypto');
var key = 'testtesttesttesttesttesttesttest';

try{
var cipher = crypto.createCipheriv('aes-256-cfb', key, 'testtesttesttest');
var encryptedData = cipher.update("hello", 'utf8', 'hex') + cipher.final('hex');
console.log(encryptedData);

var decipher = crypto.createDecipheriv('aes-256-cfb', key, 'testtesttesttest');
var decryptedData = decipher.update(encryptedData, 'hex', 'utf8') + decipher.final('utf8');
console.log(decryptedData);
} catch(exception) {
    console.error(exception);
}