title: Turn String to 128-bit key for AES
body: <p>I want to enter my own String variable to then turn it into a key for encryption/decryption for AES algorithm. I have tried many known ways such as UTF-8, base64, some methods doing conversion byte-string and vice versa and some other. Although it's true that all of them work even with some of them not working accurately, all of them turn the string in bytes, but what i want is to enter something like "helloWorld" and get back a 128-bit key for AES. Anything i use it goes for "Invalid key length" since the bytes are not accurate.
  What do i need to do to get the correct bytes? Also i want to clarify that i want String and not an array of char since i want to make it as a function in my programm later so that the user can change the key at will should it be compromised.</p>
  
  <p>UPDATE: i edited the example and this i what i have so far, still throws exception about parameters and key length though</p>
  
  <pre><code>    public class SHAHashingExample
  {
      private static byte[] keyValue;
  
      public static void main(String[] args)throws Exception
      {
          String password = "123456";
  
          MessageDigest md = MessageDigest.getInstance("SHA-256");
          md.update(password.getBytes());
  
          byte byteData[] = md.digest();
          keyValue = md.digest();
  
          //convert the byte to hex format method 1
          StringBuffer sb = new StringBuffer();
          for (int i = 0; i &lt; byteData.length/2; i++) {
           sb.append(Integer.toString((byteData[i] &amp; 0xff) + 0x100, 16).substring(1));
          }
  
          System.out.println("Hex format : " + sb.toString());
  
          //convert the byte to hex format method 2
          StringBuffer hexString = new StringBuffer();
          for (int i=0;i&lt;byteData.length/2;i++) {
              String hex=Integer.toHexString(0xff &amp; byteData[i]);
              if(hex.length()==1) hexString.append('0');
              hexString.append(hex);
          }
          System.out.println("Hex format : " + hexString.toString());
  
          String k = "hello world";
          String f = encrypt(k);
          System.out.println(f);
          String j = decrypt(f);
          System.out.println(j);
      }
  
      public static String encrypt(String Data) throws Exception {
          Key key = generateKey();
          Cipher c = Cipher.getInstance("AES");
          c.init(Cipher.ENCRYPT_MODE, key);
          byte[] encVal = c.doFinal(Data.getBytes());
          String encryptedValue = new BASE64Encoder().encode(encVal);
          return encryptedValue;
      }
  
      public static String decrypt(String encryptedData) throws Exception {
          Key key = generateKey();
          Cipher c = Cipher.getInstance("AES");
          c.init(Cipher.DECRYPT_MODE, key);
          byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
          byte[] decValue = c.doFinal(decordedValue);
          String decryptedValue = new String(decValue);
          return decryptedValue;
      }
  
      private static Key generateKey() throws Exception {
          Key key = new SecretKeySpec(keyValue, "AES");
          return key;
      }
  }
  </code></pre>