title: Problems with encrypt/decrypt between Android and PHP
body: <p>hello fellow programmers,</p>
  
  <p>I followed a tutorial at <a href="http://www.androidsnippets.com/encrypt-decrypt-between-android-and-php" rel="nofollow">http://www.androidsnippets.com/encrypt-decrypt-between-android-and-php</a> to send a <code>String</code> encrypted by PHP back to my Android client where I want to decrypt it again. The encryption/decryption part is running like a charm on my PHP Server and I'm also able to deliver the encrypted String to my Android Client, but I can't figure out how to decrypt the String in Android... all I got are some cryptic (haha) signs...</p>
  
  <p>problem description:
  I'm sending the IV and Encryption Key I generated on the Android Client via REST Api to my PHP Server which is processing some database queries and encrypting a String with the given IV and Encrytpion Key which is then returned to the Android client.
  The Client is able to read out the returned value and to get the encrypted String, BUT the encryption is not acting as awaited...</p>
  
  <p>I followed the upper tutorial strictly but I can't figure out what the problem would be....</p>
  
  <p>here's the code for encryption on PHP:</p>
  
  <pre><code>class MCrypt {
  private $iv;
  private $key;
  private $mode;
  
  function __construct() {
      $this-&gt;mode = 'cbc';
  }
  
  public function setIV($iv) {
      $this-&gt;iv = $iv;
  }
  
  public function setKey($key) {
      //$this-&gt;key = $this-&gt;hex2bin($key);
      $this-&gt;key = $key;
  }
  
  public function getIV() {
      return $this-&gt;iv;
  }
  
  public function getKey() {
      return $this-&gt;key;
  }
  
  public function encrypt($value) {
      $iv = $this-&gt;iv;
      $td = mcrypt_module_open('rijndael-128', '', $this-&gt;mode, $iv);
  
      mcrypt_generic_init($td, $this-&gt;key, $iv);
  
      $encrypted = mcrypt_generic($td, $value);
  
      mcrypt_generic_deinit($td);
      mcrypt_module_close($td);
  
      return bin2hex($encrypted);
  }
  
  
  public function decrypt($value) {
      $value = $this-&gt;hex2bin($value);
      $iv = $this-&gt;iv;
  
      $td = mcrypt_module_open('rijndael-128', '', $this-&gt;mode, $iv);
  
      mcrypt_generic_init($td, $this-&gt;key, $iv);
      $decrypted = mdecrypt_generic($td, $value);
  
      mcrypt_generic_deinit($td);
      mcrypt_module_close($td);
  
      return utf8_encode(trim($decrypted));
  }
  
  
  protected function hex2bin($hexdata) {
      $bindata = '';
  
      for($i = 0; $i &lt; strlen($hexdata); $i += 2) {
          $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
      }
  
      return $bindata;
  }
  
  }
  </code></pre>
  
  <p>and here's how I implemented it:</p>
  
  <pre><code>$value = "some data I want to read out";
  // create new MCrypt object
  $mcrypt = new MCrypt();
  // set IV to hash
  $mcrypt-&gt;setIV($data['iv']);
  // set key to salt
  $mcrypt-&gt;setKey($data['key']);
  // encrypt the key
  $enc = $mcrypt-&gt;encrypt($value);
  
  return array("data"=&gt;$enc);
  </code></pre>
  
  <p>So, this is returning the correctly encoded String (tested with the PHP decrypt function, which is decoding the String to the awaited result).</p>
  
  <p>Here's what the JAVA implementation looks like:
  import java.security.NoSuchAlgorithmException;
  import javax.crypto.Cipher;
  import javax.crypto.NoSuchPaddingException;
  import javax.crypto.SecretKey;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;</p>
  
  <p>import android.util.Log;</p>
  
  <p>public class MCrypt {
      private static final String TAG = MCrypt.class.getSimpleName();</p>
  
  <pre><code>private String iv;
  private String key;
  private IvParameterSpec mIvParameterSpec;
  private SecretKeySpec mSecretKeySpec;
  private Cipher mCipher;
  
  public MCrypt(String iv, String key) {
      Log.v(TAG, "IV Bytes.length=" + iv.getBytes().length);
      this.iv = cut(iv, 16);
      Log.i(TAG, "IV = " + this.iv + ", Bytelength=" + this.iv.getBytes().length);
      this.key = key;
  
      mIvParameterSpec = new IvParameterSpec(this.iv.getBytes());
      mSecretKeySpec = new SecretKeySpec(this.key.getBytes(), "AES");
  
      try {
          mCipher = Cipher.getInstance("AES/CBC/NoPadding");
      } catch (NoSuchAlgorithmException e) {
          Log.e(TAG, "Got Exception while initializing mCipher: " + e.toString(), e);
      } catch (NoSuchPaddingException e) {
          Log.e(TAG, "Got Exception while initializing mCipher: " + e.toString(), e);
      }
  }
  
  public String getIV() {
      return this.iv;
  }
  
  public byte[] decrypt(String code) throws Exception {
      if(code == null || code.length() == 0) {
          throw new Exception("Emtpy string given");
      }
  
      byte[] decrypted = null;
  
      try {
          mCipher.init(Cipher.DECRYPT_MODE, mSecretKeySpec, mIvParameterSpec);
          decrypted = mCipher.doFinal(hexToBytes(code));
      } catch(Exception e) {
          throw new Exception("[decrypt] " + e.getMessage());
      }
  
      return decrypted;
  }
  
  private byte[] hexToBytes(String str) {
      if (str==null) {
          return null;
      } else if (str.length() &lt; 2) {
          return null;
      } else {
          int len = str.length() / 2;
          byte[] buffer = new byte[len];
          for (int i=0; i&lt;len; i++) {
                  buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
          }
          return buffer;
      }
  }
  
  private String cut(String s, int n) {
      byte[] sBytes = s.getBytes();
  
      if(sBytes.length &lt; n) {
          n = sBytes.length;
      }
  
      int n16 = 0;
      boolean extraLong = false;
  
      int i = 0;
  
      while(i &lt; n) {
          n16 += (extraLong) ? 2 : 1;
          extraLong = false;
  
          if((sBytes[i] &amp; 0x80) == 0) {
              i += 1;
          }
          else if((sBytes[i] &amp; 0xC0) == 0x80) {
              i += 2;
          }
          else if((sBytes[i] &amp; 0xE0) == 0xC0) {
              i += 3;
          } else {
              i += 4;
              extraLong = true;
          }
      }
      return s.substring(0, n16);
  }
  }
  </code></pre>
  
  <p>If I want now to en/decrypt a String (e.g. "Hello World") I get this String in return: af8d89fa962794d5b945b74c4b6fd9b6, which is decoded by the PHP implementation to "Hello World", but JAVA just gives some funny signs...</p>
  
  <p>I know that this is a lot of code but I'd appreciate any help here on this issue!</p>
  
  <p>Thanks in advance for your patience and time to read the thread ;)</p>
  
  <hr>
  
  <p>So, what I've come up so far is that <strong>these 'cryptic' signs I got instead of the expected decrypted output are actually the address of the byte array itself</strong>, even if I try to create a new String from this byte array by using <code>String str = new String(byteArray);</code> I get an output which looks like <code>[B@44e8c0c8</code>... what's going on here? Any ideas??</p>