I use CryptoJS to decrypt an encryption from my web server(use php and AES-128-ECB), but I can't get the right wordArray , it's length is too long. Here is my test code:
var pwd = "abcdefghijklmnop";
var words = [0x86C5464, 0x7335231];
var plain_array= CryptoJS.lib.WordArray.create(words);
var base64_pwd = CryptoJS.enc.Utf8.parse(pwd).toString(CryptoJS.enc.Base64);
var pwd_key = CryptoJS.enc.Base64.parse(base64_pwd);
var encryption = AES.encrypt(plain_array,pwd_key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7}).toString();
var decrypt = AES.decrypt(encryption,pwd_key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
And The decrypt is :
decrypt == {
sigBytes : 8,
words : [0x86C5464, 0x7335231, 0x8080808, 0x8080808]
}
Why decrypt.words was padded with 0x8080808? How can I get the right length wordArray?
Thanks in advance.
AES is a block cipher and requires input in block size chunks, 16-bytes for AES. If the data to be encrypted is not an even multiple of the block size padding bytes need to be added. PKCS#7 padding is a common padding mode. Most AES libraries support a PKCS#7 padding mode which will add padding on encryption and remove the padding on decryption.
In this case 8-bytes of paddig was added and that is eight bytes of the value 0x08.
See PKCS#7 padding.
Note: Do not use ECB mode, it is insecure, see ECB mode, scroll down to the Penguin. Instead use CBC mode with a random IV, just prefix the encrypted data with the IV for use in decryption.