title: Why am I getting javax.crypto.BadPaddingException (AES/CBC/PKCS5Padding)
body: <p>I'm trying to get some encryption/decryption going using <strong>AES/CBC/PKCS5Padding</strong> and am getting a strange result. Depending on the original value I use to encrypt I get an exception:</p>
  <p><code>javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.</code></p>
  <p>To test this out, I wrote a little function that starts with a string and progressively makes it bigger, trying to encrypt the string and decrypt the encrypted result in each iteration.</p>
  <ul>
  <li>First iteration ==&gt; string == &quot;5&quot; <code>Encrypt and decrypt</code></li>
  <li>Next iteration ==&gt; string == &quot;55&quot; <code>Encrypt and decrypt</code></li>
  <li>Next iteration ==&gt; string == &quot;555&quot; <code>Encrypt and decrypt</code></li>
  <li>Next iteration ==&gt; string == &quot;5555&quot; <code>Encrypt and decrypt</code></li>
  <li>Next iteration ==&gt; string == &quot;55555&quot; <code>Encrypt and decrypt</code></li>
  </ul>
  <p>If consistently fails to decrypt the encrypted values in items 0 and 4 (first and last). It successfully decrypts the other values.</p>
  <p>Any clues what may be causing this?</p>
  <p>Here is the output of the program:</p>
  <pre><code>0 **************************************
  
  ENCRYPT Key: [00000000000000000000000000000000] value: [5]
  This is the ciphertext encrypted [ÂZ??¢?»NÔå?Ó^Ç ]
  Encrypted Value = [C25A863FA23FBB4ED4E53FD35E7FC7A0]
  DECRYPT Key: [00000000000000000000000000000000] value: [C25A863FA23FBB4ED4E53FD35E7FC7A0]
  This is the ciphertext [[B@5fdef03a]
  javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
          at java.base/com.sun.crypto.provider.CipherCore.unpad(CipherCore.java:977)
          at java.base/com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1058)
          at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:855)
          at java.base/com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
          at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2205)
          at com.mgl.siebel.crypt.AES256Crypt.decrypt(AES256Crypt.java:35)
          at com.mgl.siebel.crypt.AES256Crypt.test2(AES256Crypt.java:85)
          at com.mgl.siebel.crypt.AES256Crypt.main(AES256Crypt.java:101)
  Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
  
  1 **************************************
  
  ENCRYPT Key: [00000000000000000000000000000000] value: [55]
  This is the ciphertext encrypted []­*çü×z%?eÑ¥zx~÷]
  Encrypted Value = [5DAD2AE7FCD77A259665D1A57A787EF7]
  DECRYPT Key: [00000000000000000000000000000000] value: [5DAD2AE7FCD77A259665D1A57A787EF7]
  This is the ciphertext [[B@5ccd43c2]
  Decrypted Value = [55]
  
  2 **************************************
  
  ENCRYPT Key: [00000000000000000000000000000000] value: [555]
  This is the ciphertext encrypted [M÷o?gI¶àeØÖ8c.+]
  Encrypted Value = [4DF76F916749B6E065D807D638632E2B]
  DECRYPT Key: [00000000000000000000000000000000] value: [4DF76F916749B6E065D807D638632E2B]
  This is the ciphertext [[B@4aa8f0b4]
  Decrypted Value = [555]
  
  3 **************************************
  
  ENCRYPT Key: [00000000000000000000000000000000] value: [5555]
  This is the ciphertext encrypted [ÖFè7tÔ·ðGÂ?WÂGs ]
  Encrypted Value = [D646E83774D4B7F047C28657C24773A0]
  DECRYPT Key: [00000000000000000000000000000000] value: [D646E83774D4B7F047C28657C24773A0]
  This is the ciphertext [[B@7960847b]
  Decrypted Value = [5555]
  
  4 **************************************
  
  ENCRYPT Key: [00000000000000000000000000000000] value: [55555]
  This is the ciphertext encrypted [È±iã?'èÀ­0&lt;eäy?]
  Encrypted Value = [C80EB169E33F27E8C0AD303C65E4791B]
  DECRYPT Key: [00000000000000000000000000000000] value: [C80EB169E33F27E8C0AD303C65E4791B]
  This is the ciphertext [[B@2aae9190]
  javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
          at java.base/com.sun.crypto.provider.CipherCore.unpad(CipherCore.java:977)
          at java.base/com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1058)
          at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:855)
          at java.base/com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
          at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2205)
          at com.mgl.siebel.crypt.AES256Crypt.decrypt(AES256Crypt.java:35)
          at com.mgl.siebel.crypt.AES256Crypt.test2(AES256Crypt.java:85)
          at com.mgl.siebel.crypt.AES256Crypt.main(AES256Crypt.java:101)
  Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
  </code></pre>
  <p>Here is the code being executed</p>
  <pre><code>
    public String decrypt(String key, String encryptedRawValue) throws Exception {
          System.out.println(&quot;DECRYPT Key: [&quot; + key + &quot;] value: [&quot; + encryptedRawValue + &quot;]&quot;);
          try {
            if ((key == null) || (encryptedRawValue == null)) {
              throw new Exception(&quot;key and value must not be null&quot;);
            } 
            
            // convert raw value into its original encrypted sequence of bytes
            byte[] ciphertext = DatatypeConverter.parseHexBinary(encryptedRawValue);
            System.out.println(&quot;This is the ciphertext [&quot; + ciphertext + &quot;]&quot;);
            byte[] raw = key.getBytes(Charset.forName(&quot;UTF-8&quot;));
            if (raw.length != 32) {
              throw new IllegalArgumentException(&quot;Invalid key size.&quot;);
            }
            SecretKeySpec skeySpec = new SecretKeySpec(raw, &quot;AES&quot;);
            
            Cipher cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5Padding&quot;);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
            byte[] original = cipher.doFinal(ciphertext);
            String plainTextValue = new String(original, Charset.forName(&quot;UTF-8&quot;));
            return (plainTextValue);
          } catch (Exception e) {
            e.printStackTrace();
            throw e;
          }
        }
        
    public String encrypt(String key, String value) throws Exception {
          System.out.println(&quot;ENCRYPT Key: [&quot; + key + &quot;] value: [&quot; + value + &quot;]&quot;);
      try {
        byte[] raw = key.getBytes(Charset.forName(&quot;UTF-8&quot;));
        if (raw.length != 32) {
          throw new Exception(&quot;Invalid key size.&quot;);
        }
    
        SecretKeySpec skeySpec = new SecretKeySpec(raw, &quot;AES&quot;);
        Cipher cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5Padding&quot;);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
        String encryptedValue = new String(cipher.doFinal(value.getBytes(Charset.forName(&quot;UTF-8&quot;))));
        System.out.println(&quot;This is the ciphertext encrypted [&quot; + encryptedValue + &quot;]&quot;);
  
        String rawValue = DatatypeConverter.printHexBinary(encryptedValue.getBytes());
        return (rawValue);
      } catch(Exception e) {
        e.printStackTrace();
        throw e;
      }
    }
  
  
    private void test2() throws Exception {
      String key = &quot;00000000000000000000000000000000&quot;;
      try {
        String value = &quot;&quot;;
        for (int i=0; i &lt; 5; i++) { // loop 5 times encrypting and decrypting 
          System.out.println(&quot;\n&quot; + i + &quot; **************************************\n&quot;);
          try {
            value = value + &quot;5&quot;;
            String encryptedValue = this.encrypt(key, value);
            System.out.println(&quot;Encrypted Value = [&quot;+ encryptedValue + &quot;]&quot;);
      
            String plainTextValue = this.decrypt(key, encryptedValue);
            System.out.println(&quot;Decrypted Value = [&quot;+ plainTextValue + &quot;]&quot;);
  
          } catch(Exception e) {
            System.out.println(e.getMessage());
          }
  
        }
      } catch(Exception e) {
        e.printStackTrace();
      }
    }
  
    public static void main(String[] args) {
      AES256Crypt c = new AES256Crypt();
      try {
        c.test2();
      } catch(Exception e) {
        e.printStackTrace();
      }
    }
  
  
  }
  </code></pre>