title: Java AES encryption error
body: <p>I want to encrypt a message with <code>AES</code> encryption techniques. 
  When i use this code i got some error as</p>
  
  <pre><code>java.security.InvalidKeyException: Illegal key size or default parameters
  </code></pre>
  
  <p>My Encryption code :</p>
  
  <pre><code>public class Encryption {
  
      public static class MessageEncrypt {
  
          public static class AES {
              private final static String ALGO = "AES";
              private String secretKey;
              private String data;
  
              public String encrypt(String secretKey, String data) throws Exception {
                  SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
                  KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), secretKey.getBytes(), 128, 256);
                  SecretKey tmp = factory.generateSecret(spec);
                  SecretKey key = new SecretKeySpec(tmp.getEncoded(), ALGO);
                  Cipher cipher = Cipher.getInstance(ALGO);
                  cipher.init(Cipher.ENCRYPT_MODE, key);
                  return toHex(cipher.doFinal(data.getBytes()));
              }
  
              public String decrypt(String secretKey, String data) throws Exception {
                  SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
                  KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), secretKey.getBytes(), 128, 256);
                  SecretKey tmp = factory.generateSecret(spec);
                  SecretKey key = new SecretKeySpec(tmp.getEncoded(), ALGO);
                  Cipher cipher = Cipher.getInstance(ALGO);
                  cipher.init(Cipher.DECRYPT_MODE, key);
                  return new String(cipher.doFinal(toByte(data)));
              }
  
              private static byte[] toByte(String hexString) {
                  int len = hexString.length() / 2;
                  byte[] result = new byte[len];
                  for (int i = 0; i &lt; len; i++)
                      result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
                  return result;
              }
  
              public static String toHex(byte[] stringBytes) {
                  StringBuffer result = new StringBuffer(2 * stringBytes.length);
                  for (int i = 0; i &lt; stringBytes.length; i++) {
                      result.append(HEX.charAt((stringBytes[i] &gt;&gt; 4) &amp; 0x0f)).append(HEX.charAt(stringBytes[i] &amp; 0x0f));
                  }
                  return result.toString();
              }
  
              private final static String HEX = "0123456789ABCDEF";
          }
      }
  
      static class DataEncrypt {
  
      }
  
  }
  </code></pre>
  
  <p>My Testing Program :</p>
  
  <pre><code>public class Testing {
  
      public static void main(String[] args) throws Exception {
  
          AES cryptoAES = new AES();
          System.out.println(cryptoAES.encrypt("43234sfeff", "re"));
  
      }
  
  }
  </code></pre>
  
  <p>When i run this i got this error as:</p>
  
  <pre><code>Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters
      at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1021)
      at javax.crypto.Cipher.implInit(Cipher.java:796)
      at javax.crypto.Cipher.chooseProvider(Cipher.java:859)
      at javax.crypto.Cipher.init(Cipher.java:1229)
      at javax.crypto.Cipher.init(Cipher.java:1166)
      at com.detroit.Encryption$MessageEncrypt$AES.encrypt(Encryption.java:35)
      at testing.Testing.main(Testing.java:10)
  </code></pre>
  
  <p>Working in Android Studio :</p>
  
  <pre><code>But the Same code working in Android (Android Studio), but when i run the same code in netbeans i got such kind of errors.
  </code></pre>