I'm trying to perform AES encryption in CBC mode with zero padding. Does anyone know if aesSlow supports zero padding? Based on my reading of the code it doesn't and if that's the cas开发者_Python百科e; can anyone tell me why?
I'm using a 3rd party API which requires this encryption method.
jsfiddle.net/NMATS/2 is my current POC. I'll bemoving it to node once it's debugged. Also the inputs are similar but different for security.
Cheers, Denis
It looks like you are correct. It appears to use PKCS7. Your options then are:
- Pad it with 0's yourself and discard the last block of ciphertext
- Edit the code and make the padding function do zero-padding (beware of license restrictions - I didn't look at the license)
As to why, I'd guess they just haven't had a need to do it or the time to implement it yet.
If you go with option 1, PKCS7 adds a full block of 0x10
bytes if your plaintext is already the multiple of a block size. Therefore, you can just pad your plaintext with 0x00
bytes to make it a multiple of a block size and encrypt it. Then you would drop the last 128-bits of the ciphertext (which is just 16 bytes of 0xFF encrypted). You will end up with a compatible result.
If you go with option 2, I'm not sure which implementation you are using, but I think they're all simple enough.
Here is the padding function for the Javascript implementation:
padBytesIn: function(data) {
var len = data.length;
var padByte = 16 - (len % 16);
for (var i = 0; i < padByte; i++) {
data.push(padByte);
}
},
Here is what you would change it to:
padBytesIn: function(data) {
var len = data.length;
if( len % 16 > 0 ){
var padLen = 16 - (len % 16);
for (var i = 0; i < padLen; i++) {
data.push(0);
}
}
},
精彩评论