【问题标题】:AES encryption works with iOS5 but not with iOS6AES 加密适用于 iOS5 但不适用于 iOS6
【发布时间】:2013-02-14 03:55:37
【问题描述】:

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

【问题讨论】:

    标签: ios5 encryption ios6


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多