title: AES BadPaddingException
body: <p>if I use a wrong key or wrong salt for decryption an BadPaddingException is thrown.
  I would expect an incorrect string to be returned.
  The doFinal() causes the exception in the decrypt-method</p>
  
  <p>Message : <code>This is just an example</code></p>
  
  <p>Unfug : <code>'ΩÙΩ„SåF?V®ßs.k˚·ºç€èÀHﬁf∫ÙÉÕ</code></p>
  
  <pre><code>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 casino.AES.decryptString(AES.java:130)
      at casino.AES.main(AES.java:172)
  </code></pre>
  
  <hr>
  
  <pre><code>     public static void main(String[] args) throws Exception {
          //Encryption
          AES encr = new AES();   
          encr.setKey("KEY");
          encr.setSalt("SALT");
          encr.setup();
          String message = "This is just an example";
          System.out.println("Message : " + message);
  
  
  
          byte[] code = encr.encrypt(message);
          System.out.println("Encrypted Strinng : "+ new String(code, "UTF-8"));
  
          //Decryption
          AES dec = new AES();
          dec.setKey("INCORRECT"); //&lt;--- incorrect 
          dec.setSalt("SALT");
          dec.setup();
  
          System.out.println(dec.decryptString(code));
      }
  
  
  
  
          public synchronized  void setKey(String key) throws UnsupportedEncodingException {
          this.key = key.getBytes("UTF-8");
          isPasswordAlreadySet = true;
      }
  
  
      public synchronized  void setSalt(String salt) throws UnsupportedEncodingException {
          this.salt = salt.getBytes("UTF-8");
      }
  
      public synchronized  void setup() throws Exception {
      MessageDigest digest = MessageDigest.getInstance("SHA-256");
      digest.update(key);
      digest.update(salt);
      byte[] raw = digest.digest();
  
      skeySpec = new SecretKeySpec(raw, "AES");
      cipher = Cipher.getInstance("AES");
      }  
  
  public synchronized byte[] encrypt(byte[] klartext) throws Exception {
      cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  
      byte[] encrypted = cipher.doFinal(klartext);
  
      return encrypted;
      }
  
      public synchronized byte[] encrypt(String klartext) throws Exception{
      return encrypt(klartext.getBytes("UTF-8")); 
      }
  
  
  
  
  
  
       public synchronized byte[] decrypt(byte[] code) throws Exception {
      cipher.init(Cipher.DECRYPT_MODE, skeySpec);
      byte[] original = cipher.doFinal(code);
      return original;
      }
  
      public synchronized double decryptDouble(byte[] code) throws Exception {
      cipher.init(Cipher.DECRYPT_MODE, skeySpec);
      byte[] original = cipher.doFinal(code);
      return doubleFromBytes( original);
      }
  </code></pre>
  
  <p>Thank you!
  Frederik</p>