RijndaelManaged Padding when the data matches the block size

If I use PKCS7 padding in Rijndael control with 16 bytes of data, then I get 32 ​​bytes of data output. It looks like for PKCS7, when the data size matches the block size, it adds a whole extra block of data.

If I use Zeros padding for 16 bytes of data, I get 16 bytes of data. So for padding with zeros, if the data matches the block size, it won't fit.

I have looked at the documentation and it says nothing about this difference in behavior.

Can anyone point me to some documentation that indicates what should be for padding behavior for different padding modes when the data size matches the block size.

+2


a source to share


1 answer


I came across this article which has an explanation that seems to undermine some of the other articles I found during my search. Here's the main reason:



You might be wondering what happens if our data length is a perfect multiple of the block size. In this case PaddingMode.None and PaddingMode.Zeros add no padding. However, in the case of PaddingMode.PKCS7, the padding must be added, because the cipher must be able to reverse even the absence of padding. In this case, an extra block must be added to the plain text, and the value of each byte is set to the block size in bytes.

+6


a source







All Articles