使用PKCS7填充的CCCrypt在OS X 10.9中不起作用

时间:2013-11-12 19:55:01

标签: encryption osx-mavericks

我们在我们的应用中使用CCryptCreate和CCryptUpdate以及PKCS7 Padding。它在OS X 10.7和10.8中运行良好,但在OS X 10.9中,它不起作用。当我尝试用PKCS7 Padding解密时,CCCryptorUpdate返回-4301,kCCBufferTooSmall错误。相比之下,使用非Padding,CCCryptorUpdate返回成功。 这是加密和解密的代码。有谁知道为什么这可能会出错?

@implementation Crptor

@synthesize secretKey;
const uint8_t keyBytes[] ="abcdef0123456789";

- (id)init
{
    if(self == [super init]){
        secretKey = [NSData dataWithBytes:keyBytes length:sizeof(keyBytes)];
    }
    return self;
}

- (int)cryptFile:(CCOperation)op inPath:(NSString *)inPath outPath:(NSString *)outPath
{
    int             rc = kCCSuccess;
    NSFileManager   *fileManager = [[NSFileManager alloc] init];

    if ([fileManager createFileAtPath:outPath contents:nil attributes:nil] == NO) {
        NSLog(@"[]Failed:NSFileManager::createFileAtPath:contents:attributes");
        return -1;
    }

    NSFileHandle    *input = [NSFileHandle fileHandleForReadingAtPath:inPath];
    NSFileHandle    *output = [NSFileHandle fileHandleForWritingAtPath:outPath];

    if (input == nil) {
        NSLog(@"[]Failed:NSFileHandle::fileHandleForReadingAtPath:");
        return -1;
    }
    if (output == nil) {
        NSLog(@"[]Failed:NSFileHandle::fileHandleForWritingAtPath:");
        return -1;
    }

    CCCryptorRef    cryptor = NULL;
    int             blockSize = 4096;
    size_t          outDataLength;
    size_t          dataOutMoved;
    char            *buffer = NULL;



    rc = CCCryptorCreate(op, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode, CFBridgingRetain(self.secretKey) ,kCCKeySizeAES128, NULL, &cryptor);
    if (rc != kCCSuccess) {
        NSLog(@"[]Failed:CCCryptorCreate[err=%d]", rc);
        goto CLEAN_UP;
    }

    outDataLength = CCCryptorGetOutputLength(cryptor, blockSize, false);

    buffer = malloc(outDataLength * 10);
    bzero(buffer,malloc_size(buffer));

    if (buffer == NULL) {
        rc = (-1);
        goto CLEAN_UP;
    }

    while (true) {
        NSData  *inData;
        inData = [input readDataOfLength:blockSize];
        if (inData.length == 0) {
            break;
        }
        rc = CCCryptorUpdate(cryptor, inData.bytes, inData.length, buffer, outDataLength, &dataOutMoved);
        if (rc != kCCSuccess) {
            NSLog(@"[]CCCryptorUpdate[err=%d]", rc);
            goto CLEAN_UP;
        }

        [output writeData:[NSData dataWithBytesNoCopy:buffer length:dataOutMoved freeWhenDone:NO]];
    }

    rc = CCCryptorFinal(cryptor, buffer, outDataLength, &dataOutMoved);
    if (rc != kCCSuccess) {
        NSLog(@"[]CCCryptorFinal[err=%d]", rc);
        goto CLEAN_UP;
    }
    [output writeData:[NSData dataWithBytesNoCopy:buffer length:dataOutMoved freeWhenDone:NO]];

CLEAN_UP:
    [input closeFile];
    [output closeFile];

    if (cryptor != NULL) {
        CCCryptorRelease(cryptor);
        cryptor = NULL;
    }

    if (buffer != NULL) {
        free(buffer);
        buffer = NULL;
    }

    return rc;
}

@end

0 个答案:

没有答案