【问题标题】:CCCrypt encrypted with ios5 can't be decrypted with ios6ios5加密的CCCrypt不能用ios6解密
【发布时间】:2012-10-27 15:59:21
【问题描述】:

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

有什么办法可以解密旧文件?

代码如下:

@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

【问题讨论】:

  • 我在从 iOS4 到 iOS5 时遇到了类似的问题,结果证明这是一个错误,在 4 中被忽略,在 5 中并没有以同样的方式被忽略。在一种情况下缓冲区被填满错误,而另一个缓冲区在块开始时被截断。我建议你寻找类似的东西。
  • 您不应根据 MA​​C 地址加密数据。如果用户购买了新设备,然后从 iTunes 恢复,他们将无法解密您在新设备上保存的数据。

标签: objective-c ios ios5 encryption ios6


【解决方案1】:

您需要详细说明您是如何实施加密的,尤其是您使用了哪些选项。

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

【讨论】:

    【解决方案2】:

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

    这里的重点:

    我在 NSData 中添加了两种方法来使用基于代码的 IOS5 库进行加密和解密。

    @implementation NSData (AESAdditions)
    -(NSData*)AES256EncryptWithKey:(NSString*)key;
    -(NSData *)AES256DecryptWithKey:(NSString *)key
    

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

    在基于代码的 IOS6 中,我在课堂上编写了方法。 像这样:

    - (NSData*)AES256EncryptWithKey:(NSString*)key data:(NSData *)data;
    - (NSData *)AES256DecryptWithKey:(NSString *)key data:(NSData *)data;
    

    代码运行良好,与 IOS5 相同。

    【讨论】:

      猜你喜欢
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 2015-05-01
      • 1970-01-01
      相关资源
      最近更新 更多