title: PHP Java AES CBC Encryption Different Results
body: <p>PHP Function:</p>
  
  <pre><code>$privateKey = "1234567812345678";
  $iv = "1234567812345678";
  $data = "Test string";
  
  $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv);
  
  echo(base64_encode($encrypted));
  
  Result: iz1qFlQJfs6Ycp+gcc2z4w==
  </code></pre>
  
  <hr>
  
  <p>Java Function</p>
  
  <pre><code>public static String encrypt() throws Exception{
  try{
      String data = "Test string";
      String key = "1234567812345678";
      String iv = "1234567812345678";
  
      javax.crypto.spec.SecretKeySpec keyspec = new javax.crypto.spec.SecretKeySpec(key.getBytes(), "AES");
      javax.crypto.spec.IvParameterSpec ivspec = new javax.crypto.spec.IvParameterSpec(iv.getBytes());
  
      javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES/CBC/NoPadding");
      cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, keyspec, ivspec);
      byte[] encrypted = cipher.doFinal(data.getBytes());
  
      return new sun.misc.BASE64Encoder().encode(encrypted);
  
  }catch(Exception e){
      return null;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>returns null.</p>
  
  <p>Please note that we are not allowed to change the PHP code. Could somebody please help us get the same results in Java? Many thanks.</p>