首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用ios5加密的CCCrypt不能用ios6解密

用ios5加密的CCCrypt不能用ios6解密
EN

Stack Overflow用户
提问于 2012-11-07 23:06:31
回答 2查看 1.6K关注 0票数 6

我的cocos2d游戏使用CCCrypt()加密来保存数据。我使用mac地址作为加密密钥。在IOS6中,无法使用相同的mac地址解密在IOS5中加密的保存文件。这意味着更新游戏的用户将丢失所有数据!

有没有办法解密旧文件?

代码如下:

代码语言:javascript
复制
@implementation NSData (AESAdditions)
- (NSData*)AES256EncryptWithKey:(NSString*)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) 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* buffer                = malloc(bufferSize);

    size_t numBytesEncrypted    = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);

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

    free(buffer); //free the buffer;
    return nil;
}
- (NSData *)AES256DecryptWithKey:(NSString *)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) 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 *buffer = malloc(bufferSize);

    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesDecrypted);

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

    free(buffer); //free the buffer;
    return nil;
}
@end
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-09 20:14:04

好了,我找到解决方案了。

这里的关键点:

我向NSData添加了两个方法,用于使用基于代码的IOS5库进行加密和解密。

代码语言:javascript
复制
@implementation NSData (AESAdditions)
-(NSData*)AES256EncryptWithKey:(NSString*)key;
-(NSData *)AES256DecryptWithKey:(NSString *)key

现在在IOS6库中,NSData可能会被更改,所以两种方法的工作方式不同,它不能在IOS5中解密加密的文件。

在基于代码的IOS6中,我在类中编写了方法。如下所示:

代码语言:javascript
复制
- (NSData*)AES256EncryptWithKey:(NSString*)key data:(NSData *)data;
- (NSData *)AES256DecryptWithKey:(NSString *)key data:(NSData *)data;

该代码与IOS5中的代码一样工作良好。

票数 -3
EN

Stack Overflow用户

发布于 2012-11-09 03:02:16

您需要详细说明您是如何实现加密的,特别是您使用了哪些选项。

根据我的经验,在iOS 6上解密失败的最常见原因是他们更改/修复的CTR bug。在iOS 5中,有一个选项kCCModeOptionCTR_LE,这是一个谎言。它实际上是用kCCModeOptionCTR_BE加密的。在iOS 6中,他们修复了这个问题,如果你尝试使用kCCModeOptionCTR_LE,你会得到一个“未实现”的错误。但是CCCrypt()通常不使用CTR模式,所以我不知道这是否适用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13272383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档