2014-01-24 2 views
1

다음과 같은 상황이 있습니다. 해독해야하는 NSData가 있습니다. 데이터는 다음과 같이 구성복사하지 않고 NSData에서 하위 데이터 추출

내가 해독에 CCCrypt를 사용하고 있지만, 아마 문제가되지 않는 고정 길이 파일 헤더

  • 암호화 된 내용이이있는 NSData 관련 더 있기 때문에 문제.

    int hdrsize; // this contains the size of the header 
    NSData *data; // this contains full encrypted data with a header 
    
    // this gives me information, stored in the header + some additional stuff 
    NSDictionary *hdr = [self _headerInfoFromData:data]; 
    
    // THIS IS THE PROBLEM AREA 
    data = [data subdataWithRange:NSMakeRange(hdrsize, [data length] - hdrsize)]; 
    
    // And the decryption part 
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, MS_SEC_ENC_ALGORITHM, kCCOptionPKCS7Padding, 
                 [key bytes], MS_SEC_ENC_KEY_SIZE, 
                 [[hdrdict objectForKey:@"iv"] bytes], 
                 [data bytes], dataLength, 
                 buffer, bufferSize, 
    

    당신이 볼 수 있듯이, 여기 내 문제는 암호 해독 내가 헤더 않고있는 NSData의 일부를 추출해야한다는 것입니다 : 내가 (의사) 지금 일을 분리하고있어 방법이다. 그러나 복사하는 대신 이미있는 바이트를 단순히 "재사용"하는 방법이 있습니까? 어쩌면 첫 번째 X 바이트를 건너 뛰고이를 CCCrypt로 전달하는 대신 복사 금지 바이트 버퍼를 만드는 방법이 있습니다. 당신의 도움에 대한

    덕분에

  • 답변

    0

    당신은 바이트를 복사 않습니다 -subdataWithRange: 있음을 확인 했습니까? 그렇다면 항상 +dataWithBytesNoCopy:length:을 사용할 수 있으며 소유권을 올바르게 처리해야합니다.

    수정

    나는 바보입니다. 그냥 다음과 같이하십시오 :

    int hdrsize; // this contains the size of the header 
    NSData *data; // this contains full encrypted data with a header 
    
    // this gives me information, stored in the header + some additional stuff 
    NSDictionary *hdr = [self _headerInfoFromData:data]; 
    
    // And the decryption part 
    CCCryptorStatus cryptStatus = CCCrypt(
        kCCDecrypt, 
        MS_SEC_ENC_ALGORITHM, 
        kCCOptionPKCS7Padding, 
        [key bytes], 
        MS_SEC_ENC_KEY_SIZE, 
        [[hdrdict objectForKey:@"iv"] bytes], 
        data.bytes + hdrsize, 
        data.length - hdrsize, 
        buffer, 
        bufferSize, 
    
    +0

    이는'-dataWithBytesNoCopy : length : freeWhenDone :'이어야합니다. –

    +0

    dataWithBytesNoCopy에 대해 알고 있지만 먼저 X 바이트를 건너 뛸 필요가 있습니다. 그리고 포인터와 물건으로이 작업을 수행 할 수 있다고 가정합니다. 그러나 나는 그저 끔찍한 일입니다. – Marius

    +1

    충분히 간단합니다. 그냥 data.bytes를 포인터로 사용하고 건너 뛸 바이트 수를 추가하십시오. 그러므로 [NSData dataWithBytesNoCopy : data.byes + hdrSize length : data.length - hdrSize freeWhenDone : NO]'라고 말하십시오. –