Search code examples
javascriptvue.jsnuxt.jscryptojs

CryptoJS AES 256 ECB Decrypt


I already tried aes-ecb-js and now im trying cryptoJS if it can solve my problem. I already read a few topics and googled a lot but I am not able to decrypt a HEX String with AES ECB 256.

When using an online decoder it works just fine: enter image description here

I tried with the following code according to the documentation (https://cryptojs.gitbook.io/docs/#ciphers)

console.log('decrypt: ' + result)
        const dec = CryptoJS.AES.decrypt(result, key)
        console.log(dec)
        console.log(CryptoJS.enc.Utf8.stringify(dec))

"key" in this case is a String which looks similar to this: 34AKDASFA12312ADSFKLSDK2

The output is sadly undefined when trying to stringily the word array in var "dec"


Solution

  • I solved the problem with switching to the deprecated NPM Package Crypto which is now a built in functionality in NodeJS.

    From there on it is a little messy (in my opinion) to decrypt an AES 256 ECB HEX String.

    function decrypt(encodedString) {
      const crypto = require('crypto')
      const algorithm = 'aes-256-ecb'
      const dateKey = Buffer.from(
        '<YOUR_KEY>',
        'binary'
      )
    
      const decipher = crypto.createDecipheriv(
        algorithm,
        dateKey.toString('binary'),
        ''
      )
      decipher.setAutoPadding(false)
      let dec = decipher.update(encodedString, 'hex', 'utf8')
      dec += decipher.final('utf8')
      return dec
    }