aes-js icon indicating copy to clipboard operation
aes-js copied to clipboard

Error: invalid plaintext size (must be multiple of 16 bytes)

Open shiqikai opened this issue 5 years ago • 6 comments

aes-js version: 3.1.2. why should the length of plaintext in CBC be 16?

shiqikai avatar Oct 22 '19 04:10 shiqikai

CBC Operates against blocks, so the data length must be a multiple of 16.

See the CBC section here: https://en.m.wikipedia.org/wiki/Block_cipher_mode_of_operation

Hope this helps. :)

ricmoo avatar Oct 22 '19 05:10 ricmoo

How to solve this problem, I also encountered this pit

fxqy avatar Nov 15 '19 06:11 fxqy

I don't know how to use it gracefully.Please help me write a better code. ------------------------------------------My code-------------------------------------------- function aesEcpt(text, pwd) { var shp = sha256_digest(pwd); var key = []; var iv = []; for (var i = 0; i < 32; i++) { var itm = shp.charCodeAt(i); if (i < 16) key.push(itm); else iv.push(itm); } var textBytes = aesjs.utils.utf8.toBytes(text); var aesCbc = new aesjs.ModeOfOperation.cbc(key, iv); var encryptedBytes = aesCbc.encrypt(textBytes); var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes); return encryptedHex; } function aesDcpt(text, pwd) { var shp = sha256_digest(pwd); var key = []; var iv = []; for (var i = 0; i < 32; i++) { var itm = shp.charCodeAt(i); if (i < 16) key.push(itm); else iv.push(itm); } var encryptedBytes = aesjs.utils.hex.toBytes(text); var aesCbc = new aesjs.ModeOfOperation.cbc(key, iv); var decryptedBytes = aesCbc.decrypt(encryptedBytes); var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes); return decryptedText; }

fxqy avatar Nov 15 '19 06:11 fxqy

But when I'm using ECB mode, this error occurs too. And in Java, the bytes array is not limited to 16*N bytes.

zhbzhbzhbz avatar Jan 08 '20 08:01 zhbzhbzhbz

Perhaps Cipher Text Stealing should be implemented in CBC mode, to support arbitrary plaintext lengths:

https://en.wikipedia.org/wiki/Ciphertext_stealing

RokerHRO avatar Jun 05 '20 15:06 RokerHRO