title: How to create a secure random AES key in Java?
body: <p>What is the recommended way of generating a secure, random AES key in Java, using the standard JDK?</p>
  
  <p>In other posts, I have found this, but using a <code>SecretKeyFactory</code> might be a better idea:</p>
  
  <pre class="lang-java prettyprint-override"><code>KeyGenerator keyGen = KeyGenerator.getInstance("AES");
  SecureRandom random = new SecureRandom(); // cryptograph. secure random 
  keyGen.init(random); 
  SecretKey secretKey = keyGen.generateKey();
  </code></pre>
  
  <p>It would be great if the answer included an explanation of why it is a good way of generating the random key. Thanks!</p>