title: Java AES String decrypting "given final block not properly padded"
body: <h1>For all haters, I READ MANY topics like this one, and non of them was helpful.</h1>
  <p>eg. here <a href="https://stackoverflow.com/questions/20297621/javax-crypto-badpaddingexception-given-final-block-not-properly-padded-error-wh">javax.crypto.BadPaddingException: Given final block not properly padded error while decryption</a> or here <a href="https://stackoverflow.com/questions/8049872/given-final-block-not-properly-padded">Given final block not properly padded</a></p>
  <p>I want to encrypt and then decrypt Strings. Read many topics about
  &quot;Given final block not properly padded&quot; exception, but non of these solutions worked.</p>
  <p><b> My Class: </b></p>
  <pre><code>package aes;
  
  import javax.crypto.*;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;
  import javax.swing.JOptionPane;
  import java.util.Date;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  
  public class EncryptionExample {
  
  private static SecretKeySpec    key;
  private static IvParameterSpec  ivSpec;
  private static Cipher           cipher; 
  private static byte[]           keyBytes;
  private static byte[]           ivBytes;
  private static int              enc_len;
  
  public static void generateKey() throws Exception
          {
          
              String complex = new String (&quot;9#82jdkeo!2DcASg&quot;);
              keyBytes = complex.getBytes();
              key = new SecretKeySpec(keyBytes, &quot;AES&quot;);
  
              complex = new String(&quot;@o9kjbhylK8(kJh7&quot;); //just some randoms, for now
              ivBytes = complex.getBytes();
              ivSpec = new IvParameterSpec(ivBytes);
  
              cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5Padding&quot;);
          }
          
          public static String encrypt(String packet) throws Exception
          {
              byte[] packet2 = packet.getBytes();
              cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
              byte[] encrypted = new byte[cipher.getOutputSize(packet2.length)];
              enc_len = cipher.update(packet2, 0, packet2.length, encrypted, 0);
              enc_len += cipher.doFinal(encrypted, enc_len);
              
              return packet = new String(encrypted);
          }
          
          public static String decrypt(String packet) throws Exception
          {
              byte[] packet2 = packet.getBytes();
              cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
              byte[] decrypted = new byte[cipher.getOutputSize(enc_len)];
              int dec_len = cipher.update(packet2, 0, enc_len, decrypted, 0);
  HERE EXCEPTION&gt;&gt;&gt;&gt;&gt; dec_len += cipher.doFinal(decrypted, dec_len);  &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
              
              return packet = new String(decrypted);
          }
          
          
          // and display the results
      public static void main (String[] args) throws Exception 
      {
          
            // get the text to encrypt
          generateKey();
          String inputText = JOptionPane.showInputDialog(&quot;Input your message: &quot;);
          
          String encrypted = encrypt(inputText);
          String decrypted = decrypt(encrypted);            
                  
          JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                      &quot;Encrypted:  &quot; + new String(encrypted) + &quot;\n&quot;
                        +  &quot;Decrypted: : &quot; + new String(decrypted));
            .exit(0);
      }
  }
  </code></pre>
  <p><b>The thing is, when I decrypt strings (about 4/10 of shots), I get that exception:</b></p>
  <pre><code>Exception in thread &quot;main&quot; javax.crypto.BadPaddingException: Given final block not properly padded
  at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
  at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:479)
  at javax.crypto.Cipher.doFinal(Cipher.java:2068)
  at aes.EncryptionExample.deszyfrujBez(EncryptionExample.java:HERE tag)
  at aes.EncryptionExample.main(EncryptionExample.java:Main starting)
  </code></pre>
  <p>Does anybody know what to change here (key? *.doFinal() method?) to make it work?</p>
  <p>@ for those curious - methods have to be static, as this is a part of something bigger ;)</p>