Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

vishalpatel

macrumors newbie
Original poster
Hi Guys,

This is CommonCrypto problem which is produces only on iPhone device, I can't encrypt data properly with CommonCrypto API ( CCCrypt ) please refere the following code.. and please tell me I am doing it correctly or not ??

Data size less than 1040 bytes encrypted correctly but when data size exceeds this function fails.. 😡


+ (NSMutableData *)encryptOrDecryptWithAES1 : ( NSData *)plainText
key : ( NSData *)aSymmetricKey
context : ( CCOperation)encryptOrDecrypt
{
CCCryptorStatus ccStatus = kCCSuccess;
// Cipher Text container.
NSMutableData * cipherOrPlainText = nil;

// Pointer to output buffer.
uint8_t * bufferPtr = NULL;

// Total size of the buffer.
size_t bufferPtrSize = 0;

// Number of bytes moved to buffer.
size_t movedBytes = 0;

// Length of plainText buffer.
size_t plainTextBufferSize = 0;


// Initialization vector; dummy in this case 0's.
uint8_t iv[(int)kChosenCipherBlockSize];
memcpy((void *) iv, aSymmetricKey.bytes,(size_t) sizeof(iv));

plainTextBufferSize = [plainText length];

bufferPtrSize = ((plainTextBufferSize / kChosenCipherBlockSize) + 1)
* kChosenCipherBlockSize;

// Allocate buffer.
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t) );
// Zero out buffer.
memset((void *)bufferPtr, 0x0, bufferPtrSize);

ccStatus = CCCrypt(encryptOrDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
(const void *) aSymmetricKey.bytes,//[self getSymmetricKeyBytes],
kChosenCipherKeySize,
iv,
(const void *) [plainText bytes],
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
&movedBytes
);


if (ccStatus == kCCSuccess)
cipherOrPlainText = [NSData dataWithBytes : ( const void *)bufferPtr length : ( NSUInteger)movedBytes];
else
cipherOrPlainText = nil;

if(bufferPtr)
free(bufferPtr);

return cipherOrPlainText;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.