title: AES_CBC Encryption in IOS Adding salt and iteration
body: <p>I want encrypt my code With AES_CBC Mode in ios , i've android code reference (in android used bouncycastle.crypto).</p>
  
  <p>i've tried with AESEncryption in ios . but i've found one difference, they are used (SALT and ITERATION_COUNT) , is there any way to add in ios(SALT and ITERATION_COUNT) . please help me to solve this issue.</p>
  
  <pre><code>Android Reference:-
  
  private static final int ITERATION_COUNT = 100;
  
  private static final byte[] SALT = new byte[] { (byte) 28,
                                               (byte) 116,
                                               (byte) 95,
                                               (byte) 222,
                                               (byte) 35,
                                               (byte) 65,
                                               (byte) 43,
                                               (byte) 20 };
  inputData:-
      //key:   McBLVcxzJTtJ74yRouQ0bw==    
  
      //rawValue:
  011101010010100101000111110010000000100101100110111110010010000000100010000101110001111000101110110111001000010110100001100011010110100111110010010011101011010011010100010111010010110011011010010010110011001101011001010110001011010011101000100001000100011010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  
     public static byte[] encryptAesCbc(byte[] rawValue, String key) {
      try {
        CipherParameters chiperParams = getKey(key);
        BufferedBlockCipher cipherEncrypt = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
        cipherEncrypt.init(true, chiperParams);
        return processData(rawValue, cipherEncrypt);
  
      }
      catch(Exception e) {
  
      }
      return new byte[0];
    }
  
  public static CipherParameters getKey(String key) {
          final PBEParametersGenerator paramKeyGenerator = new PKCS12ParametersGenerator(new SHA256Digest());
          paramKeyGenerator.init(PKCS12ParametersGenerator.PKCS12PasswordToBytes(key.toCharArray()), SALT, ITERATION_COUNT);
          return paramKeyGenerator.generateDerivedParameters(Integer.parseInt("256"), Integer.parseInt("128"));
   }
  </code></pre>
  
  <p>output:
  1000000000000000000000001000000000000000000010110000011100001011000001110000100000001101000010100000001110010110000000110000100100000111000011011000100100001000000000111001000000000011000010000000110000001101000011000000110110010011000000001010000000111011</p>
  
  <p>//IOS Code reference :-(How to add SALT And Iteration count)</p>
  
  <p>//Encrypts or Decrypts based on CCOperation</p>
  
  <pre><code>- (NSData *) aes128Operation:(CCOperation)operation key:(NSString *)key iv:(NSString *)iv ecb:(BOOL) ecb {
      char keyPtr[kCCKeySizeAES128 + 1];
      bzero(keyPtr, sizeof(keyPtr));
      [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
  
      char ivPtr[kCCBlockSizeAES128 + 1];
      bzero(ivPtr, sizeof(ivPtr));
      if (iv) {
          [iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
      }
  
      NSUInteger dataLength = [self length];
      size_t bufferSize = dataLength + kCCBlockSizeAES128;
      void *buffer = malloc(bufferSize);
  
      size_t numBytesEncrypted = 0;
  
  
  
     CCCryptorStatus cryptStatus;
  
      if (ecb) {
          cryptStatus = CCCrypt(operation, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode, keyPtr,  kCCBlockSizeAES128, ivPtr, [self bytes], dataLength, buffer, bufferSize, &amp;numBytesEncrypted);
      } 
  else {
          cryptStatus = CCCrypt(operation, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr,  kCCBlockSizeAES128, ivPtr, [self bytes], dataLength, buffer, bufferSize, &amp;numBytesEncrypted);
      }
  
      if (cryptStatus == kCCSuccess) {
          return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
      }
  
      free(buffer);
      return nil;
  }
  </code></pre>