由于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)