puja puja - 1 month ago 14
iOS Question

how to get return data of CCCrypt iOS?

I am getting CCCryptorStatus as kCCSuccess.
This means encryption is done successfully.
However, I want to get encrypted data, how to get it?


Any help will be appreciated.

Thanks,

Puja Rathod

Answer

Take a good look at the arguments of CCCrypt:

CCCryptorStatus CCCrypt(CCOperation op,
     CCAlgorithm alg,
     CCOptions options,
     const void *key,
     size_t keyLength,
     const void *iv,
     const void *dataIn,
     size_t dataInLength,
     void *dataOut,
     size_t dataOutAvailable,
     size_t *dataOutMoved);

(This is from an old man page which uses C types.)

With the last three arguments, you must provide:

  • a byte buffer where the function writes the encrypted data
  • the total (available) length of the buffer
  • and a place where the function can write the number of bytes written.

In Objective-C, you can use NSMutableData for providing the necessary buffer (code example taken from here):

CCCryptorStatus ccStatus   = kCCSuccess;
size_t          cryptBytes = 0;
NSMutableData  *dataOut    = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeAES128];

ccStatus = CCCrypt(encryptOrDecrypt, // kCCEncrypt or kCCDecrypt
                   kCCAlgorithmAES128,
                   kCCOptionPKCS7Padding,
                   key.bytes, 
                   kCCKeySizeAES128,
                   iv.bytes,
                   dataIn.bytes,
                   dataIn.length,
                   dataOut.mutableBytes,
                   dataOut.length,
                   &cryptBytes);

dataOut.length = cryptBytes;