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

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

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

现在记录下实现的步骤

链接: http://download.csdn.net/detail/dingkun520wy/9268409 去下载加密资源的脚本,这个是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;
?

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会在这里读取资源。

在bool CCSAXParser::parse(const char *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];

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

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, "rb", &fileSize);
NSData *adata = [[NSData alloc] initWithBytes:pFileData length:fileSize];
delete []pFileData;
jpg = [[UIImage alloc] initWithData:adata];

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, "rb", &nSize);

到此,基本结束了。

在自己程序当中加入资源前把设置密钥和标识和自己加密资源时的一样: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的更多相关文章

  1. 【Quick 3.3】资源脚本加密及热更新(二)资源加密

    [Quick 3.3]资源脚本加密及热更新(二)资源加密 注:本文基于Quick-cocos2dx-3.3版本编写 一.介绍 在前一篇文章中介绍了代码加密,加密方式是XXTEA.对于资源文件来说,同样 ...

  2. Quick 3.3 的代码资源加密

    http://cn.cocos2d-x.org/tutorial/show?id=1507 http://cn.cocos2d-x.org/tutorial/show?id=1447 http://b ...

  3. 转载:Cocos2D-x 游戏接入 Windows 设备所需做的六件事

    原文地址:http://msopentech.com/zh-hans/blog/2014/05/09/cocos2d-x-%E6%B8%B8%E6%88%8F%E6%8E%A5%E5%85%A5-wi ...

  4. [转]eoe社区cocos2d-x游戏引擎知识大汇总

    [eoeAndroid 社区]特意为大家汇总了cocos2d-x知识贴,分量十足,纯正干或.从基础教程到游戏应用的开发,我们不让知识流失,我们要做知识的搬运工还有加工 师.希望大家能够一起的学习,和大 ...

  5. cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发

     cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发 的产生 视持续更新中.... 视频存放地址例如以下:http://ipd.pps.tv/user/1058663622     ...

  6. cocos2d&amp;cocos2dx学习资源

    汇总一下自己学习Cocos2d和cocos2dx认为比較好的一些资源: 书籍: <iPhone&iPad cocos2d游戏开发实战> Steffen Itterheim < ...

  7. 【Cocos2d-X游戏实战开发】捕鱼达人之开发前准备工作(一)

    本系列学习教程使用的是cocos2d-x-2.1.4(最新版为cocos2d-x-2.1.5) 博主发现前两个系列的学习教程被严重抄袭,在这里呼吁大家请尊重开发者的劳动成果, 转载的时候请务必注明出处 ...

  8. 15款Cocos2d-x游戏源码

    (1)用cocos2d-x开发的中国象棋游戏源码 使用Cocos2d-X2.2.3开发的一款中国象棋游戏,游戏中可以实现.新局面.悔棋.游戏音乐.胜利后会显示游戏结果. 源码下载:http://www ...

  9. Cocos2d-x游戏导出android工程,提取cocos的so文件

      Cocos2d-x游戏导出android工程,提取cocos的so文件   原本cocos游戏的android工程编译时,需要将cocos的库文件进行编译,这些文件大部分是cpp文件, 使用ndk ...

随机推荐

  1. ZOJ 3122 Sudoku

    Sudoku Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status ...

  2. 【暴力,STL,水】UVa 1523 - Helicopter

    Since ancient time, people have been dreaming of flying in the sky. Eventually, the dream was realiz ...

  3. Git工作流

    关于git工作原理请参见:http://blog.csdn.net/zdy0_2004/article/details/46552227

  4. poj2243

    Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13433   Accepted: 7518 Des ...

  5. Lombok(1.14.8) - @NonNull

    @NonNull @NonNull,生成一个非空检查. package com.huey.lombok; import lombok.Getter; import lombok.NonNull; im ...

  6. Ubuntu系统下常用的新建、删除、拷贝文件命令

    我们在Ubuntu系统中安装程序时,经常要在usr目录下新建.拷贝文件,此文件夹在Linux类系统中需要root权限才能访问,因此用常规的鼠标右键菜单操作是无效的,今天分享一下在终端中使用命令新建.拷 ...

  7. (转).NET技术+25台服务器怎样支撑世界第54大网站

    英文原文:StackOverflow Update: 560M Pageviews A Month, 25 Servers, And It's All About Performance StackO ...

  8. Java的导入与导出Excel

    使用Jakarta POI导入.导出Excel Jakarta POI 是一套用于访问微软格式文档的Java API.Jakarta POI有很多组件组成,其中有用于操作Excel格式文件的HSSF和 ...

  9. nutch安装配置

    http://nlp.solutions.asia/?p=180 http://www.promenade.me/archives/146 环境 ubuntu 12.04 sql建表 CREATE D ...

  10. 正文字体大小:大 中 小 解决configure: error: Cannot find libmysqlclient under /usr

    今天在64位centos5.6系统上编译PHP5.2.17报错 checking for MySQL support... yes, shared checking for specified loc ...