title: AES/CBC/PKCS5Padding issue
body: <p>I am trying to encrypt and decrypt some simple text. But for some reason I am getting a strange error: <code>javax.crypto.BadPaddingException</code>. Why would JCE generates bytes that are not properly padded?</p>
  
  <p>I have the following code:</p>
  
  <pre><code>import javax.crypto.Cipher;
  import javax.crypto.KeyGenerator;
  import javax.crypto.SecretKey;
  import javax.crypto.spec.IvParameterSpec;
  import java.security.SecureRandom;
  
  public class SimplestTest {
      public static void main(String[] args) throws Exception {
          SecureRandom rnd = new SecureRandom();
  
          String text = "Hello, my dear! ... " + System.getProperty("user.home");
          byte[] textData = text.getBytes();
  
          IvParameterSpec iv = new IvParameterSpec(rnd.generateSeed(16));
  
          KeyGenerator generator = KeyGenerator.getInstance("AES");
          generator.init(128);
          SecretKey k = generator.generateKey();
  
          Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
          c.init(Cipher.ENCRYPT_MODE, k, iv);
          c.update(textData);
          byte[] data = c.doFinal();
  
          System.out.println("E: " + data.length);
  
          c = Cipher.getInstance("AES/CBC/PKCS5Padding");
          c.init(Cipher.DECRYPT_MODE, k, iv);
          c.update(data);
  
          System.out.println("R: " + c.doFinal().length);
      }
  
  }
  </code></pre>
  
  <p>But for some reason it does not work. It fails with this exception:</p>
  
  <pre><code>E: 16
  Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
          at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
          at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
          at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
          at javax.crypto.Cipher.doFinal(DashoA13*..)
          at SimplestTest.main(SimplestTest.java:31)
  </code></pre>
  
  <p>What's going wrong? The data size is 16 bytes in length but is still "not properly padded" ?</p>