title: How to debug "Given final block not properly padded" with MD5 and DES in Java?
body: <p>I've files on my computer which are encrypted using this code:</p>
  
  <pre><code>//Arbitrarily selected 8-byte salt sequence:
  private static final byte[] salt = {
      (byte) 0x43, (byte) 0x76, (byte) 0x95, (byte) 0xc7,
      (byte) 0x5b, (byte) 0xd7, (byte) 0x45, (byte) 0x17 
  };
  
  public static Cipher makeCipher(String pass, Boolean decryptMode) throws GeneralSecurityException{
  
      //Use a KeyFactory to derive the corresponding key from the passphrase:
      PBEKeySpec keySpec = new PBEKeySpec(pass.toCharArray());
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");//PBKDF2WithHmacSHA256
      SecretKey key = keyFactory.generateSecret(keySpec);
  
      //Create parameters from the salt and an arbitrary number of iterations:
      PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 42);
  
      //Set up the cipher:
      Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
  
      //Set the cipher mode to decryption or encryption:
      if(decryptMode){
          cipher.init(Cipher.ENCRYPT_MODE, key, pbeParamSpec);
      } else {
          cipher.init(Cipher.DECRYPT_MODE, key, pbeParamSpec);
      }
  
      return cipher;
  }
  
  
  /**Encrypts one file to a second file using a key derived from a passphrase:**/
  public static void encryptFile(String fileName, String pass){
      try{
          byte[] decData;
          byte[] encData;
          File inFile = new File(fileName);
          //Generate the cipher using pass:
          Cipher cipher = Main.makeCipher(pass, true);
  
          //Read in the file:
          FileInputStream inStream = new FileInputStream(inFile);
  
          int blockSize = 8;
          //Figure out how many bytes are padded
          int paddedCount = blockSize - ((int)inFile.length()  % blockSize );
  
          //Figure out full size including padding
          int padded = (int)inFile.length() + paddedCount;
  
          decData = new byte[padded];
  
  
          inStream.read(decData);
  
          inStream.close();
  
          //Write out padding bytes as per PKCS5 algorithm
          for( int i = (int)inFile.length(); i &lt; padded; ++i ) {
              decData[i] = (byte)paddedCount;
          }
  
          //Encrypt the file data:
          encData = cipher.doFinal(decData);
  
          writeToFile(fileName, encData);
  
      } catch(Exception e){
          e.printStackTrace();
      }
  }
  
  private static void writeToFile(String path, byte[] data) {
      try {
          File file = new File(path);
  
          //Write the encrypted data to a new file:
          FileOutputStream outStream = new FileOutputStream(file);
          outStream.write(data);
          outStream.close();
      } catch(Exception e){
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>The key used to encrypt the files is: 'Test', 'test' or 'goon'.</p>
  
  <p>The problem lays around the fact that I'm not able to access the encrypted files. I have an decrypt method which should work:</p>
  
  <pre><code> /**Decrypts one file to a second file using a key derived from a passphrase:**/
  public static void decryptFile(String fileName, String pass, String addition)
                          throws GeneralSecurityException, IOException{
      byte[] encData;
      byte[] decData;
      File inFile = new File(fileName);
  
      //Generate the cipher using pass:
      Cipher cipher = Main.makeCipher(pass, false);
  
      //Read in the file:
      FileInputStream inStream = new FileInputStream(inFile);
      encData = new byte[(int)inFile.length()];
      inStream.read(encData);
      inStream.close();
  
      //Decrypt the file data:
      decData = cipher.doFinal(encData);
  
      //Write the decrypted data to a new file:
      FileOutputStream target = new FileOutputStream(new File(fileName + addition+ ".png"));
      target.write(decData);
      target.close();
  }
  </code></pre>
  
  <p>But when trying to decrypt the file, the following error is thrown:</p>
  
  <pre><code>Exception in thread "main" 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.PBES1Core.doFinal(PBES1Core.java:416)
  at com.sun.crypto.provider.PBEWithMD5AndDESCipher.engineDoFinal(PBEWithMD5AndDESCipher.java:316)
  at javax.crypto.Cipher.doFinal(Cipher.java:2165)
  at Main.decryptFile(Main.java:145)
  at Main.main(Main.java:22)
  </code></pre>
  
  <p>Which is this line :</p>
  
  <pre><code>decData = cipher.doFinal(encData);
  </code></pre>
  
  <p>This is just one of many files. All of my programming projects, websites, work, internship related files are encrypted.</p>
  
  <p>I would really appreciate if someone could help me regaining access to my files. The reason the files got encrypted is because of some wrong permission settings of my user folder which I didn't change back after testing. Which is now fixed.</p>
  
  <p>Is it possible that I did not correctly encrypt the data and therefore cannot decrypt it?</p>
  
  <p>I uploaded an encrypted word document so you can get an insight of what the problem might be. It can be found here: </p>
  
  <p><a href="http://www70.zippyshare.com/v/KfY6qFGD/file.html" rel="nofollow">http://www70.zippyshare.com/v/KfY6qFGD/file.html</a></p>
  
  <p>This document contains 3 lists of HTML/CSS related checks..</p>