title: AES/CBC/PKCS5Padding encryption with fixed IV (or without one)
body: <p>I need to code a Java function that encrypts a string using AES256, with CBC mode and PKCS#5 padding. I've been given a secret key and some before-after examples so I can validate my implementation. This is all I've got. I found that the expected results are exactly the one produced by this online generator: <a href="https://encode-decode.com/aes-256-cbc-encrypt-online/" rel="nofollow noreferrer">https://encode-decode.com/aes-256-cbc-encrypt-online/</a></p>
  <p>One of the parameters I must provide my Cipher instance with is an initialization vector (&quot;IV&quot;). If I don't specify one, Java uses a random one, and therefore produces a different result on each run, which is not the behavior I want.</p>
  <p>The above generator does not ask its users for an IV, and still it produces the same results as my target. So I'd like to know how it is possible. Do people tend to use the same IV (regardless of whether it's secure or not), something like &quot;0000000000000000&quot;, &quot;1234567812345678&quot; (I tried both, just in case)? Or is there any other way to encrypt with the above parameters without using an IV?</p>
  <p>Just in case, here is my code for the moment :</p>
  <pre class="lang-java prettyprint-override"><code>package com.example.test;
  
  import javax.crypto.Cipher;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;
  import java.nio.charset.StandardCharsets;
  import java.util.Base64;
  
  public class AesTest {
      public static String key = &quot;abcdefghijklmnopqrstuvwxyz012345&quot;;
      public static String email = &quot;test@example.com&quot;;
      public static String initVector = &quot;????????????????&quot;;
  
      public static void main(String []args) {
          try {
              Cipher cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5PADDING&quot;);
  
              cipher.init(
                  Cipher.ENCRYPT_MODE,
                  new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), &quot;AES&quot;),
                  new IvParameterSpec(initVector.getBytes(StandardCharsets.UTF_8))
              );
  
              byte[] encrypted = cipher.doFinal(email.getBytes());
              System.out.println(Base64.getEncoder().encodeToString(encrypted));
          } catch (Exception ex) {
              ex.printStackTrace();
          }
      }
  }
  </code></pre>