title: MCRYPT_RIJNDAEL_256 PHP Encryption in Java
body: <p>I have the following source code provided by DHL that performs MCRYPT_RIJNDAEL_256 and MCRYPT_RIJNDAEL_128 encryption in PHP:</p>
  
  <pre><code>&lt;?php
      function encrypt_128($data, $key) {
          return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB));
      }
  
      function encrypt_256($data, $key) {
          return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB));
      }
  
      $data = "sellerid=Company&amp;returnsreference=0003&amp;contents_value=101&amp;contents_description=clothes&amp;fullname=Joe%20Bloggs&amp;company=Company&amp;email=joe.bloggs@gmail.com&amp;email_confirm=joe.bloggs@gmail.com&amp;telephone=123&amp;address_line_1=1&amp;city=Acampo&amp;postcode=952200001&amp;country_code=US&amp;weight=1&amp;height=2&amp;length=3&amp;width=4";
      $key = "b1U995YFbERWuzO72GmKSBWpACVIb3L9";
      echo '128:' . PHP_EOL;
      echo encrypt_128($data, $key) . PHP_EOL;
      echo PHP_EOL;
      echo '256:' . PHP_EOL;
      echo encrypt_256($data, $key) . PHP_EOL;
      echo PHP_EOL;
  ?&gt;
  </code></pre>
  
  <p>Which outputs:</p>
  
  <pre><code>128:
  AEt1OHf8hjEnQYeJq07qIxj1j8M0Tyzbg4/5NCwzMwIAA72SecGEdi4XoxwXlgpTbNRyum642KVM3POo2pcPPiRiA6+WPEf6XB0eTAzIUVp8yt2Ibjk83SsNVHCK7n4lR5wXICQ4Go/zFdz68Uwc2Y2mu3SCdiferc2noyhLaInEtej8Q8yF2NwwsZreLtf/6zM7S4RKR6SDa7G2EM3wu0FjOhcyA+6HaTpl+3ZFSdMs5YwqksZjWD5SYQskVXks5y4wtw++sduJ77p2iMe/bLdIAtNwgwLHty9y7jFellxlWMeWmysmyYFNUPaoUPbXM4kDlBle3jz4sjpLiUdjQdLJIfCN6UWyQf47vFzxubt3IfsbiqHU2Qo9mHb7WQh4vtPC+U3wjM9kzV9eXcMCRQ==
  
  256:
  BUMVdaCQoce//aQWEsMgSHNRsWVPvi02ljCtF1PUfEA8Wm0tTZ6EUBEGsAiTOSINwiWikaoGm5jyBRaajHgtgj4y/7R8t5KkxEwM5H9iU/X56c8H65p3pV2P7wNayLiu5eUcLJBXb+siqCCi2VFETgGOMC8o9zjXPQ0oNCYYy5+tT2NGxSGebjM7XMIOAkIKCKvSbF+lUdxPRS1vs5ILuvzx9LP4+461ZYGYmmDKX35qPbCnsx8xaYkXnhAO8W1ddm1ylcanyAFWb2hk6UUmS7SGpSmJMAVwyUDT2uFxaizL4Z6p+KeBRgjPW2i9b5gC/MpjphJO4IKhTqKfHwziinYQ6FZOtDKtD7JC8om1kINpaMIZEHLXHEHF/iuqRbaFcH+M1Qph4Uu95d0eJ0x1A2rs2zRdlWVR72RHVIWez0w=
  </code></pre>
  
  <p>I need to perform 256 bit Rijndael encryption in Java. I have written the following code:</p>
  
  <pre><code>import org.apache.commons.codec.binary.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;
  import java.io.UnsupportedEncodingException;
  import java.security.InvalidAlgorithmParameterException;
  import java.security.InvalidKeyException;
  import java.security.NoSuchAlgorithmException;
  import java.security.spec.AlgorithmParameterSpec;
  
  public class Encrypt {
      public static byte[] ivBytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  
      public static void main(String[] args) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
          String data = "sellerid=Company&amp;returnsreference=0003&amp;contents_value=101&amp;contents_description=clothes&amp;fullname=Joe%20Bloggs&amp;company=Company&amp;email=joe.bloggs@gmail.com&amp;email_confirm=joe.bloggs@gmail.com&amp;telephone=123&amp;address_line_1=1&amp;city=Acampo&amp;postcode=952200001&amp;country_code=US&amp;weight=1&amp;height=2&amp;length=3&amp;width=4";
          String key = "b1U995YFbERWuzO72GmKSBWpACVIb3L9";
          String encoded = encode(data, key);
          System.out.println("Encoded: " + encoded);
          String decoded = decode(encoded, key);
          System.out.println("Decoded: " + decoded);
      }
  
      public static String encode(String data, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
          byte[] textBytes = data.getBytes("UTF-8");
          AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
          SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
          Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
          cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
          return Base64.encodeBase64String(cipher.doFinal(textBytes));
      }
  
      public static String decode(String data, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
          byte[] textBytes = Base64.decodeBase64(data);
          AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
          SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
          Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
          cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
          return new String(cipher.doFinal(textBytes), "UTF-8");
      }
  }
  </code></pre>
  
  <p>Which is outputting:</p>
  
  <pre><code>Encoded: AEt1OHf8hjEnQYeJq07qIxtV0c4Zcfro0Yidrx/rb91OBcbyZ1RZ5id7dYCVhJe4WUvIb2OWyOVDqeVu7cHkM7zHrxRoY5hyQDWKsj4BIuxqxKO0grLYRgqQ2MZtm23D5Ko23h5sJetEYsKI1AxeXJLPQmUDB+RecELIDzJparWto9UcU6llik/v8ZX7DDmrg6K57xaSlCKWVkCKI2HivblAYwTirh7D9n/hO46c3MLngM1/Ao32S1y2XwkNX8k7PTFce5M6BLsDlzoZKjkv+t8n8GTAPPLheC0z9iLBHQbEs2WhzVY4UoCNStITWitn6QI9EwCuVX7oRVlQMi2pqow0GaGwFJ3kqGn5nbXlrbc5Lg4G8SUnM1NB01LbIEBVnVuzXQg8y+Zam47uMJ/0iQ==
  Decoded: sellerid=Company&amp;returnsreference=0003&amp;contents_value=101&amp;contents_description=clothes&amp;fullname=Joe%20Bloggs&amp;company=Company&amp;email=joe.bloggs@gmail.com&amp;email_confirm=joe.bloggs@gmail.com&amp;telephone=123&amp;address_line_1=1&amp;city=Acampo&amp;postcode=952200001&amp;country_code=US&amp;weight=1&amp;height=2&amp;length=3&amp;width=4
  </code></pre>
  
  <p>i.e. It is encrypting it with 128 bit encryption. How do I get 256 bit Rijndael encryption?</p>