【发布时间】:2019-12-12 07:40:26
【问题描述】:
这是我的代码
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.createCipherBase (internal/crypto/cipher.js:79:18) 在 Cipheriv.createCipherWithIV (internal/crypto/cipher.js:115:20) 在新密码 (internal/crypto/cipher.js:217:22) 在 Object.createCipheriv (crypto.js:109:10)
【问题讨论】:
-
IV 的大小是 26 字节,但应该是 32。最好生成随机 IV 并将其与消息一起传递 - 即不要使用可预测的纯文本重用它,因为它会生成它轻松恢复您的密钥
标签: node.js express encryption cryptojs cryptoapi