title: AES java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long
body: <p>Iam trying to Encrypt data using AES encryption in my Java application. When I run the code(given below), i get: </p>
  
  <pre><code>java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long
      at com.sun.crypto.provider.CipherCore.init(CipherCore.java:525)
      at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:346)
      at javax.crypto.Cipher.implInit(Cipher.java:806)
      at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
      at javax.crypto.Cipher.init(Cipher.java:1396)
      at javax.crypto.Cipher.init(Cipher.java:1327)
      at TestEncription.encryptData(TestEncription.java:164)
      at TestEncription.encodeRequest(TestEncription.java:109)
      at TestEncription.main(TestEncription.java:65)
  </code></pre>
  
  <hr>
  
  <p>Code:</p>
  
  <pre><code>public String encryptData(String requestData, byte[] sessionKey,
          String messageRefNo) throws Exception {
  
      SecretKey secKey = new SecretKeySpec(sessionKey, "AES");
      Cipher cipher = Cipher.getInstance(symmetricKeyAlgorithm);
      IvParameterSpec ivSpec = new IvParameterSpec(messageRefNo.getBytes("UTF-8"));
      System.out.println("Seckey: "+secKey);
      cipher.init(Cipher.ENCRYPT_MODE, secKey, ivSpec);
      byte[] newData = cipher.doFinal(requestData.getBytes());
  
      return Base64.encodeBase64String(newData);
  }
  </code></pre>
  
  <p>What is that going wrong in here?</p>