title: Aes decryption in java - problems with padding
body: <p>I'd like to implement a simple encryption/decryption tool in java.
  Therefore I found a little tutorial on: <a href="http://www.codejava.net/coding/file-encryption-and-decryption-simple-example" rel="nofollow">http://www.codejava.net/coding/file-encryption-and-decryption-simple-example</a></p>
  
  <p>I changed some lines of code, to guarantee the encryption of large files.
  Now I got the problem, that the decryption doen't work.</p>
  
  <p>I got the following error message/exception:</p>
  
  <pre><code>Error encrypting/decrypting file
      at Algorithmus.Encryptor.doCrypto(Encryptor.java:71)
      at Algorithmus.Encryptor.decrypt(Encryptor.java:39)
      at GUI.MainWindow$encryptThread.run(MainWindow.java:838)
  Caused by: javax.crypto.BadPaddingException: Given final block not properly padded
      at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
      at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
      at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
      at javax.crypto.Cipher.doFinal(Cipher.java:2165)
      at Algorithmus.Encryptor.doCrypto(Encryptor.java:60)
      ... 2 more
  </code></pre>
  
  <p>I tried to change the Transoformation parameter to <code>AES/CBC/PKCS5Padding</code> but that has no effect. Does anyone know, how to optimize the given code?</p>
  
  <pre><code>        private static final String ALGORITHM = "AES";
  
          private static final String TRANSFORMATION = "AES";
  
          public static  void encrypt(String key, File inputFile, File outputFile)
                  throws ExtendedException {
              doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
          }
  
          public static  void decrypt(String key, File inputFile, File outputFile)
                  throws ExtendedException {
              doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
          }
  
          private static void doCrypto(int cipherMode, String key, File inputFile,
                  File outputFile) throws ExtendedException {
              try {
                  Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
                  Cipher cipher = Cipher.getInstance(TRANSFORMATION);
                  cipher.init(cipherMode, secretKey);
  
                  FileInputStream inputStream = new FileInputStream(inputFile);
  
  
                  CipherOutputStream out = new CipherOutputStream(new FileOutputStream(outputFile), cipher);
                  byte[] buffer = new byte[8192];
                  byte[] outputBytes = null;
                  FileOutputStream outputStream = new FileOutputStream(outputFile);
                  int count;
                  while ((count = inputStream.read(buffer)) &gt; 0)
                  {
                      out.write(buffer, 0, count);
                      outputBytes = cipher.doFinal(buffer);
  
                  }
  
  
                  inputStream.close();
                  outputStream.close();
  
              } catch (NoSuchPaddingException | NoSuchAlgorithmException
                      | InvalidKeyException | BadPaddingException
                      | IllegalBlockSizeException | IOException ex) {
                  throw new ExtendedException("Error encrypting/decrypting file", ex);
              }
          }
  </code></pre>