Cocos2dx游戏资源加密之XXTEA

Cocos2dx游戏资源加密之XXTEA,第1张

概述在手机游戏当中,游戏的资源加密保护是一件很重要的事情。 我花了两天的时间整理了自己在游戏当中的资源加密问题,实现了跨平台的资源流加密,这个都是巨人的肩膀之上的。 大概的思路是这样的,游戏资源通过XXTEA加密方法对流的加密方式,有自己的密钥和标识,通过标识可知是否有加密,密钥是自己程序当中的。除非有密钥,否则很难通过解出正确的文件。经过加密后,加密文件也就是游戏资源放在resource的自己文件夹

在手机游戏当中,游戏的资源加密保护是一件很重要的事情。

我花了两天的时间整理了自己在游戏当中的资源加密问题,实现了跨平台的资源流加密,这个都是巨人的肩膀之上的。

大概的思路是这样的,游戏资源通过XXTEA加密方法对流的加密方式,有自己的密钥和标识,通过标识可知是否有加密,密钥是自己程序当中的。除非有密钥,否则很难通过解出正确的文件。经过加密后,加密文件也就是游戏资源放在resource的自己文件夹中,否则在xcode编译到趁机是会识别不了文件。在程序中cocos2dx底层加入解密过程,就可以把文件正确读取出来,让程序显示。经试验,已经可以读取,png,pList,Json文件。

现在记录下实现的步骤

链接: http://pan.baIDu.com/s/1ntx98VZ 密码: qyqe去下载加密资源的脚本,这个是quick-cocos2d-x提取出来的打包工具。

pack_files.sh -i olddir -o newdir -ek XXTEA -es decodetest

把ResourcesDecode和xxtea四个文件加入到cocos2dx/platform下;

把platform/ResourcesDecode.cpp \
platform/xxtea.c \加入到cocos2dx/platform的androID.mk文件中,加入androID编译。

写一个单例用来保存密码和对流解密过程,代码如下:

CCAssert(buf != NulL, "decodeData buf not NulL");    unsigned char* buffer = NulL;    ResourcesDecode* decode = ResourcesDecode::sharedDecode();    bool isXXTEA = decode && decode->m_xxteaEnabled;    for (unsigned int i = 0; isXXTEA && i < decode->m_xxteaSignLen && i < size; ++i)    {        isXXTEA = buf[i] == decode->m_xxteaSign[i];    }    if (isXXTEA)    {        //decrypt XXTEA        xxtea_long  len = 0;        buffer = xxtea_decrypt(buf+decode->m_xxteaSignLen, (xxtea_long)size -(xxtea_long)decode->m_xxteaSignLen, (unsigned char*)decode->m_xxteaKey, (xxtea_long)decode->m_xxteaKeyLen, &len);        delete [] buf;        buf = NulL;        size = len;    }    else    {        buffer = buf;    }    if (pSize)    {        *pSize = size;    }    return buffer;
@H_487_419@

@H_487_419@

@H_487_419@

buffer就是经过XXTEA解密后正确的流。

CCfileUtils::getfileData()当中return返回之前调用解密pBuffer =ResourcesDecode::sharedDecode()->decodeData(pBuffer,size,pSize);这里是跨平台的读取资源的方法。

Zipfile::getfileData()当中也加入解密方法pBuffer =ResourcesDecode::sharedDecode()->decodeData(pBuffer,fileInfo.uncompressed_size,pSize);这个是androID读取pList的地方,我也不太清楚为什么androID会在这里读取资源。

在boolCCSAXParser::parse(constchar*pszfile)中把原先的rt改为rb :char* pBuffer = (char*)CCfileUtils::sharedfileUtils()->getfileData(pszfile,/*"rt"*/"rb",&size);

ios的修改地方 不一样

在CCfileUtilsIOS中的createCCDictionaryWithContentsOffile修改如下,注释掉的是原先的,后面是新增的。

CCDictionary* CCfileUtilsIOS::createCCDictionaryWithContentsOffile(const std::string& filename){    std::string fullPath = CCfileUtils::sharedfileUtils()->fullPathForfilename(filename.c_str());//    Nsstring* pPath = [Nsstring stringWithUTF8String:fullPath.c_str()];//    NSDictionary* pDict = [NSDictionary dictionaryWithContentsOffile:pPath];    unsigned long fileSize = 0;    unsigned char* pfileData = CCfileUtils::sharedfileUtils()->getfileData(fullPath.c_str(), "rb", &fileSize);    NSData *data = [[[NSData alloc] initWithBytes:pfileData length:fileSize] autorelease];    delete []pfileData;    nspropertyListFormat format;    Nsstring *error;    NSMutableDictionary *pDict = (NSMutableDictionary *)[                                                         nspropertyListSerialization propertyListFromData:data                                                         mutabilityOption:nspropertyListMutableContainersAndLeaves                                                         format:&format                                                         errorDescription:&error];
@H_487_419@

@H_487_419@



在CCImage.mm当中修改,同样是注释是原先的,后面是新增的。

@H_487_419@

@H_487_419@

@H_487_419@

static bool _initWithfile(const char* path, timageInfo *pImageinfo){    CGImageRef                CGImage;        UIImage                    *jpg;    UIImage                    *png;    bool            ret;    // convert jpg to png before loading the texture//    Nsstring *fullPath = [Nsstring stringWithUTF8String:path];//    jpg = [[UIImage alloc] initWithContentsOffile: fullPath];unsigned long fileSize = 0;    unsigned char* pfileData = cocos2d::CCfileUtils::sharedfileUtils()->getfileData(path, &fileSize);    NSData *adata = [[NSData alloc] initWithBytes:pfileData length:fileSize];    delete []pfileData;    jpg = [[UIImage alloc] initWithData:adata];
@H_487_419@

@H_487_419@


@H_487_419@androID平台

在CCImageCommon_cpp当中修改如下

bool CCImage::initWithImagefileThreadSafe(const char *fullpath, EImageFormat imageType){    bool bRet = false;    unsigned long nSize = 0;#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)    CCfileUtilsAndroID *fileUitls = (CCfileUtilsAndroID*)CCfileUtils::sharedfileUtils();//    unsigned char *pBuffer = fileUitls->getfileDataForAsync(fullpath,"rb",&nSize);    unsigned char* pBuffer = CCfileUtils::sharedfileUtils()->getfileData(fullpath, &nSize);
@H_487_419@

@H_487_419@

到此,基本结束了。

在自己程序当中加入资源前把设置密钥和标识和自己加密资源时的一样:ResourcesDecode::sharedDecode()->setXXTeaKey("XXTEA",strlen("XXTEA"),"decodetest",strlen("decodetest"));

其它就正常的读取和显示。

参考网址:http://my.oschina.net/SunlightJuly/blog/184061

http://my.oschina.net/SunlightJuly/blog/189971

http://my.oschina.net/SunlightJuly/blog/184179

总结

以上是内存溢出为你收集整理的Cocos2dx游戏资源加密之XXTEA全部内容,希望文章能够帮你解决Cocos2dx游戏资源加密之XXTEA所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1081854.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存