Node.js AES-192的IV长度无效
我正在更新旧函数以加密密码,因为Node.js AES-192的IV长度无效,node.js,encryption,Node.js,Encryption,我正在更新旧函数以加密密码,因为createCipher已被弃用 以下是我的旧功能: encrypt(payload) { let AES192 = crypto.createCipher('aes192', Buffer.from(config.secret)) let crypted = AES192.update(payload, 'utf8', 'hex') crypted += AES192.final('hex') return crypted
createCipher
已被弃用
以下是我的旧功能:
encrypt(payload) {
let AES192 = crypto.createCipher('aes192', Buffer.from(config.secret))
let crypted = AES192.update(payload, 'utf8', 'hex')
crypted += AES192.final('hex')
return crypted
},
decrypt(payload) {
let AES192 = crypto.createDecipher('aes192', Buffer.from(config.secret))
let decrypted = AES192.update(payload, 'hex', 'utf8')
decrypted += AES192.final('utf8')
return decrypted
}
以下是我试图做的:
encrypt(payload) {
const iv = crypto.randomBytes(96)
const cipher = crypto.createCipheriv('aes192', Buffer.from(config.secret, 'hex'), iv)
const encrypted = cipher.update(payload)
encrypted = Buffer.concat([encrypted, cipher.final()])
return iv.toString('hex') + ':' + encrypted.toString('hex')
},
decrypt(payload) {
let textParts = payload.split(':')
let iv = Buffer.from(textParts.shift(), 'hex')
let encryptedText = Buffer.from(textParts.join(':'), 'hex')
let decipher = crypto.createDecipheriv('aes192', Buffer.from(config.secret, 'hex'), iv)
let decrypted = decipher.update(encryptedText)
decrypted = Buffer.concat([decrypted, decipher.final()])
return decrypted.toString()
}
但当我尝试这样做时,我得到了这个错误:
Error: Invalid IV length
at Cipheriv.createCipherBase (internal/crypto/cipher.js:103:19)
at Cipheriv.createCipherWithIV (internal/crypto/cipher.js:121:20)
at new Cipheriv (internal/crypto/cipher.js:225:22)
at Object.createCipheriv (crypto.js:119:10)
对于这一行,我尝试了多个值,如12、16、32、124等,但都不起作用
const iv = crypto.randomBytes(96)
AES-192(对于这一点,AES-128和AES-256)都使用128位的块长度,因此IV也应该是128位或16字节。奇怪的是,你试着用16作为长度;无论如何,这个代码对我有用:
function encrypt(payload) {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes192', Buffer.from(config.secret, 'hex'), iv)
let encrypted = cipher.update(payload)
encrypted = Buffer.concat([encrypted, cipher.final()])
return iv.toString('hex') + ':' + encrypted.toString('hex')
}
我假设配置如下所示:
{ secret: 'dc8a453e728fc19398178797e2c39067e1965f2061220257' }
我的秘密可能太长了吗?你的秘密应该是24字节长或192位,所以这可能会引起问题!没问题@Splinteer要注意,你有很多问题的答案都是经过高度投票但不被接受的。接受答案是表达感激/感谢Stackoverflow的最好方式(我只是看了看,因为我很惊讶你没有接受这个答案)。