使用CCCrypt时崩溃(EXC_BAD_ACCESS)

时间:2012-07-04 08:01:37

标签: ios c encryption

以下是我加密字符串的代码,

NSString *token = @"us=foo;pw=bar;pwAlg=false;";
NSString *key = @"testtest";

const void *vplainText;
size_t plainTextBufferSize;

plainTextBufferSize = [token length];
vplainText = (const void *) [token UTF8String];

CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t *movedBytes;

bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
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(kCCEncrypt,
                   kCCAlgorithmDES,
                   kCCOptionECBMode,
                   vkey, //"123456789012345678901234", //key
                   kCCKeySizeDES,
                   NULL,// vinitVec, //"init Vec", //iv,
                   vplainText, //"Your Name", //plainText,
                   plainTextBufferSize,
                   (void *)bufferPtr,
                   bufferPtrSize,
                   movedBytes);

NSString *result;
NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
result = [myData base64Encoding];

 NSLog( @"Original String dddd: %@", result );

它在行上提示我EXC_BAD_ACCESS ccStatus = CCCrypt(kCCEncrypt,

任何人都可以提供建议吗?

1 个答案:

答案 0 :(得分:1)

这是CCCrypt的最后一个参数。它试图输出一个值(所以你知道它写的数据的大小),你给它一个指针。您需要为其指定size_t变量的地址。

尝试这样的事情:

size_t movedBytes;
ccStatus = CCCrypt(kCCEncrypt,
                kCCAlgorithmDES,
                kCCOptionECBMode,
                vkey, //"123456789012345678901234", //key
                kCCKeySizeDES,
                NULL,// vinitVec, //"init Vec", //iv,
                vplainText, //"Your Name", //plainText,
                plainTextBufferSize,
                (void *)bufferPtr,
                bufferPtrSize,
                &movedBytes);