Bloqueo (EXC_BAD_ACCESS) al usar CCCrypt

Below is my code to encrypt my string,

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

It prompt me EXC_BAD_ACCESS on line ccStatus = CCCrypt(kCCEncrypt,

Can anyone help to advice?

preguntado el 04 de julio de 12 a las 09:07

Two issues, first, movedBytes is either a NULL or garbage pointer. Second, if you're using ARC, your key pointers are probably getting freed before they're used. Advice: don't do crypto yourself, leave it to the experts and use industry standards like TLS -

@JasonCoco I'm pretty sure DES (Data Encryption Standard) is an industry standard. Probably not the best choice nowadays but it depends what the OP is interacting with. -

@mttrb my point was more toward doing an authorization handshake. Using something higher level is the way to go unless you really know what your doing, and the platform supports TLS over arbitrary protocols so there is really no point in rolling your own auth handshake/user info exchange. -

And given this code snippet already feeds a dangling pointer into CCCrypt... -

@JasonCoco You are assuming that the OP has control over both ends of the communication. It seems much more likely to me that the OP is writing an iOS app to connect to something old. However, if the OP does have control over everything, then DES is a bad choice. -

1 Respuestas

It's the last parameter to CCCrypt. It's trying to output a value (so you know the size of the data its written) and you're giving it a pointer. You need to give it the address of a size_t variable.

Intente algo como esto:

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

Respondido 04 Jul 12, 09:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.