Resource protection is often encountered in game development.
Currently, files that are often encrypted in game development include images, Lua files, and audio files. encryption is a double-edged sword.
To ensure security, you need to spend a certain amount of resources to implement it. Currently, the texturepacker tool is also used for encryption on the Internet, but it is not targeted enough.
The analysis principle is as follows:
1. format conversion: Convert the files to be encrypted into streams;
2. encryption: You can use encryption methods, such as MD5 and AES, to directly change the displacement and add some special characters to encrypt the file. After encryption, the encryption is basically guaranteed.
The image type cannot be opened even if it cannot be previewed with special software. The Lua file is encrypted with garbled characters ····;
3. Save the custom format file: Save it as a special type of file name, for example, "XX. d" "XX. XYZ.
4. Image decryption: Modify the retrieval path of the underlying cocos2dx Library and the source code modification when loading the ccimage texture processing;
5. Special Lua file interface: Modify the Lua loading method;
After the basic principles are clear, I will post several pieces of encryption methods that are commonly used in my own projects:
First, convert the format and encrypt it.
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;}
You can write an application to traverse the resource files that need to be transferred under the custom directory, and convert and encrypt all resources accordingly;
Newbuf [I] = pbuffer [I] + MD5; this section can be used freely! The password must be decrypted!
Of course, you can also easily put it into your game to modify the ccfileutils: fullpathforfilename method at the bottom of cocos2dx to get the full path;
The decryption is as follows:
To decrypt an image, you must modify the cctexturecache: addimage class of cocos2dx cctexture2d.
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;}
Add your encrypted image type to the ccimage image type, for example, ccimage: XXXXX.
Then follow bool Bret = pimage-> initwithimagefile (fullpath. c_str (), eimageformat );
Ccimage: initwithimagefile method in ccimage. mm;
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;}
Among them, pbuffer [I] = pbuffer [I]-MD5; needs to correspond to the previous encryption!
OK. As long as it is an image and belongs to your custom image type, the actual texture will be decrypted.
The same idea applies to Lua decryption. However, decryption must be performed before the Lua method is loaded. cross-platform decryption is required.
Http://www.cnblogs.com/zisou/p/3850593.html
Cocos2d-x image resource encryption, Lua file encryption ()