title: Why am I getting different results each time I try to decrypt AES data with the iOS SDK?
body: <p>I am working on AES encryption and decryption.
  I am giving single data and single key to decrypt but am getting different outputs every time.</p>
  
  <p>I am using like this:</p>
  
  <pre><code>+(NSData*)decryptData:(NSData*)data key:(NSData*)key iv:(NSData*)iv
  {
  NSData* result = nil;
  
  // setup key
  unsigned char cKey[FBENCRYPT_KEY_SIZE];
  bzero(cKey, sizeof(cKey));
  [key getBytes:cKey length:FBENCRYPT_KEY_SIZE];
  
  // setup iv
  char cIv[FBENCRYPT_BLOCK_SIZE];
  bzero(cIv, FBENCRYPT_BLOCK_SIZE);
  if (iv) {
      [iv getBytes:cIv length:FBENCRYPT_BLOCK_SIZE];
  }
  
  // setup output buffer
  size_t bufferSize = [data length] + FBENCRYPT_BLOCK_SIZE;
  void *buffer = malloc(bufferSize);
  
  // do decrypt
  size_t decryptedSize = 0;
  CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                        FBENCRYPT_ALGORITHM,
                                        kCCOptionECBMode,
                                        cKey,
                                        FBENCRYPT_KEY_SIZE,
                                        cIv,
                                        [data bytes],
                                        [data length],
                                        buffer,
                                        bufferSize,
                                        &amp;decryptedSize);
  
  if (cryptStatus == kCCSuccess) {
      result = [NSData dataWithBytesNoCopy:buffer length:decryptedSize];
  } else {
      free(buffer);
      NSLog(@"[ERROR] failed to decrypt| CCCryptoStatus: %d", cryptStatus);
  }
  
  return result;
  }
  </code></pre>