title: Xcode AES encrypt / decrypt - php compatibilty issues
body: <p>I am trying to encrypt a string in Xcode and decrypt it with PHP or encrypt with PHP and decrypt with Xcode.</p>
  
  <p>So first of all I am trying to get the same <code>AES encryption</code> result.</p>
  
  <p>Here is my Xcode file (I've put everything in a single file for simplicity):</p>
  
  <pre><code>#import "t.h"
  #import &lt;CommonCrypto/CommonCryptor.h&gt;
  
  @interface NSData(AES)
  - (NSData*)AES256EncryptWithKey:(NSString*)key;
  - (NSData*)AES256DecryptWithKey:(NSString*)key;
  @end
  
  @implementation NSData (AES)
  
  - (NSData*)AES256EncryptWithKey:(NSString*)key {
  char keyPtr[kCCKeySizeAES256];
  
  [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSASCIIStringEncoding];
  
  NSString *iv = @"1234567812345678";
  char ivPtr[kCCKeySizeAES128];
  
  [iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSASCIIStringEncoding];    
  
  NSUInteger dataLength = [self length];
  
  size_t bufferSize           = dataLength + kCCBlockSizeAES128;
  void* buffer                = malloc(bufferSize);
  
  size_t numBytesEncrypted    = 0;
  CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, NULL,
                                        keyPtr, kCCKeySizeAES256,
                                        ivPtr /* initialization vector (optional) */,
                                        [self bytes], dataLength, /* input */
                                        buffer, bufferSize, /* output */
                                        &amp;numBytesEncrypted);
  
  if (cryptStatus == kCCSuccess)
  {
      return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
  }
  
  free(buffer); //free the buffer;
  return nil;
  }
  
  - (NSData*)AES256DecryptWithKey:(NSString*)key {
  char keyPtr[kCCKeySizeAES256];  
  
  NSString *iv = @"1234567812345678";
  char ivPtr[kCCKeySizeAES128];
  
  [iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSASCIIStringEncoding];
  
  
  // fetch key data
  [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSASCIIStringEncoding];
  
  NSUInteger dataLength = [self length];
  
  size_t bufferSize           = dataLength + kCCBlockSizeAES128;
  void* buffer                = malloc(bufferSize);
  
  size_t numBytesDecrypted    = 0;
  CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, NULL,
                                        keyPtr, kCCKeySizeAES256,
                                        ivPtr /* initialization vector (optional) */,
                                        [self bytes], dataLength, /* input */
                                        buffer, bufferSize, /* output */
                                        &amp;numBytesDecrypted);
  
  if (cryptStatus == kCCSuccess)
  {
      return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
  }
  
  free(buffer); //free the buffer;
  return nil;
  }
  
  @end
  
  
  
  @interface t ()
  @end
  
  @implementation t
  
  - (void)viewDidLoad
  {
  [super viewDidLoad];
  
  
  NSString *key = @"12345678123456781234567812345678";
  
  NSData *plaintext =  [[@"aaa0000000000000" dataUsingEncoding:NSASCIIStringEncoding] AES256EncryptWithKey: key];
  
  label1.text = [[NSString alloc] initWithData:plaintext encoding:NSASCIIStringEncoding];
  
  
  }
  
  @end
  </code></pre>
  
  <p>And now the PHP code: </p>
  
  <pre><code>&lt;?php
  
  $key256 = "12345678123456781234567812345678";
  $iv128 = "1234567812345678";
  $text = "aaa";
  
  $blocksize = 16;
  $len = strlen($text);
  $pad = $blocksize - ($len % $blocksize);
  
  $text .= str_repeat("0", $pad);
  
  $cipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key256, $text, MCRYPT_MODE_CBC, $iv128);
  
  echo $cipher;
  
  ?&gt;
  </code></pre>
  
  <p>The Xcode encrypted string and the php encrypted string are different.</p>
  
  <p>PHP: <code>â5¢Ð[Iüf#hÂ(Æ</code></p>
  
  <p>Xcode: <code>¡#yZPbEs¿D6ñ</code></p>
  
  <p>What can I do to make it work? Am I doing something wrong?</p>
  
  <p>I will further explain what the code does and how it is structured so you can easier understand the problem (correct me if I am wrong):</p>
  
  <ol>
  <li><p>Both PHP and Xcode use CBC for AES encryption so I guess there is no problem there.</p></li>
  <li><p>As you can see I am just trying to get the same result - security does not matter here - so I used no padding - the text is exactly 128 bits in length (16 characters).</p></li>
  <li><p>The key is 256 bits (32 characters).</p></li>
  <li><p>The Initialization Vector is 128 bits (16 characters).</p></li>
  </ol>
  
  <p>Thank you very much. </p>
  
  <p>Any help is appreciated! </p>