title: Java AES encryption and decryption
body: <p>I would like to encrypt and decrypt a password using 128 bit AES encryption with 16 byte key.  I am getting <code>javax.crypto.BadPaddingException</code> error while decrypting the value.  Am I missing anything while decrypting?</p>
  
  <pre><code>public static void main(String args[]) {
      Test t = new Test();
      String encrypt = new String(t.encrypt("mypassword"));
      System.out.println("decrypted value:" + t.decrypt("ThisIsASecretKey", encrypt));
  }
  
  public String encrypt(String value) {
      try {
          byte[] raw = new byte[]{'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'};
          SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
          byte[] encrypted = cipher.doFinal(value.getBytes());
          System.out.println("encrypted string:" + (new String(encrypted)));
          return new String(skeySpec.getEncoded());
      } catch (NoSuchAlgorithmException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IllegalBlockSizeException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (BadPaddingException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (InvalidKeyException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (NoSuchPaddingException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      }
      return null;
  }
  
  public String decrypt(String key, String encrypted) {
      try {
          SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(skeySpec.getEncoded(), "AES"));
          //getting error here
          byte[] original = cipher.doFinal(encrypted.getBytes());
          return new String(original);
      } catch (IllegalBlockSizeException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (BadPaddingException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (InvalidKeyException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (NoSuchAlgorithmException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (NoSuchPaddingException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      }
      return null;
  }  
  </code></pre>
  
  <p>Error message</p>
  
  <pre><code>encrypted string:�Bj�.�Ntk�F�`�
  encrypted key:ThisIsASecretKey
  decrypted value:null
  May 25, 2012 12:54:02 PM bean.Test decrypt
  SEVERE: null
  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 bean.Test.decrypt(Test.java:55)
  at bean.Test.main(Test.java:24)
  </code></pre>
  
  <p><strong>Finally I am using following Solution based on @QuantumMechanic answer</strong></p>
  
  <pre><code>public class Test {
  
  public String encryptionKey;
  
  public static void main(String args[]) {
      Test t = new Test();
      String encrypt = t.encrypt("mypassword");
      System.out.println("decrypted value:" + t.decrypt(t.encryptionKey, encrypt));
  }
  
  public String encrypt(String value) {
      try {
          // Get the KeyGenerator
          KeyGenerator kgen = KeyGenerator.getInstance("AES");
          kgen.init(256);
          // Generate the secret key specs.
          SecretKey skey = kgen.generateKey();
          byte[] raw = skey.getEncoded();
          String key = new Base64().encodeAsString(raw);
          this.encryptionKey = key;
          System.out.println("------------------Key------------------");
          System.out.println(key);
          System.out.println("--------------End of Key---------------");
          SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
          String encrypt = (new Base64()).encodeAsString(cipher.doFinal(value.getBytes()));
          System.out.println("encrypted string:" + encrypt);
          return encrypt;
      } catch (NoSuchAlgorithmException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IllegalBlockSizeException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (BadPaddingException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (InvalidKeyException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (NoSuchPaddingException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      }
      return null;
  }
  
  public String decrypt(String key, String encrypted) {
      try {
          Key k = new SecretKeySpec(Base64.getDecoder().decode(key), "AES");
          Cipher c = Cipher.getInstance("AES");
          c.init(Cipher.DECRYPT_MODE, k);
          byte[] decodedValue = Base64.getDecoder().decode(encrypted);
          byte[] decValue = c.doFinal(decodedValue);
          String decryptedValue = new String(decValue);
          return decryptedValue;
      } catch (IllegalBlockSizeException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (BadPaddingException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (InvalidKeyException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (NoSuchAlgorithmException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      } catch (NoSuchPaddingException ex) {
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
      }
      return null;
  }
  </code></pre>
  
  <p>}</p>