title: Java equivalent for PHP Triple DES ECB encryption/decryption
body: <p>I have a below PHP function that I want to implement in Java with no luck, I'm not able to obtain the same output:</p>
  
  <pre class="lang-php prettyprint-override"><code>function encryptText( $plainText, $key )
  {
      $mcopen = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB,"");
      $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($mcopen), MCRYPT_RAND);
      $td = mcrypt_module_open('tripledes', '', 'ecb', '');
      $cryptedHash = '';
  
      if (mcrypt_generic_init($td, $key, $iv) != -1)
      {
          $cryptedHash = mcrypt_generic($td, $plainText);
          mcrypt_generic_deinit($td);
          mcrypt_module_close($td);
      }
      return base64_encode($cryptedHash);
   }
  </code></pre>
  
  <p>This is my java code found <a href="https://stackoverflow.com/questions/20227/how-do-i-use-3des-encryption-decryption-in-java">here</a> :</p>
  
  <pre class="lang-java prettyprint-override"><code>public static String encrypt(String message, String key) throws Exception {
      final MessageDigest md = MessageDigest.getInstance("md5");
      final byte[] digestOfPassword = md.digest(key.getBytes("utf-8"));
      final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
      for (int j = 0, k = 16; j &lt; 8;) {
          keyBytes[k++] = keyBytes[j++];
      }
  
      final SecretKey keyz = new SecretKeySpec(keyBytes, "DESede");
      final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
      final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
      cipher.init(Cipher.ENCRYPT_MODE, keyz, iv);
  
      final byte[] plainTextBytes = message.getBytes("utf-8");
      final byte[] cipherText = cipher.doFinal(plainTextBytes);
      final String encodedCipherText = new   sun.misc.BASE64Encoder().encode(cipherText);
  
      return encodedCipherText;
  }
  </code></pre>