title: AES: encrypt using CCCrypt() method and decrypt using C#, the output is wrong
body: <p>Use <code>CCCrypt()</code> method to encrypt, and then decrypt it in C#. But the output is not same as the original plain text. </p>
  
  <p>The key is 256 bits long, and the IV is the default value.</p>
  
  <p>The main codes is showing as below:</p>
  
  <pre><code>// Encrypt
  {
      // the key is 32 bytes （256 bits）.
      Byte iv[] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  
      size_t numBytesEncrypted = 0;
      CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,        // Operation
                                        kCCAlgorithmAES128, // Algorithm
                                        kCCOptionPKCS7Padding, // Option
                                        keyPtr,                // key
                                        kCCKeySizeAES256,      // key length
                                        iv, /* initialization vector (optional) */
                                        [self bytes],    // plain text
                                        dataLength, /* input */
                                        buffer,
                                        bufferSize, /* output */
                                        &amp;numBytesEncrypted); //dataOutMove
      if (cryptStatus == kCCSuccess) {
          NSData *encryptedData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
          NSString *encryptedString = [encryptedData base64Encoding];
  }
  
  // Decrypted
  {
      byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
      public static string AESDecrypt(string encryptedString, string key)
      {
          AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
          aes.BlockSize = 128;
          aes.KeySize = 256;
          aes.IV = _key1;
          aes.Key = Encoding.UTF8.GetBytes(key);
          aes.Mode = CipherMode.CBC;
          aes.Padding = PaddingMode.PKCS7;
  
          // Convert Base64 strings to byte array
          byte[] src = System.Convert.FromBase64String(encryptedString);
  
          // decryption
          using (ICryptoTransform decrypt = aes.CreateDecryptor())
          {
              byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
              return Encoding.UTF8.GetString(dest);
          }
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong>
  I found the reason is the <code>keyPtr</code>. In the Encrypt process, I handle the key early like this:</p>
  
  <pre><code>char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
  bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) [key
  getCString:keyPtr maxLength:sizeof(keyPtr)
  encoding:NSUTF8StringEncoding];
  </code></pre>
  
  <p>Now, I modify those codes like this:</p>
  
  <pre><code>NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
  NSUInteger keyLength = [keyData length];
  Byte *keyPtr= (Byte *)malloc(keyLength);
  memcpy(keyPtr, [keyData bytes], keyLength);
  </code></pre>
  
  <p>Then I got the correct output. 
  Although the problem is gone, I really do not know what's wrong about the previous version.</p>