title: iOS AES encryption and PHP decryption
body: <p>I am trying to encrypt a string in iOS and decrypt it with PHP.</p>
  
  <p>In iOS I have the following code, based on the information in this link <a href="http://tharindufit.wordpress.com/2011/12/15/aes128-encryption-in-ios-and-decryption-in-php/" rel="nofollow">AES-128 encryption in iOS and Decryption in PHP</a></p>
  
  <pre><code>- (void)testCrypto {
      NSString *password = @"1234567891234567";
      NSString *plaintext = @"This is a test";
      NSString *encrypted = [plaintext AES128EncryptWithKey:password];
  
      dispatch_queue_t jsonParsingQueue = dispatch_queue_create("jsonParsingQueue", NULL);
      dispatch_async(jsonParsingQueue, ^{
          NSString *webResult = [EPWebServices cryptoData:[encrypted dataUsingEncoding:NSUTF8StringEncoding] password:password];
          [self performSelectorOnMainThread:@selector(testing:) withObject:webResult waitUntilDone:YES];
      });
  }
  
  - (void)testing:(id)test {
      test = [[NSString alloc] initWithData:test encoding:NSUTF8StringEncoding];
      NSLog(@"Result:%@", test);
  }
  </code></pre>
  
  <p>The category on NSData used above to encrypt is in the link. <code>EPWebServices</code> is a class that converts the cipher text to base64 and sends a URLRequest to the server with the password and cipher text.</p>
  
  <p>In PHP I have (also from the link above):</p>
  
  <pre><code>$Pass = $_GET['pw'];
  $crypted = urldecode($_GET['encrypted']);        
  $newClear = decrypt_password($crypted, $Pass);
  echo "Dec: ".$newClear . "Input: ".$crypted;
  
  
  function decrypt_password($pass,$key)
  {
  $base64encoded_ciphertext = $pass;
  
  $res_non = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($base64encoded_ciphertext), ‘ecb’);
  $decrypted = $res_non;
  $dec_s2 = strlen($decrypted);
  
  $padding = ord($decrypted[$dec_s2-1]);
  $decrypted = substr($decrypted, 0, -$padding);
  
  return  $decrypted;
  }
  </code></pre>
  
  <p>The problem is that the decrypted message is just an empty string. The result I get in NSLog is just <code>Dec:Input: ejhCNHo0Z2tNZ0paMGdJOTJLdEJjZz09</code>. I have confirmed that both the password and cipher text is received properly in PHP, however, the decryption only prints out an empty string. I am new to encryption, so there might be some basic errors. Please let me know if particular output/NSLog etc is needed. </p>