游戲開發中常遇到資源保護的問題。
目前游戲開發中常加密的文件類型有:圖片,Lua文件,音頻等文件,而其實加密也是一把雙刃劍。
需要安全那就得耗費一定的資源去實現它。目前網上也有用TexturePacker工具來加密的,不過針對性還是不夠強。
分析一下原理為:
1,轉格式:將需要加密的文件轉為流的方式;
2,加密:根據自己需要使用加密手段,MD5,AES,甚至可以直接改變位移,加一些自己的特殊字符也可以使文件簡單加密,加密完后基本保證
圖片類型基本用特殊軟件預覽不了也打不開,Lua文件加密后一片亂碼····;
3,保存自定義格式文件:另存為自己特殊類型的文件名如"xx.d" "xx.xyz"等。
4,圖片解密:修改cocos2dx底層庫的獲取路徑處,和加載CCImage紋理處理時的源碼修改;
5,特殊Lua文件界面:修改對應Lua加載方法;
基本原理清楚了后我貼幾段我自己項目中常用的加密方式:
首先是轉格式并且加密的方式
bool PublicCommen::recode_getFileByName(string pFileName){ unsigned long nSize = 0; unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData( pFileName.c_str(), "rb",&nSize); unsigned char* newBuf = new unsigned char[nSize]; int newblen = nSize; if(pBuffer!=NULL&&nSize>0) { for (int i = 0; i<nSize; i++) { newBuf[i]=pBuffer[i]+MD5; } string savepath = pFileName; savepath = savepath.substr(0,savepath.length()-4); savepath = savepath + "xx.X"; FILE *fp = fopen(savepath.c_str(), "wb+"); fwrite(newBuf, 1, newblen, fp); fclose(fp); CCLOG("save file ok. path = %s" ,savepath.c_str()); return true; } return false; }
通常可以自己寫一個應用程序遍歷一下自定義目錄下,需要轉的資源文件,對應的把所有資源轉換并加密;
里面newBuf[i]=pBuffer[i]+MD5;這段可以自由發揮!解密的時候需要對應!
當然你也可以取巧的放進你的游戲中修改cocos2dx底層的CCFileUtils::fullPathForFilename獲取全路徑的方法中;
下面說一下解密:
圖片的解密需要修改cocos2dx186 CCTexture2D 的CCTextureCache41128515::addImage類里面修改
CCTexture2D * CCTextureCache::addImage(const char * path) { CCAssert(path != NULL, "TextureCache: fileimage MUST not be NULL"); CCTexture2D * texture = NULL; CCImage* pImage = NULL; // Split up directory and filename // MUTEX: // Needed since addImageAsync calls this method from a different thread //pthread_mutex_lock(m_pDictLock); std::string pathKey = path; pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(pathKey.c_str()); if (pathKey.size() == 0) { return NULL; } texture = (CCTexture2D*)m_pTextures->objectForKey(pathKey.c_str()); std::string fullpath = pathKey; // (CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(path)); if (! texture) { std::string lowerCase(pathKey); for (unsigned int i = 0; i < lowerCase.length(); ++i) { lowerCase[i] = tolower(lowerCase[i]); } // all images are handled by UIImage except PVR extension that is handled by our own handler do { if (std::string::npos != lowerCase.find(".pvr")) { texture = this->addPVRImage(fullpath.c_str()); } else if (std::string::npos != lowerCase.find(".pkm")) { // ETC1 file format, only supportted on Android texture = this->addETCImage(fullpath.c_str()); } else { CCImage::EImageFormat eImageFormat = CCImage::kFmtUnKnown; if (std::string::npos != lowerCase.find(".png")) { eImageFormat = CCImage::kFmtPng; } else if (std::string::npos != lowerCase.find(".jpg") || std::string::npos != lowerCase.find(".jpeg")) { eImageFormat = CCImage::kFmtJpg; } else if (std::string::npos != lowerCase.find(".tif") || std::string::npos != lowerCase.find(".tiff")) { eImageFormat = CCImage::kFmtTiff; } else if (std::string::npos != lowerCase.find(".webp")) { eImageFormat = CCImage::kFmtWebp; } else if (std::string::npos != lowerCase.find("XX.X")) { eImageFormat = CCImage::xxxxx; } pImage = new CCImage(); CC_BREAK_IF(NULL == pImage); bool bRet = pImage->initWithImageFile(fullpath.c_str(), eImageFormat); CC_BREAK_IF(!bRet); texture = new CCTexture2D(); if( texture && texture->initWithImage(pImage) ) { #if CC_ENABLE_CACHE_TEXTURE_DATA // cache the texture file name VolatileTexture::addImageTexture(texture, fullpath.c_str(), eImageFormat); #endif m_pTextures->setObject(texture, pathKey.c_str()); texture->release(); } else { CCLOG("cocos2d: Couldn't create texture for file:%s in CCTextureCache", path); } } } while (0); } CC_SAFE_RELEASE(pImage); //pthread_mutex_unlock(m_pDictLock); return texture; }
并且在CCImage的圖片類型中添加你加密后的圖片類型如:CCImage::xxxxx
然后跟到 bool bRet = pImage->initWithImageFile(fullpath.c_str(), eImageFormat);
CCImage.mm中的CCImage::initWithImageFile方法;
bool CCImage::initWithImageFile(const char * strPath, EImageFormat eImgFmt/* = eFmtPng*/) { bool bRet = false; unsigned long nSize = 0; unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData( CCFileUtils::sharedFileUtils()->fullPathForFilename(strPath).c_str(), "rb", &nSize); if(eImgFmt==xxxxxx) { for (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的方法前先解密,要考慮跨平臺性
文章列表