iOS5和iOS6的CCCrypt区别

时间:2023-03-23
本文介绍了iOS5和iOS6的CCCrypt区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个使用 CCCrypt() 的解密/加密方法,它在 iOS5 上运行得非常好.现在我正在使用 iOS6 SDK 并且从未更改过我的代码,但似乎有些东西坏了.我仍然可以使用密钥加密字符串并对其进行解密,但如果我使用另一个密钥解密同一字符串,则从 CCCrypt() 返回的 CCCryptStatus 仍然是 0(kCCSuccess) - 即使解密失败,因为在那之后我的 NSData 没有被填充.在 iOS5 上,我收到了我可以处理的错误消息 -4303.有什么想法现在有什么问题吗?

I have got a decryption/encryption method using CCCrypt() which worked really well on iOS5. Now I am working with the iOS6 SDK and never changed my code, but it seems that something is broken. I can still encrypt a string with a key and decrypt it, but if I use another key to decrypt the same string, the CCCryptStatus coming back from CCCrypt() is still 0(kCCSuccess) - even when the decryption fails, because after that my NSData isn't filled. On iOS5 I got the error message -4303 which I could handle then. Any ideas what can be wrong now?

我的代码:

char keyPtr[kCCKeySizeAES256+1]; 
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

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

if (encryptOrDecrypt == kCCDecrypt)
{
    data = [GTMBase64 decodeData:data];
}

NSUInteger dataLength = [data length];

size_t bufferSize = dataLength + kCCBlockSizeAES128;

void *buffer = malloc(bufferSize);

size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(encryptOrDecrypt,
                                      kCCAlgorithmAES128,
                                      kCCOptionPKCS7Padding,
                                      keyPtr,
                                      kCCKeySizeAES256,
                                      NULL ,
                                      [data bytes], dataLength, 
                                      buffer,       bufferSize, 
                                      &numBytesDecrypted);

if (cryptStatus != kCCSuccess){
    // do something, but cryptStatus is always 0!
}

在 iPad Simulator 5 上对其进行了测试.当我使用另一个密钥进行解密时,我收到的状态是 -4303.仅在 ios6 中返回的状态为 0.

Tested it on iPad Simulator 5. When I make a decryption with another key the status I receive is -4303. Only in ios6 the status coming back is 0.

推荐答案

我不是加密专家,但我有同样的问题并想出了一个解决方法,也许在有人找到真正的解决方案之前会很好.

I'm not an expert i encryption, but I have the same problem and figured a workaround maybe it will be fine until some will find a real solution.

我所做的只是确定哪个 iOS 正在运行,对于 6+,我将 CCCrypt 调用更改为无填充(0 表示无填充,1 是 kCCOptionPKCS7Padding 的枚举)

all I did is to figure which iOS is running and for 6+ i'm changing the CCCrypt call to no padding (0 is for no padding, 1 is the enum for kCCOptionPKCS7Padding)

float version = [[UIDevice currentDevice].systemVersion floatValue];
if (version >= 6)
{
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, 0,
                                          keyPtr, kCCKeySizeAES128,
                                          ivPtr,
                                          [self bytes], dataLength,
                                          buffer, bufferSize, 
                                          &numBytesDecrypted );


    if( cryptStatus == kCCSuccess )
    {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }

    free( buffer ); 
    return nil;
}
else
{
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, 1,
                                          keyPtr, kCCKeySizeAES128,
                                          ivPtr,
                                          [self bytes], dataLength,
                                          buffer, bufferSize, 
                                          &numBytesDecrypted );
    if( cryptStatus == kCCSuccess )
    {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }

    free( buffer );
    return nil;
}

这篇关于iOS5和iOS6的CCCrypt区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:应用程序崩溃 - “DiskImageCache:无法解析旧目录的绝对路径." 下一篇:iOS5.0.1 上的内存泄漏 drawInRect

相关文章