Whenever the input is an exact multiple of blocksize, using ZeroBytePadding, no 
padding is necessary, and PaddedBufferedBlockCipher adds an extra block.


static void Main(string[] args)
{
                var key = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13, 14, 15 };

                // ciphertext will be 48 bytes, but should only be 32
                var plaintext = new byte[] {
                                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
14, 15,
                                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
14, 15
                };

                // ciphertext will be 32 bytes, but should only be 16
                //var plaintext = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
10, 11, 12, 13, 14, 15};

                // ciphertext will be 16 bytes, as expected
                //var plaintext = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
10, 11, 12, 13, 14 };

                var cipher = new PaddedBufferedBlockCipher(new AesEngine(), new 
ZeroBytePadding());
                cipher.Init(forEncryption: true, parameters:new 
KeyParameter(key));
                byte[] ciphertext = cipher.DoFinal(plaintext);
}

Reply via email to