мы используем CCryptCreate и CCryptUpdate с дополнением PKCS7 в нашем приложении. Он отлично работает в OS X 10.7 и 10.8, но в OS X 10.9 не работает. Когда я пытаюсь расшифровать с помощью заполнения PKCS7, CCCryptorUpdate возвращает -4301, ошибка kCCBufferTooSmall. Напротив, при отсутствии заполнения 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
outDataLength * 10
в вашем коде? Это кажется немного странным. Вы прошли через свой код с помощью отладчика, чтобы просмотреть, какие размеры созданы? - person Maarten Bodewes   schedule 13.11.2013buffer = malloc(outDataLength);
. Поскольку CCCryptorUpdate возвращал ошибку kCCBufferTooSmall в OX 10.9, я заменил ее наbuffer = malloc(outDataLength * 10);
. Он вернул ошибку kCCBufferTooSmall, как и до замены. - person user2984559   schedule 13.11.2013kCCOptionPKCS7Padding | kCCOptionECBMode
не противоречит? Я думаю, вы должны выбрать один или другой, не так ли? stackoverflow.com/a/1204507/220338 - person hyperspasm   schedule 02.02.2014goto
, правда?), это, вероятно, является причиной ошибок. - person Krystian   schedule 10.07.2017