0

我正在尝试使用 Crypto.js 进行简单加密,然后使用 Node.js 加密库进行解密。

const crypto = require('crypto')
const CryptoJS = require('crypto-js')

const message = 'Hello World'
const key = '3zTvzr3p67VC61jmV54rIYu1545x4TlY'
const iv = '655478daef95fdae'

const encrypted = CryptoJS.AES.encrypt(message, key, {
  iv: iv,
  padding: CryptoJS.pad.Pkcs7,
  mode: CryptoJS.mode.CBC
})

const encryptedString = encrypted.toString()

const cipher = crypto.createDecipheriv('aes-256-cbc', key, iv)
let decrypted = cipher.update(encryptedString, 'base64', 'utf8')
decrypted += cipher.final('utf8')

console.log(decrypted)

调用cipher.final('utf8')给出了错误:

Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt

据我所见,此错误与密钥错误有关,但我不明白这里怎么可能出现这种情况。

4

0 回答 0