使用CCCrypt加密/解密文件

时间:2013-03-28 05:37:48

标签: ios encryption

我不知道为什么原始mp3Data大小是4750347字节,而decryptData大小是4750344字节 TripleDES方法中是否存在任何实现错误?
谢谢你的回复。

- (void) testTripleDESMp3
{
    NSString * mp3Path = [[self libraryPath]stringByAppendingPathComponent:@"A.mp3"];
    NSData *mp3Data = [[NSData alloc] initWithContentsOfFile:mp3Path] ;//4750347
    NSData *encryptData = [self TripleDES:mp3Data encryptOrDecrypt:kCCEncrypt key:@"12345"];//bufferPtrSize 4750352, plainTextBufferSize:4750347, movedBytes:47503520 ,NSData 6333804
    NSData *decryptData = [self TripleDES:encryptData encryptOrDecrypt:kCCDecrypt key:@"12345"];//bufferPtrSize 4750360, plainTextBufferSize:4750352, movedBytes:4750344,NSData 4750344
    //mp3Data 4750347 -> encryptData 6333804-> decryptData 4750344

    NSError* error;
    NSString * mp3Path2 = [[self libraryPath]stringByAppendingPathComponent:@"B.mp3"];
    [decryptData writeToFile:mp3Path2 options:NSDataWritingAtomic error:&error];

}

- (NSData*)TripleDES:(NSData*)plainData encryptOrDecrypt:(CCOperation)encryptOrDecrypt key:(NSString*)key {

    const void *vplainText;
    size_t plainTextBufferSize;

    if (encryptOrDecrypt == kCCDecrypt)
    {
        NSData *EncryptData = [GTMBase64 decodeData:plainData];
        plainTextBufferSize = [EncryptData length];
        vplainText = [EncryptData bytes];
    }
    else
    {
        plainTextBufferSize = [plainData length];
        vplainText = (const void *)[plainData bytes];
    }

    CCCryptorStatus ccStatus;

    //I am not sure use char*/void*/uint8_t
    uint8_t *bufferPtr = NULL;

    size_t bufferPtrSize = 0;
    size_t movedBytes = 0;

    bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);

    bufferPtr = static_cast<uint8_t *>(malloc( bufferPtrSize * sizeof(uint8_t)));
    //bufferPtr = (char *)malloc( bufferPtrSize * sizeof(char));

    memset((void *)bufferPtr, 0x0, bufferPtrSize);
    // memset((void *) iv, 0x0, (size_t) sizeof(iv));

    NSString *initVec = @"init Vec";
    const void *vkey = (const void *) [key UTF8String];
    const void *vinitVec = (const void *) [initVec UTF8String];

    ccStatus = CCCrypt(encryptOrDecrypt,
                       kCCAlgorithm3DES,
                       kCCOptionPKCS7Padding,//kCCOptionECBMode,
                       vkey, 
                       kCCKeySize3DES,
                       vinitVec, //"init Vec", //iv,
                       vplainText, //"Your Name", //plainText,
                       plainTextBufferSize,
                       (void *)bufferPtr,
                       bufferPtrSize,
                       &movedBytes);
    if (ccStatus == kCCSuccess) NSLog(@"SUCCESS");
    else if (ccStatus == kCCParamError) NSLog( @"PARAM ERROR");
    else if (ccStatus == kCCBufferTooSmall) NSLog( @"BUFFER TOO SMALL");
    else if (ccStatus == kCCMemoryFailure) NSLog( @"MEMORY FAILURE");
    else if (ccStatus == kCCAlignmentError) NSLog( @"ALIGNMENT");
    else if (ccStatus == kCCDecodeError) NSLog( @"DECODE ERROR");
    else if (ccStatus == kCCUnimplemented) NSLog( @"UNIMPLEMENTED");

    NSData *result;

    if (encryptOrDecrypt == kCCDecrypt)
    {
        result = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
    }
    else
    {
        NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
        result = [GTMBase64 encodeData:myData];
    }
    //bufferPtrSize 1931240, plainTextBufferSize:1931232, movedBytes:1931232
    NSLog(@"bufferPtrSize %d, plainTextBufferSize:%d, movedBytes:%d", (int)bufferPtrSize,(int)plainTextBufferSize, (int)movedBytes);//
    return result;
}

0 个答案:

没有答案