brix / crypto-js

JavaScript library of crypto standards.
Other
15.71k stars 2.38k forks source link

AES CBC encrypt error #307

Open shijunsong opened 4 years ago

shijunsong commented 4 years ago

chrome version 80+, occur this error:

TypeError: Cannot read property '0' of undefined
    at Object.xorBlock (cipher-core.js?7c6a:369)
    at Object.processBlock (cipher-core.js?7c6a:314)
    at Object._doProcessBlock (cipher-core.js?7c6a:482)
    at Object._process (core.js?d361:585)
    at Object._doFinalize (cipher-core.js?7c6a:495)
    at Object.finalize (cipher-core.js?7c6a:163)
    at Object.encrypt (cipher-core.js?7c6a:675)
    at Object.encrypt (cipher-core.js?7c6a:201)
    at VueComponent.encrypt (Registry.vue?6ab0:118)
    at VueComponent.onSubmit (Registry.vue?6ab0:168)

my code is this:


encrypt (word, key, iv) {
      key = key || 'absoietlj32fai12'
      iv = iv || 'absoietlj32fai12'
      let utf8Key = cryptojs.enc.Utf8.parse(key)
      let utf8Iv = cryptojs.enc.Utf8.parse(iv)
      let content = cryptojs.enc.Utf8.parse(word)

      let encrypted = cryptojs.AES.encrypt(content, utf8Key, {
        utf8Iv,
        mode: cryptojs.mode.CBC,
        padding: cryptojs.pad.ZeroPadding
      })
      return encrypted.toString()
    },

i don't kown ,where is wrong in my code?

amit13091992 commented 3 years ago

I am getting the below Error while encrypting the data: TypeError: undefined is not an object (evaluating 'block[i]')

Relevant Code:

const encryptUserData = (userData) => {
    var CryptoJS = require("crypto-js");
    var data = userData;
    var key = CryptoJS.enc.Base64.parse('Secret_key');
    var encrypted = CryptoJS.AES.encrypt(
        data,
        key,
        {
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7,
        });
    return encrypted.toString();
}

Please help me out to resolve this issue asap. As I need to use AES encryption with PKCS5/PKCS7 padding. Thanks In Advance.

shijunsong commented 3 years ago

I am getting the below Error while encrypting the data: TypeError: undefined is not an object (evaluating 'block[i]')

Relevant Code:

const encryptUserData = (userData) => {
    var CryptoJS = require("crypto-js");
    var data = userData;
    var key = CryptoJS.enc.Base64.parse('Secret_key');
    var encrypted = CryptoJS.AES.encrypt(
        data,
        key,
        {
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7,
        });
    return encrypted.toString();
}

Please help me out to resolve this issue asap. As I need to use AES encryption with PKCS5/PKCS7 padding. Thanks In Advance.

Hi, partner。 By viewing doc of java about AES, which has not support the model of PKCS7,the last, I had used the PKCS5,I'm sorry I couldn't help you。

Alanscut commented 2 years ago

You left out the property 'iv'. You can try:

let encrypted = cryptojs.AES.encrypt(content, utf8Key, {
        iv: utf8Iv,
        mode: cryptojs.mode.CBC,
        padding: cryptojs.pad.ZeroPadding
})