title: Generating random IV for AES in Java
body: <p>I'm implementing and AES encryption engine for PBE in android, and I've found two ways to implement the creation of the IV and I would like to know which one is better and more secure for getting <code>IvParameterSpec</code>:</p>
  
  <p><strong>Method #1:</strong></p>
  
  <pre><code>SecureRandom randomSecureRandom = SecureRandom.getInstance("SHA1PRNG");
  byte[] iv = new byte[cipher.getBlockSize()];
  randomSecureRandom.nextBytes(iv);
  
  IvParameterSpec ivParams = new IvParameterSpec(iv);
  </code></pre>
  
  <p><strong>Method #2:</strong></p>
  
  <pre><code>AlgorithmParameters params = cipher.getParameters();
  byte[] iv2 = params.getParameterSpec(IvParameterSpec.class).getIV();
  
  ivParams = new IvParameterSpec(iv2);
  </code></pre>