Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 节点js模块中使用加密的IV长度无效_Node.js_Express_Encryption_Cryptojs_Cryptoapi - Fatal编程技术网

Node.js 节点js模块中使用加密的IV长度无效

Node.js 节点js模块中使用加密的IV长度无效,node.js,express,encryption,cryptojs,cryptoapi,Node.js,Express,Encryption,Cryptojs,Cryptoapi,这是我的密码 const crypto = require('crypto') const algorithm = 'aes-256-cbc'; const key = 't\ufffdy\u0005\ufffdH\ufffd\u0015\ufffdCh\ufffdı\ufffd\ufffd\ufffd>\ufffd(d\ufffd3\ufffd\ufffd\ufffd\ufffd\ufffd\' 4' const iv = '\u0005\ufffd\ufffd\ufffd\ufffd

这是我的密码

const crypto = require('crypto')
const algorithm = 'aes-256-cbc';
const key = 't\ufffdy\u0005\ufffdH\ufffd\u0015\ufffdCh\ufffdı\ufffd\ufffd\ufffd>\ufffd(d\ufffd3\ufffd\ufffd\ufffd\ufffd\ufffd\' 4'

const iv = '\u0005\ufffd\ufffd\ufffd\ufffdKV`\u0007z\ufffd\"H\ufffd\u0013\ufffd'

exports.postMessage = (req,res,next) =>{

    //   var buf= Buffer.from(crypto.randomBytes(16)).toString()

    //   return res.json(iv.length)

    function encrypt(text) {
        let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
        let encrypted = cipher.update(text);
        encrypted = Buffer.concat([encrypted, cipher.final()]);
        return res.json({ iv: iv.toString('hex').slice(0, 16), encryptedData: encrypted.toString('hex') });
       }
      encrypt('some text')
}
错误:错误:无效的IV长度 在Cipheriv.createCipherse(internal/crypto/cipher.js:79:18) 在Cipheriv.createCipherWithIV(internal/crypto/cipher.js:115:20) 在新的Cipheriv(internal/crypto/cipher.js:217:22) 在Object.createCipheriv(crypto.js:109:10)


AES后面的256表示密钥大小,而不是块大小。AES具有128、192和256位大小,并且始终具有128位块大小。当您使用CBC模式时,需要使用IV随机加密,并且IV大小等于块大小。在这种情况下,它是128位,即16字节。因此,请确保您的IV正好是16字节

如果你刚刚开始一个项目,不要使用CBC模式,这是一个旧的操作模式,有很多问题。例如,在你的情况下,它是固定的,这是灾难性的,你把CBC变成了欧洲央行。在CBC模式下,同一密钥下的IV不得重复使用。除此之外,静脉注射必须是不可预测的。你可以用

var iv = Crypto.randomBytes(16);
创建一个随机IV

在密钥大小为128的情况下,在生成2^64个随机IVs之前应该停止,请参见生日悖论。在您的情况下,密钥大小是256,我们不希望一个好的随机IV生成器再次命中同一个IV


要启动新项目,请选择AES-GCM等经过身份验证的加密模式,该模式将为您提供机密性、完整性和身份验证。

您的IV大小为26字节,但应该是32字节。最好是生成random IV并将其与消息一起传递,也就是说,不要将其与可预测的纯文本一起重用,因为这样可以很容易地恢复密钥