title: aes cbc encrypt, decrypt result is diffrent(objecitve-c, c#)
body: <p>i have a code in c# for aes decryption</p>
  
  <p>i want make same encryption result by objective-c</p>
  
  <p>but i failed.. help me</p>
  
  <p>i can fix objective-c code, what can i for this?</p>
  
  <p>c# for decrypt</p>
  
  <pre class="lang-cs prettyprint-override"><code>        private static readonly string AES_KEY = "asdfasdfasdfasdf";
          private static readonly int BUFFER_SIZE = 1024 * 4;
          private static readonly int KEY_SIZE = 128;
          private static readonly int BLOCK_SIZE = 128;
  
          static public string Composite(string value)
          {
              using (AesManaged aes = new AesManaged())
              using (MemoryStream ims = new MemoryStream(Convert.FromBase64String(value), false))
              {
                  aes.KeySize = KEY_SIZE;
                  aes.BlockSize = BLOCK_SIZE;
                  aes.Mode = CipherMode.CBC;
                  aes.Key = Encoding.UTF8.GetBytes(AES_KEY);
  
                  byte[] iv = new byte[aes.IV.Length];
                  ims.Read(iv, 0, iv.Length);
                  aes.IV = iv;
  
                  using (ICryptoTransform ce = aes.CreateDecryptor(aes.Key, aes.IV))
                  using (CryptoStream cs = new CryptoStream(ims, ce, CryptoStreamMode.Read))
                  using (DeflateStream ds = new DeflateStream(cs, CompressionMode.Decompress))
                  using (MemoryStream oms = new MemoryStream())
                  {
                      byte[] buf = new byte[BUFFER_SIZE];
                      for (int size = ds.Read(buf, 0, buf.Length); size &gt; 0; size = ds.Read(buf, 0, buf.Length))
                      {
                          oms.Write(buf, 0, size);
                      }
                      return Encoding.UTF8.GetString(oms.ToArray());
                  }
              }
          }
  </code></pre>
  
  <p>objective-c for encrypt</p>
  
  <pre><code>- (NSString *)AES128EncryptWithKey:(NSString *)key
  {
      NSData *plainData = [self dataUsingEncoding:NSUTF8StringEncoding];
      NSData *encryptedData = [plainData AES128EncryptWithKey:key];
  
      NSString *encryptedString = [encryptedData stringUsingEncodingBase64];
  
      return encryptedString;
  }
  
  </code></pre>
  
  <pre><code>#import "NSData+AESCrypt.h"
  #import &lt;CommonCrypto/CommonCryptor.h&gt;
  
  static char encodingTable[64] =
  {
     'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
     'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
     'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
     'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
  };
  
  @implementation NSData (AESCrypt)
  
  - (NSData *)AES128EncryptWithKey:(NSString *)key
  {
      // 'key' should be 16 bytes for AES128
      char keyPtr[kCCKeySizeAES128 + 1]; // room for terminator (unused)
      bzero( keyPtr, sizeof( keyPtr ) ); // fill with zeroes (for padding)
  
      // fetch key data
      [key getCString:keyPtr maxLength:sizeof( keyPtr ) encoding:NSUTF8StringEncoding];
  
      NSUInteger dataLength = [self length];
  
      //See the doc: For block ciphers, the output size will always be less than or
      //equal to the input size plus the size of one block.
      //That's why we need to add the size of one block here
      size_t bufferSize = dataLength + kCCBlockSizeAES128;
      void *buffer = malloc( bufferSize );
  
      size_t numBytesEncrypted = 0;
      CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCModeCBC | kCCOptionPKCS7Padding,
                                            keyPtr, kCCKeySizeAES128,
                                            NULL /* initialization vector (optional) */,
                                            [self bytes], dataLength, /* input */
                                            buffer, bufferSize, /* output */
                                            &amp;numBytesEncrypted );
      if( cryptStatus == kCCSuccess )
      {
          //the returned NSData takes ownership of the buffer and will free it on deallocation
          return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
      }
  
      free( buffer ); //free the buffer
      return nil;
  }
  
  - (NSString *)base64Encoding
  {
     const unsigned char   *bytes = [self bytes];
     NSMutableString *result = [NSMutableString stringWithCapacity:self.length];
     unsigned long ixtext = 0;
     unsigned long lentext = self.length;
     long ctremaining = 0;
     unsigned char inbuf[3], outbuf[4];
     unsigned short i = 0;
     unsigned short charsonline = 0, ctcopy = 0;
     unsigned long ix = 0;
  
     while( YES )
     {
        ctremaining = lentext - ixtext;
        if( ctremaining &lt;= 0 ) break;
  
        for( i = 0; i &lt; 3; i++ )
        {
           ix = ixtext + i;
           if( ix &lt; lentext ) inbuf[i] = bytes[ix];
           else inbuf [i] = 0;
        }
  
        outbuf [0] = (inbuf [0] &amp; 0xFC) &gt;&gt; 2;
        outbuf [1] = ((inbuf [0] &amp; 0x03) &lt;&lt; 4) | ((inbuf [1] &amp; 0xF0) &gt;&gt; 4);
        outbuf [2] = ((inbuf [1] &amp; 0x0F) &lt;&lt; 2) | ((inbuf [2] &amp; 0xC0) &gt;&gt; 6);
        outbuf [3] = inbuf [2] &amp; 0x3F;
        ctcopy = 4;
  
        switch( ctremaining )
        {
           case 1:
              ctcopy = 2;
              break;
           case 2:
              ctcopy = 3;
              break;
        }
  
        for( i = 0; i &lt; ctcopy; i++ )
           [result appendFormat:@"%c", encodingTable[outbuf[i]]];
  
        for( i = ctcopy; i &lt; 4; i++ )
           [result appendString:@"="];
  
        ixtext += 3;
        charsonline += 4;
     }
     return [NSString stringWithString:result];
  }
  </code></pre>
  
  <h2>------------------------------------------------------------</h2>
  
  <h2>------------------------------------------------------------</h2>
  
  <hr>