1

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

boraseoksoon
  • 2,164
  • 1
  • 20
  • 25
puja
  • 209
  • 1
  • 15

1 Answers1

1

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;
Community
  • 1
  • 1
vzsg
  • 2,831
  • 17
  • 20
  • yes i got this Thanks ,but now i have issue with passing encrypted data to php web api as in php can not decrypt data with NSDATA so how can i convert this encrypted NSDATA to Bytes or string. – puja Oct 27 '16 at 11:39
  • If your PHP API supports it, you can set the encrypted raw data as the HTTP request's body directly. If it doesn't, you'll have to discuss with its owner what they expect. You can for example encode the data to a base64 string with NSData's appropriate methods, then convert is back to an UTF-8 encoded data, and use that. – vzsg Oct 27 '16 at 12:22