Node.js 错误:使用crypto.createDecipheriv时密钥长度无效

Node.js 错误:使用crypto.createDecipheriv时密钥长度无效,node.js,cryptography,base64,aes,Node.js,Cryptography,Base64,Aes,我想用aes-128-cbc来解码,但它涉及到Invaid密钥长度,怎么了 代码如下: const crypto = require('crypto'); var key = 'DoCKvdLslTuB4y3EZlKate7XMottHski1LmyqJHvUhs'+'='; var iv = crypto.randomBytes(16) //key.substr(0,16) var keyhex = new Buffer(key,'base64').toString('hex') var de

我想用aes-128-cbc来解码,但它涉及到Invaid密钥长度,怎么了

代码如下:

const crypto = require('crypto');
var key = 'DoCKvdLslTuB4y3EZlKate7XMottHski1LmyqJHvUhs'+'=';
var iv = crypto.randomBytes(16) //key.substr(0,16)
var keyhex = new Buffer(key,'base64').toString('hex')
var decipher = crypto.createDecipheriv('aes-128-cbc',keyhex,iv)
接下来是错误报告: crypto.js:267 这个._handle.initiv(密码,toBuf(密钥),toBuf(iv)); ^

Error: Invalid key length
    at new Decipheriv (crypto.js:267:16)
    at Object.createDecipheriv (crypto.js:627:10)
    at Object.<anonymous> (/home/sheen/workspace/app/base64.js:8:23)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
错误:密钥长度无效
在新的Decipheriv(crypto.js:267:16)
在Object.createDecipheriv(crypto.js:627:10)
反对。(/home/sheen/workspace/app/base64.js:8:23)
在模块处编译(Module.js:643:30)
在Object.Module._extensions..js(Module.js:654:10)
在Module.load(Module.js:556:32)
在tryModuleLoad时(module.js:499:12)
在Function.Module.\u加载(Module.js:491:3)
位于Function.Module.runMain(Module.js:684:10)
启动时(bootstrap_node.js:187:16)

如何解决这个问题?

你把二进制和十六进制混为一谈了。十六进制是字节的文本表示或编码

在64进制解码后,密钥大小为32字节/256位。但是,然后创建一个由64个字符组成的十六进制表示形式。这些被再次转换为字节,所以现在您的密钥大小是64字节/512位,这是一个无效的密钥大小

要使用二进制,只需删除
.toString('hex')
,就可以了