【cocos2d-x 手游研发小技巧(7)图片资源加密,Lua文件加密】

前端之家收集整理的这篇文章主要介绍了【cocos2d-x 手游研发小技巧(7)图片资源加密,Lua文件加密】前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

游戏开发中常遇到资源保护的问题。

目前游戏开发中常加密的文件类型有:图片,Lua文件,音频等文件,而其实加密也是一把双刃剑。

需要安全那就得耗费一定的资源去实现它。目前网上也有用TexturePacker工具来加密的,不过针对性还是不够强。

分析一下原理为:

1,转格式:将需要加密的文件转为流的方式;

2,加密:根据自己需要使用加密手段,MD5,AES,甚至可以直接改变位移,加一些自己的特殊字符也可以使文件简单加密,加密完后基本保证

图片类型基本用特殊软件预览不了也打不开,Lua文件加密后一片乱码····;

3,保存自定义格式文件:另存为自己特殊类型的文件名如"xx.d" "xx.xyz"等。

4,图片解密:修改cocos2dx底层库的获取路径处,和加载CCImage纹理处理时的源码修改

5,特殊Lua文件界面:修改对应Lua加载方法

基本原理清楚了后我贴几段我自己项目中常用的加密方式:

首先是转格式并且加密的方式

  1. bool PublicCommen::recode_getFileByName(string pFileName){
  2. unsigned long nSize = 0;
  3. unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData(
  4. pFileName.c_str(),"rb",&nSize);
  5. unsigned char* newBuf = new unsigned char[nSize];
  6. int newblen = nSize;
  7. if(pBuffer!=NULL&&nSize>0)
  8. {
  9. for (int i = 0; i<nSize; i++) {
  10. newBuf[i]=pBuffer[i]+MD5;
  11. }
  12. string savepath = pFileName;
  13. savepath = savepath.substr(0,savepath.length()-4);
  14. savepath = savepath + xx.X";
  15. FILE *fp = fopen(savepath.c_str(),wb+");
  16. fwrite(newBuf,128); line-height:1.5!important">1,newblen,fp);
  17. fclose(fp);
  18. CCLOG(save file ok. path = %s",savepath.c_str());
  19. return true;
  20. }
  21. false;
  22. }

通常可以自己写一个应用程序遍历一下自定义目录下,需要转的资源文件,对应的把所有资源转换并加密;

里面newBuf[i]=pBuffer[i]+MD5;这段可以自由发挥!解密的时候需要对应!

当然你也可以取巧的放进你的游戏中修改cocos2dx底层的CCFileUtils::fullPathForFilename获取全路径的方法中;

下面说一下解密:

图片的解密需要修改cocos2dx CCTexture2D 的CCTextureCache::addImage类里面修改

  1. CCTexture2D * CCTextureCache::addImage(const char * path)
  2. {
  3. CCAssert(path != NULL,0); line-height:1.5!important">TextureCache: fileimage MUST not be NULL");
  4. CCTexture2D * texture = NULL;
  5. CCImage* pImage = NULL;
  6. // Split up directory and filename
  7. MUTEX:
  8. Needed since addImageAsync calls this method from a different thread
  9. pthread_mutex_lock(m_pDictLock);
  10. std::string pathKey = path;
  11. pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(pathKey.c_str());
  12. if (pathKey.size() == return NULL;
  13. }
  14. texture = (CCTexture2D*)m_pTextures->objectForKey(pathKey.c_str());
  15. std::string fullpath = pathKey; (CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(path));
  16. if (! texture)
  17. {
  18. std::string lowerCase(pathKey);
  19. for (unsigned 0; i < lowerCase.length(); ++i)
  20. {
  21. lowerCase[i] = tolower(lowerCase[i]);
  22. }
  23. all images are handled by UIImage except PVR extension that is handled by our own handler
  24. do
  25. {
  26. if (std::string::npos != lowerCase.find(.pvr"))
  27. {
  28. texture = this->addPVRImage(fullpath.c_str());
  29. }
  30. else .pkm"))
  31. {
  32. ETC1 file format,only supportted on Android
  33. texture = this->addETCImage(fullpath.c_str());
  34. }
  35. else
  36. {
  37. CCImage::EImageFormat eImageFormat = CCImage::kFmtUnKnown;
  38. .png"))
  39. {
  40. eImageFormat = CCImage::kFmtPng;
  41. }
  42. .jpg") || std::.jpeg"))
  43. {
  44. eImageFormat = CCImage::kFmtJpg;
  45. }
  46. .tif.tiff"))
  47. {
  48. eImageFormat = CCImage::kFmtTiff;
  49. }
  50. .webp"))
  51. {
  52. eImageFormat = CCImage::kFmtWebp;
  53. }
  54. XX.X"))
  55. {
  56. eImageFormat = CCImage::xxxxx;
  57. }
  58. pImage = new CCImage();
  59. CC_BREAK_IF(NULL == pImage);
  60. bool bRet = pImage->initWithImageFile(fullpath.c_str(),eImageFormat);
  61. CC_BREAK_IF(!bRet);
  62. texture = new CCTexture2D();
  63. if( texture &&
  64. texture->initWithImage(pImage) )
  65. {
  66. #if CC_ENABLE_CACHE_TEXTURE_DATA
  67. cache the texture file name
  68. VolatileTexture::addImageTexture(texture,fullpath.c_str(),eImageFormat);
  69. #endif
  70. m_pTextures->setObject(texture,pathKey.c_str());
  71. texture->release();
  72. }
  73. else
  74. {
  75. CCLOG(cocos2d: Couldn't create texture for file:%s in CCTextureCachewhile (0);
  76. }
  77. CC_SAFE_RELEASE(pImage);
  78. pthread_mutex_unlock(m_pDictLock);
  79. return texture;
  80. }

并且在CCImage的图片类型中添加你加密后的图片类型如:CCImage::xxxxx

然后跟到 bool bRet = pImage->initWithImageFile(fullpath.c_str(),eImageFormat);

CCImage.mm中的CCImage::initWithImageFile方法

bool CCImage::initWithImageFile(char * strPath,EImageFormat eImgFmt/* = eFmtPng*/) { bool bRet = false; unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData( CCFileUtils::sharedFileUtils()->fullPathForFilename(strPath).c_str(),&nSize); if(eImgFmt==xxxxxx) { int i= 0; i < nSize; i++) { pBuffer[i] = pBuffer[i]-MD5; } pBuffer[nSize] = pBuffer[nSize]-1; eImgFmt = kFmtPng; } if (pBuffer != NULL && nSize > 0) { bRet = initWithImageData(pBuffer,nSize,eImgFmt); } CC_SAFE_DELETE_ARRAY(pBuffer); return bRet; }

其中pBuffer[i] = pBuffer[i]-MD5;需要和之前加密的时候对应·····自己发挥!

Ok,只要是图片,并且是属于你自定义类型的图片都会得到解密的真实texture

Lua的解密也是基本一样的思路,不过解密需要单独在需要加载Lua的方法前先解密,要考虑跨平台性


转自http://www.cnblogs.com/zisou/p/cocos2dxJQ-67.html

猜你在找的Cocos2d-x相关文章