title: implementing AES256 encryption into IOS
body: <p>This is my java code. Now I want to implement same functionality in Objective-C.</p>
  
  <pre class="lang-java prettyprint-override"><code>Cipher encryptCipher;
  IvParameterSpec iv = new IvParameterSpec(key);
  SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
  encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  encryptCipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
  byte[] encrypted = encryptCipher.doFinal(dataToEncrypt.getBytes());
  Log.d("TAG", "encrypted string:"
          + Base64.encodeToString(encrypted, Base64.DEFAULT));
  return Base64.encodeToString(encrypted, Base64.DEFAULT).trim();
  </code></pre>
  
  <p>This is my iOS implementation</p>
  
  <pre class="lang-obj-c prettyprint-override"><code>- (NSData *)AES256EncryptWithKey:(NSString*)key
  {
      char keyPtr[kCCKeySizeAES256 + 1];
      bzero(keyPtr, sizeof(keyPtr));
      [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
  
      NSUInteger dataLength = [self length];
  
      size_t bufferSize           = dataLength + kCCBlockSizeAES128;
      void* buffer                = malloc(bufferSize);
  
      size_t numBytesEncrypted    = 0;
      CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                    kCCAlgorithmAES128,
                                    kCCOptionPKCS7Padding,
                                    keyPtr,
                                    kCCKeySizeAES256,
                                    NULL,
                                    [self bytes],
                                    dataLength,
                                    buffer,
                                    bufferSize, 
                                    &amp;numBytesEncrypted);
  
      if (cryptStatus == kCCSuccess)
      {
  
          return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
      }
  
      free(buffer);
      return nil;
  }
  </code></pre>
  
  <p>This is my hash key generating function. this function return same key in android and ios</p>
  
  <pre><code>int dkLen = 16;
      NSData *keyData = [hash_key dataUsingEncoding:NSUTF8StringEncoding];
      NSData *salt    = [saltKey dataUsingEncoding:NSUTF8StringEncoding];
      uint    rounds  = 1000;
      uint    keySize = kCCKeySizeAES128;
  
      NSMutableData *derivedKey = [NSMutableData dataWithLength:keySize];
  
  
  
  
  
      CCKeyDerivationPBKDF(kCCPBKDF2,               // algorithm
                           keyData.bytes,           // password
                           keyData.length,          // passwordLength
                           salt.bytes,              // salt
                           salt.length,             // saltLen
                           kCCPRFHmacAlgSHA1,       // PRF
                           rounds,                  // rounds
                           derivedKey.mutableBytes, // derivedKey
                           dkLen*8);
   return derivedKey;
  </code></pre>
  
  <p>I am getting a different output. I am doing anything wrong?.please help me to find out.</p>