2013-02-13 4 views
0

Я пишу приложение, которое отправляет зашифрованные данные на внешний сервер. Для этого я написал функции для шифрования/дешифрования буфера NSData. Эти функции отлично работают с iOS5, но не работают с iOS6. Я не модифицировал код. У кого-нибудь есть решение? Вот код:Шифрование AES работает с iOS5, но не с iOS6

- (NSData*) AES256EncryptWithKey :(NSString*)pKey :(NSString*)pIV 
{ 
    // key length is incorrect? 
    if ([pKey length] != kCCKeySizeAES256) 
    { 
     M_LogErrorT("AE - incorrect value - " << [pKey length]); 
     return nil; 
    } 

    // is initialization vector used? 
    bool useIV = (pIV && [pIV length]); 

    // initialization vector length is incorrect? 
    if (useIV && [pIV length] != kCCBlockSizeAES128) 
    { 
     M_LogErrorT("AE - incorrect IV - " << [pIV length]); 
     return nil; 
    } 

// key should be 32 bytes for AES256, will be null-padded otherwise 
char pKeyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused) 
bzero(pKeyPtr, sizeof(pKeyPtr)); // fill with zeroes (for padding) 

// fetch key data 
[pKey getCString:pKeyPtr maxLength:sizeof(pKeyPtr) encoding:NSUTF8StringEncoding]; 

    // initialization vector should be 16 bytes for AES256 
    char pIVPtr[kCCBlockSizeAES128 + 1]; // room for terminator (unused) 
    bzero(pIVPtr, sizeof(pIVPtr));  // fill with zeroes (for padding) 

    // initialization vector is used? 
    if (useIV) 
     // fetch initialization vector data 
     [pIV getCString:pIVPtr maxLength:sizeof(pIVPtr) encoding:NSUTF8StringEncoding]; 

    NSUInteger dataLength = [self length]; 

// see the doc: For block ciphers, the output size will always be less than or 
// equal to the input size plus the size of one block. 
// That's why we need to add the size of one block here 
size_t   bufferSize  = dataLength + kCCBlockSizeAES128; 
void*   pBuffer   = malloc(bufferSize); 
size_t   numBytesEncrypted = 0; 
CCCryptorStatus cryptStatus  = CCCrypt(kCCEncrypt, 
              kCCAlgorithmAES128, 
              kCCOptionPKCS7Padding, 
              pKeyPtr, 
              kCCKeySizeAES256, 
              useIV ? pIVPtr : NULL, /* initialization vector (optional) */ 
              [self bytes], 
              dataLength,   /* input */ 
              pBuffer, 
              bufferSize,   /* output */ 
              &numBytesEncrypted); 

if (cryptStatus == kCCSuccess) 
    // the returned NSData takes ownership of the buffer and will free it on deallocation 
    return [NSData dataWithBytesNoCopy:pBuffer length:numBytesEncrypted]; 

    // free the buffer 
    free(pBuffer); 

return nil; 
} 

- (NSData*) AES256DecryptWithKey :(NSString*)pKey :(NSString*)pIV 
{ 
    // key length is incorrect? 
    if ([pKey length] != kCCKeySizeAES256) 
    { 
     M_LogErrorT("AE - incorrect value - " << [pKey length]); 
     return nil; 
    } 

    // is initialization vector used? 
    bool useIV = (pIV && [pIV length]); 

    // initialization vector length is incorrect? 
    if (useIV && [pIV length] != kCCBlockSizeAES128) 
    { 
     M_LogErrorT("AE - incorrect IV - " << [pIV length]); 
     return nil; 
    } 

// key should be 32 bytes for AES256, will be null-padded otherwise 
char pKeyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused) 
bzero(pKeyPtr, sizeof(pKeyPtr)); // fill with zeroes (for padding) 

// fetch key data 
[pKey getCString:pKeyPtr maxLength:sizeof(pKeyPtr) encoding:NSUTF8StringEncoding]; 

    // initialization vector should be 16 bytes for AES256 
    char pIVPtr[kCCBlockSizeAES128 + 1]; // room for terminator (unused) 
    bzero(pIVPtr, sizeof(pIVPtr));  // fill with zeroes (for padding) 

    // initialization vector is used? 
    if (useIV) 
     // fetch initialization vector data 
     [pIV getCString:pIVPtr maxLength:sizeof(pIVPtr) encoding:NSUTF8StringEncoding]; 

NSUInteger dataLength = [self length]; 

// see the doc: For block ciphers, the output size will always be less than or 
// equal to the input size plus the size of one block. 
// That's why we need to add the size of one block here 
size_t   bufferSize  = dataLength + kCCBlockSizeAES128; 
void*   pBuffer   = malloc(bufferSize); 
size_t   numBytesDecrypted = 0; 
CCCryptorStatus cryptStatus  = CCCrypt(kCCDecrypt, 
              kCCAlgorithmAES128, 
              kCCOptionPKCS7Padding, 
              pKeyPtr, 
              kCCKeySizeAES256, 
              useIV ? pIVPtr : NULL, /* initialization vector (optional) */ 
              [self bytes], 
              dataLength,   /* input */ 
              pBuffer, 
              bufferSize,   /* output */ 
              &numBytesDecrypted); 

if (cryptStatus == kCCSuccess) 
    // the returned NSData takes ownership of the buffer and will free it on deallocation 
    return [NSData dataWithBytesNoCopy:pBuffer length:numBytesDecrypted]; 

    // free the buffer 
    free(pBuffer); 

return nil; 
} 

ответ

0

Я использую тот же код, но без вектора инициализации, и это работает отлично. Возможно, попробуйте посмотреть в этом направлении.

Смежные вопросы