2013-01-09 9 views
5

Sto usando SecKeyEncrypt con una stringa formattata JSON come input. Se supera SecKeyEncrypt una lunghezza di testo normale inferiore a 246, funziona. Se lo passo una lunghezza di 246 o più, fallisce con il valore di ritorno: paramErr (-50).Perché SecKeyEncrypt restituisce paramErr (-50) per le stringhe di input superiori a 246 byte?

Potrebbe trattarsi della stringa stessa. Un esempio di quello che potrei inviare SecKeyEncrypt è:

 
{"handle":"music-list","sym_key":"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALeaEO7ZrjgOFGLBzBHZtQuzH2GNDYMLWP+fIFNu5Y+59C6HECY+jt0yOXXom2mzp/WYYI/9G+Ig8OD6YiKv2nMCAwEAAQ==","app_id":"xgfdt.LibraryTestApp","api_key":"7e080f74de3625b90dd293fc8be560a5cdfafc08"} 

Il carattere 245i è '0'.

L'unico input che sta cambiando tra questo funziona ed è plainTextLength. SecKeyGetBlockSize() restituisce 256 caratteri, quindi qualsiasi input lungo fino a 256 caratteri dovrebbe funzionare.

Ecco il mio metodo Encrypt:

 
+ (NSData*)encrypt:(NSString*)data usingPublicKeyWithTag:(NSString*)tag 
{ 

    OSStatus status = noErr; 

    size_t cipherBufferSize; 
    uint8_t *cipherBuffer; 

    // [cipherBufferSize] 
    size_t dataSize = 246;//[data lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; 
    const uint8_t* textData = [[data dataUsingEncoding:NSUTF8StringEncoding] bytes]; 

    SecKeyRef publicKey = [Encryption copyPublicKeyForTag:tag]; 

    NSAssert(publicKey, @"The public key being referenced by tag must have been stored in the keychain before attempting to encrypt data using it!"); 

    // Allocate a buffer 

    cipherBufferSize = SecKeyGetBlockSize(publicKey); 
    // this value will not get modified, whereas cipherBufferSize may. 
    const size_t fullCipherBufferSize = cipherBufferSize; 
    cipherBuffer = malloc(cipherBufferSize); 

    NSMutableData* accumulatedEncryptedData = [NSMutableData dataWithCapacity:0]; 

    // Error handling 

    for (int ii = 0; ii*fullCipherBufferSize < dataSize; ii++) { 
     const uint8_t* dataToEncrypt = (textData+(ii*fullCipherBufferSize)); 
     const size_t subsize = (((ii+1)*fullCipherBufferSize) > dataSize) ? fullCipherBufferSize-(((ii+1)*fullCipherBufferSize) - dataSize) : fullCipherBufferSize; 

     // Encrypt using the public key. 
     status = SecKeyEncrypt( publicKey, 
           kSecPaddingPKCS1, 
           dataToEncrypt, 
           subsize, 
           cipherBuffer, 
           &cipherBufferSize 
           ); 

     [accumulatedEncryptedData appendBytes:cipherBuffer length:cipherBufferSize]; 
    } 

    if (publicKey) CFRelease(publicKey); 

    free(cipherBuffer); 

    return accumulatedEncryptedData; 
} 

risposta

7

Dalla documentazione:

plainTextLen
Lunghezza in byte dei dati nel buffer non crittografato. Questo deve essere inferiore o uguale al valore restituito dalla funzione SecKeyGetBlockSize. Quando viene eseguito il riempimento PKCS1, la lunghezza massima dei dati che è possibile crittografare è inferiore di 11 byte rispetto al valore restituito dalla funzione SecKeyGetBlockSize (secKeyGetBlockSize() - 11).

(sottolineatura mia)

State usando PKCS1 imbottitura. Pertanto, se la dimensione del blocco è 256, è possibile crittografare solo fino a 245 byte alla volta.

+0

Sì. Duh. Grazie! – Mathew

Problemi correlati