Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shiqikai opened this issue Oct 22, 2019 · 5 comments
Open

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

shiqikai opened this issue Oct 22, 2019 · 5 comments

Comments

@shiqikai
Copy link

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

@ricmoo
Copy link
Owner

ricmoo commented Oct 22, 2019

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. :)

@fxqy
Copy link

fxqy commented Nov 15, 2019

How to solve this problem, I also encountered this pit

@fxqy
Copy link

fxqy commented Nov 15, 2019

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;
}

@zhbzhbzhbz
Copy link

zhbzhbzhbz commented Jan 8, 2020

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

@RokerHRO
Copy link

RokerHRO commented Jun 5, 2020

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants