title: Java code for mcrypt_encrypt PHP function with MCRYPT_RIJNDAEL_256 cipher
body: <p>I have a PHP code for encrypting a string(token). I need to generate this token and call the legacy backend API. Backend API decrypts this and validates it before allowing access to the API. We are building the client application in Java. So this code needs to be implemented in Java.</p>
  <pre class="lang-php prettyprint-override"><code>$stringToEncode=&quot;****String which needs to be encrypted. This string length is multiple of 32********************&quot;;
  $key=&quot;32ByteKey-asdcxzasdsadasdasdasda&quot;;
      echo base64_encode(
          mcrypt_encrypt(
                      MCRYPT_RIJNDAEL_256,
                      $key,
                      $stringToEncode,
                      MCRYPT_MODE_CBC,
                      $key
                  )
              );
  </code></pre>
  <p>Here IV is same as key.
  I tried with below Java code using &quot;org.bouncycastle.crypto&quot;</p>
  <pre class="lang-java prettyprint-override"><code>private static void encrypt(String key, String data) throws InvalidCipherTextException {
              
          byte[] givenKey = key.getBytes(Charset.forName(&quot;ASCII&quot;));        
          final int keysize = 256;
          byte[] keyData = new byte[keysize / Byte.SIZE];
          System.arraycopy(givenKey, 0, keyData, 0, Math.min(givenKey.length, keyData.length));
          KeyParameter keyParameter = new KeyParameter(keyData);
          BlockCipher rijndael = new RijndaelEngine(256);
          ZeroBytePadding c = new ZeroBytePadding();
          PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(rijndael, c);
          CipherParameters ivAndKey = new ParametersWithIV(keyParameter, key.getBytes(Charset.forName(&quot;ASCII&quot;)));
          pbbc.init(true, ivAndKey);
          byte[] plaintext = data.getBytes(Charset.forName(&quot;UTF8&quot;));
          byte[] ciphertext = new byte[pbbc.getOutputSize(plaintext.length)];
          int offset = 0;
          offset += pbbc.processBytes(plaintext, 0, plaintext.length, ciphertext, offset);
          offset += pbbc.doFinal(ciphertext, offset);
          System.out.println(&quot;Encrypted: &quot; + Base64.getEncoder().encodeToString(ciphertext));
      }
  </code></pre>
  <p>But getting below exception -</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.IllegalArgumentException: invalid parameter passed to Rijndael init - org.bouncycastle.crypto.params.ParametersWithIV
  </code></pre>
  <p>I even tried with &quot;javax.crypto&quot; as shown below -</p>
  <pre class="lang-java prettyprint-override"><code>import java.nio.charset.Charset;
  import java.security.InvalidAlgorithmParameterException;
  import java.security.InvalidKeyException;
  import java.security.NoSuchAlgorithmException;
  import java.util.Base64;
  
  import javax.crypto.BadPaddingException;
  import javax.crypto.Cipher;
  import javax.crypto.IllegalBlockSizeException;
  import javax.crypto.NoSuchPaddingException;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;
  
  public class aes{
      
       public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
              String key = &quot;32ByteKey-asdcxzasdsadasdasdasda&quot;;
              String data = &quot;****String which needs to be encrypted. This string length is multiple of 32********************&quot;;
              encrypt(key, data);
          }
      
      public static void encrypt(String key1, String data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
          
          byte[] key = key1.getBytes(Charset.forName(&quot;ASCII&quot;));
          byte[] decrypted = data.getBytes(Charset.forName(&quot;UTF8&quot;));      
          
          IvParameterSpec ivSpec = new IvParameterSpec(key);
  
          Cipher cipher = Cipher.getInstance(&quot;AES/CBC/NoPadding&quot;);
          cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, &quot;AES&quot;), ivSpec);
          byte[] encrypted = Base64.getEncoder().encodeToString(cipher.doFinal(decrypted)).getBytes();
  
          System.out.println(encrypted);
      }   
  }
  </code></pre>
  <p>But getting beow exception -</p>
  <pre><code>Exception in thread &quot;main&quot; java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long
  </code></pre>
  <p>Can someone please suggest on how can I implement this?</p>