问题描述
我有一个使用 CCCrypt()
的解密/加密方法,它在iOS5上运行得很好。现在我正在使用iOS6 SDK,从未改变过我的代码,但似乎有些东西坏了。我仍然可以用一个密钥加密一个字符串并进行解密,但是如果我使用另一个密钥对同一个字符串进行解密,则 CCCryptStatus
从 CCCrypt ()
仍然是 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模拟器上测试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;
}
这篇关于CCCrypt iOS5和iOS6之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!