Here is how I used AES128
- (NSData *)cipherData:(NSData *)data {
return [self aesOperation:kCCEncrypt OnData:data];
}
- (NSData *)decipherData:(NSData *)data {
return [self aesOperation:kCCDecrypt OnData:data];
}
- (NSData *)aesOperation:(CCOperation)op OnData:(NSData *)data {
NSData *outData = nil;
// Data in parameters
const void *key = cipherKey.bytes;
const void *dataIn = data.bytes;
size_t dataInLength = data.length;
// Data out parameters
size_t outMoved = 0;
// Init out buffer
unsigned char outBuffer[BUFFER_SIZE];
memset(outBuffer, 0, BUFFER_SIZE);
CCCryptorStatus status = -1;
status = CCCrypt(op, kCCAlgorithmAES128, kCCOptionPKCS7Padding, key, kCCKeySizeAES256, NULL,
dataIn, dataInLength, &outBuffer, BUFFER_SIZE, &outMoved);
if(status == kCCSuccess) {
outData = [NSData dataWithBytes:outBuffer length:outMoved];
} else if(status == kCCBufferTooSmall) {
// Resize the out buffer
size_t newsSize = outMoved;
void *dynOutBuffer = malloc(newsSize);
memset(dynOutBuffer, 0, newsSize);
outMoved = 0;
status = CCCrypt(op, kCCAlgorithmAES128, kCCOptionPKCS7Padding, key, kCCKeySizeAES256, NULL,
dataIn, dataInLength, &outBuffer, BUFFER_SIZE, &outMoved);
if(status == kCCSuccess) {
outData = [NSData dataWithBytes:outBuffer length:outMoved];
}
}
return outData;
}