title: How to use bouncycastle's PaddedBufferedBlockCipher correctly?
body: <p>I've been poking around in the java version of BouncyCastle's encryption APIs.  I'm a little bit confused by the <a href="http://www.bouncycastle.org/docs/docs1.4/org/bouncycastle/crypto/paddings/PaddedBufferedBlockCipher.html?is-external=true" rel="nofollow">PaddedBufferedBlockCipher</a> class.  I understand (perhaps mistakenly?) that it's purpose is for processing encryption incrementally, ie, as data is read from a file.  </p>
  
  <p>When I call processBytes() once, providing all the data to encrypt at once, and then doFinal() it works as expected.  However, when I make several calls to processBytes, using the appropriate offsets/lengths, with the intention of processing in smaller chunks, followed by doFinal(), it fails.  Can anyone explain why the following code fails? Values are hard-coded with the assumption that the clearText is 64 bytes long, and the encryption is performed in 4  x 16-byte blocks.</p>
  
  <pre><code>byte[] key= Hex.decode("a4ea1a7227e032c37f5635e70ba1bd38a4ea1a7227e032c37f5635e70ba1bd38");
  byte[] iv= Hex.decode("a4ea1a7227e032c37f5635e70ba1bd38");
  byte[] clearText= Hex.decode("28d3966905e33c063e7b74c7c7dcb2d688d48d53101d2a6901d365146faf0c9cde3da9ef37664a5e32e4e468f9b52f587d76b78caaf9d9823dd9eb2c1d700d7c");
  
  BlockCipher bc  = new AESFastEngine();
  CBCBlockCipher cipher   = new CBCBlockCipher(bc);
  PaddedBufferedBlockCipher mode  = new PaddedBufferedBlockCipher(cipher);
  KeyParameter kp = new KeyParameter(key);
  CipherParameters ivAndKey= new ParametersWithIV(kp, iv);
  mode.init(true, ivAndKey);
  
  byte[] outBuf   = new byte[mode.getOutputSize(clearText.length)];
  
  int blockSize    = mode.getBlockSize();
  
  // process the 64 bytes of clearText, in 4 x 16-byte chunks 
  for (int i = 0; i &lt; 4; i++) {
  
      mode.processBytes(clearText, i * blockSize, blockSize, outBuf, i * blockSize);
  }
  
  try {
      // call doFinal on the last block (bytes 48-&gt;64)
      mode.doFinal(outBuf, 48);
  } catch (Exception e){}
  
  System.out.println(new String(Hex.encode(outBuf))); 
  </code></pre>
  
  <p>The expected output is: fe391ed41650f3b344ed3d200ffa3c55e71b1d97dbd91c0fbdd0989c3a48822e7e5230e9d6d073bc0752436d9bea9f26328a11586a290e712fbbf874ddfd4ba14e28600e55c7d6ac69467693320a82d3</p>
  
  <p>But the result I'm getting is: 
  00000000000000000000000000000000fe391ed41650f3b344ed3d200ffa3c55e71b1d97dbd91c0fbdd0989c3a48822e328a11586a290e712fbbf874ddfd4ba14e28600e55c7d6ac69467693320a82d3</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Revised code as per PeterDettman's suggestion:</p>
  
  <pre><code>int bytesProcessed = 0;
  
  for (int i = 0; i &lt; 4; i++) {
              // Fixed to be '+='
      bytesProcessed += mode.processBytes(clearText, i * blockSize, blockSize, outBuf, bytesProcessed);
  }
  
  mode.doFinal(outBuf, bytesProcessed);
  </code></pre>