gpt4 book ai didi

objective-c - 用ios5加密的CCCrypt无法用ios6解密

转载 作者:可可西里 更新时间:2023-11-01 04:04:19 24 4
gpt4 key购买 nike

我的 cocos2d 游戏使用 CCCrypt() 加密保存数据。我使用 mac 地址作为加密 key 。 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

最佳答案

您需要提供有关如何实现加密的详细信息,尤其是您使用的选项。

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

关于objective-c - 用ios5加密的CCCrypt无法用ios6解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13272383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com