title: How to fix Invalid AES key length?
body: <p>I am working on a <strong>text encryption and decryption</strong> project (following Struts 2)</p>
  
  <p>Whenever I enter the password and the plain text I get a Invalid AES Key Length error.</p>
  
  <p><strong>The Service Class</strong></p>
  
  <pre><code>package com.anoncrypt.services;
  
  import java.security.Key;
  import javax.crypto.Cipher;
  import javax.crypto.spec.SecretKeySpec;
  
  import sun.misc.BASE64Decoder;
  import sun.misc.BASE64Encoder;
  
  public class SymAES
  {
      private static final String ALGORITHM = "AES";
      private static byte[] keyValue= new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };
  
       public  String encode(String valueToEnc) throws Exception {
          Key key = new SecretKeySpec(keyValue, ALGORITHM);
          Cipher c = Cipher.getInstance(ALGORITHM);
          c.init(Cipher.ENCRYPT_MODE, key);
          byte[] encValue = c.doFinal(valueToEnc.getBytes());
          String encryptedValue = new BASE64Encoder().encode(encValue);
          return encryptedValue;
      }
  
      public  String decode(String encryptedValue) throws Exception {
          Key key = new SecretKeySpec(keyValue, ALGORITHM);
          Cipher c = Cipher.getInstance(ALGORITHM);
          c.init(Cipher.DECRYPT_MODE, key);
          byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
          byte[] decValue = c.doFinal(decordedValue);
          String decryptedValue = new String(decValue);
          return decryptedValue;
      }
  
      public  void start(String passcode)throws Exception
      {
          keyValue = passcode.getBytes();
      }
  }
  </code></pre>
  
  <hr>
  
  <p>And this is the error </p>
  
  <pre><code>java.security.InvalidKeyException: Invalid AES key length: 6 bytes
      com.sun.crypto.provider.AESCrypt.init(AESCrypt.java:87)
      com.sun.crypto.provider.ElectronicCodeBook.init(ElectronicCodeBook.java:93)
      com.sun.crypto.provider.CipherCore.init(CipherCore.java:582)
      com.sun.crypto.provider.CipherCore.init(CipherCore.java:458)
      com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:307)
      javax.crypto.Cipher.implInit(Cipher.java:797)
      javax.crypto.Cipher.chooseProvider(Cipher.java:859)
      javax.crypto.Cipher.init(Cipher.java:1229)
      javax.crypto.Cipher.init(Cipher.java:1166)
      com.anoncrypt.services.SymAES.encode(SymAES.java:35)
      com.anoncrypt.actions.SymEncrypt.execute(SymEncrypt.java:24)
  </code></pre>