1

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

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 );

它在ccStatus = CCCrypt(kCCEncrypt,

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

4

1 回答 1

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); 
于 2012-07-04T08:27:52.017 回答