entronad / crypto-es

A cryptography algorithms library
Other
275 stars 31 forks source link

Unable to pass in WordArray as key to AES.encrypt #49

Open raghavsethi opened 3 months ago

raghavsethi commented 3 months ago

Hey folks, I'm a bit confused by why this doesn't work - is the AES 256 implementation only designed to work with string keys? Switching the key out to be a string in the following code causes the test to pass:

Relevant code:

import { WordArray } from "crypto-es/lib/core";
import CryptoES from "crypto-es";

const key = WordArray.random(32);
const plaintext = WordArray.random(32);
const cipherParams = CryptoES.AES.encrypt(plaintext, key);
const decrypted = CryptoES.AES.decrypt(cipherParams, key);
expect(decrypted.toString(CryptoES.enc.Hex)).toEqual(
    plaintext.toString(CryptoES.enc.Hex),
);

Expected output: test pass

Actual output: (fails at encryption step)

    TypeError: Cannot read properties of undefined (reading '0')
      at _class.xorBlock (node_modules/crypto-es/lib/cipher-core.js:280:32)
      at _class.call [as processBlock] (node_modules/crypto-es/lib/cipher-core.js:313:14)
      at AESAlgo.processBlock [as _doProcessBlock] (node_modules/crypto-es/lib/cipher-core.js:463:16)
      at AESAlgo._doProcessBlock [as _process] (node_modules/crypto-es/lib/core.js:530:14)
      at AESAlgo._process [as _doFinalize] (node_modules/crypto-es/lib/cipher-core.js:478:35)
      at AESAlgo._doFinalize [as finalize] (node_modules/crypto-es/lib/cipher-core.js:175:37)
      at Function.finalize [as encrypt] (node_modules/crypto-es/lib/cipher-core.js:651:34)
      at Object.encrypt (node_modules/crypto-es/lib/cipher-core.js:110:42)

AES 256 should accept a 256-bit (32-byte) key, so I imagine something else is going wrong here.