我有以下情况:我需要解密一个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 :(得分: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,