title: JAVA a reliable equivalent for php's MCRYPT_RIJNDAEL_256
body: <p>I need to access some data that used PHP encryption. The PHP encryption is like this.</p>
  
  <pre><code>base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($cipher), $text, MCRYPT_MODE_ECB));</code></pre>
  
  <p>As value of $text they pass the time() function value which will be different each time that the method is called in.  I have implemented this in Java. Like this,</p>
  
  <pre><code>public static String md5(String string) {
      byte[] hash;
  
      try {
          hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
      } catch (NoSuchAlgorithmException e) {
          throw new RuntimeException("Huh, MD5 should be supported?", e);
      } catch (UnsupportedEncodingException e) {
          throw new RuntimeException("Huh, UTF-8 should be supported?", e);
      }
  
      StringBuilder hex = new StringBuilder(hash.length * 2);
  
      for (byte b : hash) {
          int i = (b &amp; 0xFF);
          if (i &lt; 0x10) hex.append('0');
          hex.append(Integer.toHexString(i));
      }
  
      return hex.toString();
  }
  
  public static byte[] rijndael_256(String text, byte[] givenKey) throws DataLengthException, IllegalStateException, InvalidCipherTextException, IOException{
      final int keysize;
  
      if (givenKey.length &lt;= 192 / Byte.SIZE) {
          keysize = 192;
      } else {
          keysize = 256;
      }
  
      byte[] keyData = new byte[keysize / Byte.SIZE];
      System.arraycopy(givenKey, 0, keyData, 0, Math.min(givenKey.length, keyData.length));
      KeyParameter key = new KeyParameter(keyData);
      BlockCipher rijndael = new RijndaelEngine(256);
      ZeroBytePadding c = new ZeroBytePadding();
      PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(rijndael, c);
      pbbc.init(true, key);
  
      byte[] plaintext = text.getBytes(Charset.forName("UTF8"));
      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);
      return ciphertext;
  }
  
  
  public static String encrypt(String text, String secretKey) throws Exception {
  
  
      byte[] givenKey = String.valueOf(md5(secretKey)).getBytes(Charset.forName("ASCII"));
  
      byte[] encrypted = rijndael_256(text,givenKey);
  
      return new String(Base64.encodeBase64(encrypted));
  }
  </code></pre>
  
  <p>I have referred this answer when creating MCRYPT_RIJNDAEL_256 method."
  <a href="https://stackoverflow.com/questions/18101545/encryption-in-android-equivalent-to-phps-mcrypt-rijndael-256">Encryption in Android equivalent to php&#39;s MCRYPT_RIJNDAEL_256</a>
  "I have used apache codec for Base64.Here's how I call the encryption function,</p>
  
  <pre><code>long time= System.currentTimeMillis()/1000;
  String encryptedTime = EncryptionUtils.encrypt(String.valueOf(time), secretkey);</code></pre>
  
  <p>The problem is sometimes the output is not similar to PHP but sometimes it works fine.
  I think that my MCRYPT_RIJNDAEL_256 method is unreliable.
  I want to know where I went wrong and find a reliable method so that I can always get similar encrypted string as to PHP.</p>