Search code examples
javascriptencryptioncryptojs

cryptoJS deciphering/decrypting messages AES CFB mode


I am encrypting some string in Ruby, sending it to a client and attempting to decrypt the string there. I am not using the aes rollup (though I have tried with the same result). I have included the aes.js, core.js and cipher-core.js packages as assets. I have checked that the function that is reported as not found does exist in cipher-core.js. I am getting the error:

Uncaught TypeError: Cannot read property 'createDecryptor' of undefined
at Object.reset (aes.self-9251f7d….js?body=1:28)
at Object.init (aes.self-9251f7d….js?body=1:25)
at Object.c.hasOwnProperty.c.init (aes.self-9251f7d….js?body=1:8)
at Object.c.hasOwnProperty.c.init (aes.self-9251f7d….js?body=1:8)
at Object.c.hasOwnProperty.c.init (aes.self-9251f7d….js?body=1:8)
at Object.create (aes.self-9251f7d….js?body=1:8)
at Object.createDecryptor (aes.self-9251f7d….js?body=1:25)
at Object.decrypt (aes.self-9251f7d….js?body=1:31)
at Object.decrypt (aes.self-9251f7d….js?body=1:32)
at Object.decrypt (aes.self-9251f7d….js?body=1:26)

Here is the code:

function decipher(encipheredMessage, password, iv) {
  var parts = encipheredMessage.split('--', 2),
      enciphered = replaceHex(parts[0])

  deciphered = CryptoJS.AES.decrypt(
    enciphered,
    password,
    { iv: iv,
      mode: CryptoJS.mode.CFB,
      padding: CryptoJS.pad.NoPadding }
    ).toString(CryptoJS.enc.Utf8)

  return deciphered
}

Deciphering works when I load the code into node and give it the key/iv that Ruby uses to encipher the string. Has anyone got any idea what is going on?

EDIT

I've been trying to get this working in a number of ways. I've tried making the key a hex encoded string and a hex encoded char array, neither made any change. I'm really struggling with this as I am not a JS programmer, none of the stack exchange answers to other issues with cryptoJS seem to apply.


Solution

  • After reading the cryptoJS source I discovered that I needed to include two more modules into my application.js the complete set of necessary js files was:

    cryptoJS/components/aes
    cryptoJS/components/core
    cryptoJS/components/cipher-core
    cryptoJS/components/mode-cfb
    cryptoJS/components/pad-(whichever padding used)
    

    the aes rollup did not work as it was missing some of these components so I feel it is best to include the components individually.