title: Difficulty with Unencrypting: Error is BadPaddingException: Given final block not properly padded
body: <p>Trying to Unencrypt zipped files.  Some work and others don't.  Those that don't work give the error when trying to close the ZipInputStream.</p>
  
  <p>This was working fine when creating files on the user's computer, but I want to avoid this security risk.  I thought I had all of the risks handled by deleting files in finally, but witnessed a situation where the files were not deleted.  Therefore, I would like to avoid this possibility altogether.  Used <a href="https://stackoverflow.com/questions/23346659/using-streams-to-decrypt-and-unzip-to-limit-memory-usage">Using streams to decrypt and unzip to limit memory usage?</a> to get my code to this point.  </p>
  
  <p>I've read several places about padding and such, but I'll admit I've inherited this code and I'm still on the learning curve.</p>
  
  <pre><code>final File file = new File(path, fileName);
  Key key = new SecretKeySpec(secret, "AES");
  final Cipher cipher = Cipher.getInstance("AES");
  cipher.init(Cipher.DECRYPT_MODE, key);
  try (FileInputStream fis = new FileInputStream(file); CipherInputStream cis = new CipherInputStream(fis, cipher); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(cis))) {
                  ZipEntry entry;
                  while ((entry = zis.getNextEntry()) != null) {
  
                      List&lt;String&gt; lines;
                      try ( ByteArrayOutputStream output = new ByteArrayOutputStream(2048)) {
                          int len;
                          while ((len = zis.read(buffer)) &gt; 0) {
                              output.write(buffer, 0, len);
                          }
                          try (ByteArrayInputStream bais = new ByteArrayInputStream(output.toByteArray()); Reader reader = new InputStreamReader(bais)) {
                              lines = readFile(reader);
                          }
                      }
                      //Do something with lines of the file...
  
                  }
  
              }
  </code></pre>
  
  <p>The error I get is below and is thrown on this last line (the end of the try-with-resources block.</p>
  
  <pre><code>java.io.IOException: javax.crypto.BadPaddingException: Given final block not properly padded
      at javax.crypto.CipherInputStream.close(CipherInputStream.java:321)
      at java.io.BufferedInputStream.close(BufferedInputStream.java:472)
      at java.io.PushbackInputStream.close(PushbackInputStream.java:379)
      at java.util.zip.InflaterInputStream.close(InflaterInputStream.java:227)
      at java.util.zip.ZipInputStream.close(ZipInputStream.java:265)
  
  Caused by: javax.crypto.BadPaddingException: Given final block not properly padded
      at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
      at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
      at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
      at javax.crypto.Cipher.doFinal(Cipher.java:1970)
      at javax.crypto.CipherInputStream.close(CipherInputStream.java:314)
  </code></pre>
  
  <p>I've debugged and walked through the code.  The files that throw this error are unencrypted and unzipped and read properly.  All of the data is read out and the "lines" are complete in every way.  It just doesn't close the ZipInputStream.  Any ideas?</p>