通过 crypto-js 解密 AES 256 CBC

我有以下密钥和 IV 可以将07KxrSbGIoPCIYh0I16maw==解密为“496271”,我尝试搜索类似的问题,但我对加密知识的缺乏使我无法自己解决这个问题。


get decryptedCode() {

  var key = CryptoJS.enc.Utf8.parse(

    "814591256d331af80bec0fa2bef1123e37e9f181f363af374787e24160275bce"

  )

  var iv = CryptoJS.enc.Utf8.parse("825b1f7c5f5edd614e8a0a0fef3c9ecf")

  var ciphertext = CryptoJS.enc.Base64.parse("07KxrSbGIoPCIYh0I16maw==")

  var encryptedCP = CryptoJS.lib.CipherParams.create({

    ciphertext: ciphertext,

    formatter: CryptoJS.format.OpenSSL 

  })

  var decryptedWA = CryptoJS.AES.decrypt(encryptedCP, key, { iv: iv })

  var decryptedUtf8 = decryptedWA.toString(CryptoJS.enc.Utf8)


  console.log(decryptedUtf8) // this should be 496271 but I keep getting blank string


  return decryptedUtf8

  }


RISEBY
浏览 388回答 1
1回答

喵喔喔

您的密钥和 IV 是十六进制编码的,因此要解析它们,您需要使用CryptoJS.enc.Hex.parse() 而不是CryptoJS.enc.Utf8.parse():function getDecryptedCode() {&nbsp; var key = CryptoJS.enc.Hex.parse(&nbsp; &nbsp; "814591256d331af80bec0fa2bef1123e37e9f181f363af374787e24160275bce"&nbsp; );&nbsp; var iv = CryptoJS.enc.Hex.parse("825b1f7c5f5edd614e8a0a0fef3c9ecf");&nbsp; var ciphertext = CryptoJS.enc.Base64.parse("07KxrSbGIoPCIYh0I16maw==");&nbsp; var encryptedCP = CryptoJS.lib.CipherParams.create({&nbsp; &nbsp; ciphertext: ciphertext,&nbsp; &nbsp; formatter: CryptoJS.format.OpenSSL&nbsp; });&nbsp; var decryptedWA = CryptoJS.AES.decrypt(encryptedCP, key, {&nbsp; &nbsp; iv: iv&nbsp; });&nbsp; var decryptedUtf8 = decryptedWA.toString(CryptoJS.enc.Utf8);&nbsp; return decryptedUtf8;}console.log(getDecryptedCode()); // 496271<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript