cocos2dx使用xxtea加密资源

记录在cocos2dx下使用xxtea加密,以下都为ios版本操作

1.资源加密

quick有一个加密资源和脚本的解决方案,即使用xxtea加密并且可以进行打包,在pack_files文件夹下有一个pack_files.sh本人使用的mac,中windows中使用.bat

脚本命令的详细讲解,在这里只是使用了对资源进行加密,不进行打包。脚本命令如下

[plain]  view plain  copy
  1. ./pack_files.sh -i ./res -o ./resnew -ek key -es sign  
1.资源读取

解密涉及到对cocos2dx引擎的一些改动,所以写了个ResourcesDecode类,cocos2dx本身含有xxtea类文件,把这2个类文件放入到项目cocos2d/cocos/platform下,在cocos2d.h文件夹中加入2个类的引用,在cocos2d/build目录下把2个类加入到ios的静态类库中,在AppDelegate.cpp场景切换之前加入

[cpp]  view plain  copy
  1. ResourcesDecode::getInstance()->setXXTEAKeyAndSign("key""sign");  
具体的方法会在 ResourcesDecode中介绍,会上传文件。

修改image.h中Image::initWithImageFile方法中做如下更改

[cpp]  view plain  copy
  1. Data data = ResourcesDecode::getInstance()->decodeData(path.c_str());  
ios平台文件中的 apple/CCFileUtils中修改FileUtilsApple::getValueMapFromFile中做如下更改

[cpp]  view plain  copy
  1. Data data = ResourcesDecode::getInstance()->decodeData(fullPath.c_str());  
  2. ssize_t bufferSize = data.getSize();  
  3. unsigned char* pFileData = data.getBytes();  
  4. NSData *nsdata = [[[NSData alloc] initWithBytes:pFileData length:bufferSize] autorelease];  
  5.           
  6. NSPropertyListFormat format;  
  7. NSString *error;  
  8. NSMutableDictionary *dict = (NSMutableDictionary *)[  
  9.                                                     NSPropertyListSerialization propertyListFromData:nsdata  
  10.                                                     mutabilityOption:NSPropertyListMutableContainersAndLeaves  
  11.                                                     format:&format  
  12.                                                     errorDescription:&error];  
  13. //over  

读取json文件还需在FileUtils::getStringFromFile中做如下更改

[cpp]  view plain  copy
  1. Data data = ResourcesDecode::getInstance()->decodeData(path.c_str());  
读取tilemap文件在SAXParser::parse下做如下更改

[cpp]  view plain  copy
  1. Data data = ResourcesDecode::getInstance()->decodeData(path.c_str());  

上面的decodeData到底做了什么操作呢,其实就是读取到资源再对它解密操作

[cpp]  view plain  copy
  1. Data ResourcesDecode::decodeData(const char *fileName)  
  2. {  
  3.     Data ret;  
  4.     ssize_t dsize = 0;  
  5.     unsigned char* dresult;  
  6.     unsigned char* buffer = nullptr;  
  7.     size_t size = 0;  
  8.     size_t readsize;  
  9.     auto fileutils = FileUtils::getInstance();  
  10.     do  
  11.     {  
  12.         // Read the file from hardware  
  13.         std::string fullPath = fileutils->fullPathForFilename(fileName);  
  14.         FILE *fp = fopen(fullPath.c_str(), "rb");  
  15.         CC_BREAK_IF(!fp);  
  16.         fseek(fp,0,SEEK_END);  
  17.         size = ftell(fp);  
  18.         fseek(fp,0,SEEK_SET);  
  19.           
  20.         buffer = (unsigned char*)malloc(sizeof(unsigned char) * size);  
  21.         readsize = fread(buffer, sizeof(unsigned char), size, fp);  
  22.         fclose(fp);  
  23.           
  24.     } while (0);  
  25.       
  26.     if (nullptr == buffer || 0 == readsize)  
  27.     {  
  28.         CCLOG("Get data from file %s failed", fileName);  
  29.     }  
  30.     if (_xxteaEnabled && strncmp((char*)buffer, _xxteaSign, _xxteaSignLen) == 0)  
  31.     {  
  32.         // decrypt XXTEA  
  33.         xxtea_long len = 0;  
  34.         dresult = xxtea_decrypt(buffer + _xxteaSignLen,  
  35.                                 (xxtea_long)readsize - _xxteaSignLen,  
  36.                                 (unsigned char*)_xxteaKey,  
  37.                                 (xxtea_long)_xxteaKeyLen,  
  38.                                 &len);  
  39.         dsize = len;  
  40.         ret.fastSet(dresult, dsize);  
  41.     }  
  42.     if (buffer) {  
  43.         free(buffer);  
  44.     }  
  45.     return ret;  
  46. }  

可以参考 FileUtils ::getFileData中的处理。
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
A:在cocos2dx中,可以通过加密lua代码和图片资源来保护项目的安全性。下面是一些简单的步骤来加密和解密lua代码和图片资源: 1. 加密Lua代码 可以使用luajit来编译Lua代码,然后使用gzip压缩和base64编码来加密Lua代码。以下是使用此方法加密Lua代码的示例代码: ``` local status, bytecodes = pcall(function() local bytecodeStream = io.popen("luajit -bg myScript.lua -") local bytecode = bytecodeStream:read("*all") bytecodeStream:close() return bytecode end) if not status then error(bytecodes) end local compressed = zlib.compress(bytecodes, 9) local cipher = mime.b64(compressed) ``` 2. 加密图片资源 可以使用图片加密工具,如TexturePacker,将图片打包成一个txp包。然后可以使用AES加密算法来加密txp包,以下是使用此方法加密图片资源的示例代码: ``` local Crypto = require "crypto" local data = cc.FileUtils:getInstance():getDataFromFile("myImage.txp") local iv = Crypto.digest("sha256", "mySecretKey", true) local key = Crypto.digest("md5", "mySecretKey", true) local encrypted = Crypto.encryptAES256(data, key, iv) cc.FileUtils:getInstance():writeDataToFile(encrypted, "myImageEncrypted") ``` 3. 解密Lua代码 可以使用base64解码和gzip解压缩函数解密加密的Lua代码,然后使用loadstring函数来运行解密后的代码。以下是使用此方法解密Lua代码的示例代码: ``` local cipher = "..." local compressed = mime.unb64(cipher) local bytecodes = zlib.decompress(compressed) local f = loadstring(bytecodes) f() ``` 4. 解密图片资源 使用AES解密算法来解密加密的图片资源。以下是使用此方法解密图片资源的示例代码: ``` local Crypto = require "crypto" local encrypted = cc.FileUtils:getInstance():getDataFromFile("myImageEncrypted") local iv = Crypto.digest("sha256", "mySecretKey", true) local key = Crypto.digest("md5", "mySecretKey", true) local decrypted = Crypto.decryptAES256(encrypted, key, iv) cc.FileUtils:getInstance():writeDataToFile(decrypted, "myImage") ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值