title: Bouncy Castle Not Decrypting DoFinal from Java in C#
body: <p>I am trying to decrypt data that is encrypted using the Bouncy Castle library in Java with a C#. The data that is encrypted in Java (again with the Bouncy Castle Libraries) can be decrypted with Java. I am using the same keys and parameters but when I reach DoFinal I get the error "pad block corrupted".</p>
  
  <p>Here is the Java:</p>
  
  <pre><code>KeyParameter keyParam = new KeyParameter(key);
  CipherParameters param = new ParametersWithIV(keyParam, initVector);
  
  BlockCipherPadding padding = new PKCS7Padding();
  BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);
  cipher.reset();
  cipher.init(true, param);
  
  byte[] fileBytes = Base64.decodeBase64(decryptedStringData);
  byte[] encrypted = new byte[cipher.getOutputSize(fileBytes.length)];
  int l = cipher.processBytes(fileBytes, 0, fileBytes.length, encrypted, 0);
  l += cipher.doFinal(encrypted, l);
  
  return (Base64.encodeBase64String(encrypted));
  </code></pre>
  
  <p>Here is the C#:</p>
  
  <pre><code>KeyParameter keyParam = new KeyParameter(key);
  ICipherParameters param = new ParametersWithIV(keyParam, initVector);
  
  IBlockCipherPadding padding = new Pkcs7Padding();
  BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), padding);
  cipher.Reset();
  cipher.Init(false, param);
  
  byte[] fileBytes = Convert.FromBase64String(encryptedDataString);
  byte[] decrypted = new byte[cipher.GetOutputSize(fileBytes.Length)];
  int l = cipher.ProcessBytes(fileBytes, 0, fileBytes.Length, decrypted, 0);
  l += cipher.DoFinal(decrypted, l);
  
  return(Convert.ToBase64String(decrypted));
  </code></pre>
  
  <p>I am generating a 32 byte PBK for the key based on a hash that has been buffered... however, we checked the key generated between Java and C# and they are the same.</p>