title: AESCrypt decryption between iOS and PHP
body: <p>I am having a heck of a time figuring out how to decrypt a string encrypted with the NSData+AESCrypt.m (<a href="http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html">Explained here</a>)</p>
  
  <p>I have been looking at a handful of other threads, but I only need the iDevice to send a string to a PHP file encrypted, and then it gets decrypted inside PHP (where it gets stored into a database).</p>
  
  <p>This code :</p>
  
  <pre><code>NSString *encryptedString = [@"Hello" AES256EncryptWithKey:@"a16byteslongkey!"];
  NSLog(@"The strign encrypted : %@",encryptedString);
  </code></pre>
  
  <p>Returns the string encrypted : <strong>7opqbb7sEVNoXplyQv/X8g==</strong></p>
  
  <p>And here is my PHP code for decryption:</p>
  
  <pre><code>function decrypt_data($data, $key) {
      return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key,$data,MCRYPT_MODE_ECB);
  }
  
  function unpadPKCS7($data, $blockSize) {
      $length = strlen ( $data );
      if ($length &gt; 0) {
          $first = substr ( $data, - 1 );
  
          if (ord ( $first ) &lt;= $blockSize) {
              for($i = $length - 2; $i &gt; 0; $i --)
                  if (ord ( $data [$i] != $first ))
                      break;
  
              return substr ( $data, 0, $i );
          }
      }
      return $data;
  }
  
  function decrypt_string($string) {
      $string = unpadPKCS7($string,128);
      $string = decrypt_data($string,"a16byteslongkey!");
      return $string;
  }
  die('&lt;br&gt;Basic :'.decrypt_string('7opqbb7sEVNoXplyQv/X8g=='));
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>Been doing some MD5 decryption and experimenting a lot, but still far from achieving usable results.  This is what I got so far:</p>
  
  <pre><code>Original string : Hello
  AES256Encrypt result : 7opqbb7sEVNoXplyQv/X8
  base64_decode Decrypted: îŠjm¾ìSh^™rBÿ×
  mcrypt_rijndael_128 : Õ¯Öå«Ž(ás2’'u)
  mcrypt_rijndael_128 &amp; hex2bin : UÃ)Ä±+úy´e
  </code></pre>
  
  <p>Sadly, no matter how I bend and twist this, I just get jibberish.  Can anyone see what I'm doing wrong?</p>