0

我正在编写一个将加密数据发送到外部服务器的应用程序。为此,我编写了加密/解密 NSData 缓冲区的函数。这些功能在 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;
}
4

1 回答 1

0

我使用相同的代码,但没有初始化向量,并且运行良好。也许尝试朝那个方向看。

于 2013-02-13T19:55:37.567 回答