0

I am encrypting a video and sometimes I get a crash with CCCrypt. This is my encryption method, can anyone shed any light on why it might crash? (The process is run in an NSOperation in a queue). The crash occurs on the CCCryptorStatus cryptStatus = CCCrypt line

    char *key = MY_ENCRYPTION_KEY;
    NSUInteger dataLength = [data length];
    uint8_t encryptedData[dataLength + kCCBlockSizeAES128];
    size_t encryptedLength;
    size_t encryptedDataLength = dataLength + kCCBlockSizeAES128;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode|kCCOptionPKCS7Padding, key, kCCKeySizeAES128, NULL, [data bytes], dataLength, encryptedData, encryptedDataLength, &encryptedLength);

    if(cryptStatus == kCCSuccess)
    {
        NSData *output = [[NSData alloc] initWithBytes:encryptedData length:encryptedLength];

        completionBlock(nil, output);

    } else {
        NSError *error = [[NSError alloc] initWithDomain:@"Error encrypting" code:-999 userInfo:nil];
        completionBlock(error, nil);
    }
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
Darren
  • 10,182
  • 20
  • 95
  • 162
  • Eh, is your file over 4 GiB? You should really use `CCCryptorUpdate` and friends for large blocks of data. Otherwise you will have to read and write it all at once. ECB is insecure, use CBC with a random IV. – Maarten Bodewes Nov 25 '13 at 20:18
  • The data is only a couple of MB – Darren Nov 26 '13 at 11:26
  • It stops with EXC_BAD_ACCESS error on the CCCrypt line. – Darren Nov 26 '13 at 21:03
  • 1
    I've fixed this by changing the buffer `encryptedData` to `void * encryptedData = malloc(dataLength + kCCBlockSizeAES128);` I'd love to know why though. – Darren Nov 27 '13 at 10:15

0 Answers0