asmcrypto / asmcrypto.js

JavaScript Cryptographic Library with performance in mind.
MIT License
659 stars 182 forks source link

AES-CBC has defined blocks of 16, raises the error on data length to be multiple of block size only #141

Open aahmadfarooq opened 6 years ago

aahmadfarooq commented 6 years ago

Hi, I have been trying to use "asmCrypto.AES_CBC.encrypt" function with the a 256 bit key and a varying length of data (since data cannot be limited and will be varying depending on request)

I was getting below error alot of time while invoking this: data length must be a multiple of the block size

And upon debug realized that the AES_Encrypt_finish function has a defined variable plen = 16 and raises the issue in code below:

else if ( len % 16 ) { throw new IllegalArgumentError("data length must be a multiple of the block size"); }

jeromewagener commented 6 years ago

Hello @aahmadfarooq

I am not sure if it helps but I had a similar issue with the same error and resolved it by enabling padding. Both the AES-CBC encryption and decryption have the padding flag in their constructor.

  /**
   * @param {Uint8Array} key
   * @param {Uint8Array} [iv=null]
   * @param {boolean} [padding=true]
   * @param {Uint8Array} [heap]
   * @param {Uint8Array} [asm]
   */
  constructor(key, iv, padding, heap, asm) {
    super(key, iv, padding, heap, asm);
}

Kind regards, Jérôme

alippai commented 6 years ago

Are you using padding? If not, you have process blocks of size N*16.