lyctianya的专栏

图片加密使用xxtea来加密,加密秘钥自己定,思路就是自己使用代码首先将图片加密,在程序中使用的时候,,在加载图片资源处再将资源解密

加密代码如下:

首先要加载头文件

2、将图片加密

bool jiamiImg(string inputFileName,string outFileName){string fileName=FileUtils::getInstance()->fullPathForFilename(inputFileName);if(fileName.empty()){return false;}Data fileData=FileUtils::getInstance()->getDataFromFile(fileName);xxtea_long ret_len;unsigned char key[100]="lyctianya";unsigned char* ret_data= xxtea_encrypt(fileData.getBytes(), (xxtea_long)fileData.getSize(),key, (xxtea_long)strlen("lyctianya"), &ret_len);if (ret_data==NULL) {return false;}FILE*fp=fopen(outFileName.c_str(), "wb+");if (fp==NULL) {return false;}fwrite(ret_data, ret_len, 1, fp);fflush(fp);fclose(fp);CC_SAFE_DELETE(ret_data);return true;}3.加密部分 /*************jiami************//*std::string outFileName="/Users/liyongchuang/Desktop/cocosTool/code/myLuaTest/res/jiamiStar.png";bool jiamiRet=jiamiImg("Star.png",outFileName.c_str());if (jiamiRet) {printf("—–success—–\n");}else{printf("——false——\n");}*/4、加密后生成的文件

5、修改加载图片资源处,并使用解密

修改

加入并修改如下代码:头文件自己加

bool isEndWith(std::string inputStr,std::string endStr){if (inputStr.empty()||endStr.empty()){return false;}std::string newEndStr = inputStr.substr(inputStr.find_last_of("."));if (endStr.compare(newEndStr) == 0){return true;}else{return false;}}bool Image::initWithImageFile(const std::string& path){bool ret = false;_filePath = FileUtils::getInstance()->fullPathForFilename(path);#ifdef EMSCRIPTEN// Emscripten includes a re-implementation of SDL that uses HTML5 canvas// operations underneath. Consequently, loading images via IMG_Load (an SDL// API) will be a lot faster than running libpng et al as compiled with// Emscripten.SDL_Surface *iSurf = IMG_Load(fullPath.c_str());int size = 4 * (iSurf->w * iSurf->h);ret = initWithRawData((const unsigned char*)iSurf->pixels, size, iSurf->w, iSurf->h, 8, true);unsigned int *tmp = (unsigned int *)_data;int nrPixels = iSurf->w * iSurf->h;for(int i = 0; i < nrPixels; i++){unsigned char *p = _data + i * 4;tmp[i] = CC_RGB_PREMULTIPLY_ALPHA( p[0], p[1], p[2], p[3] );}SDL_FreeSurface(iSurf);#elseData data;if (isEndWith(_filePath, ".lyc")){Data fileData=FileUtils::getInstance()->getDataFromFile(_filePath);xxtea_long ret_len;unsigned char key[100]="lyctianya";unsigned char*ret_data=xxtea_decrypt(fileData.getBytes(), (xxtea_long)fileData.getSize(), key, strlen("lyctianya"), &ret_len);data.fastSet(ret_data, ret_len);}else{data = FileUtils::getInstance()->getDataFromFile(_filePath);}if (!data.isNull()){ret = initWithImageData(data.getBytes(), data.getSize());}#endif // EMSCRIPTENreturn ret;}bool Image::initWithImageFileThreadSafe(const std::string& fullpath){bool ret = false;_filePath = fullpath;Data data;if (isEndWith(_filePath, ".lyc")){Data fileData=FileUtils::getInstance()->getDataFromFile(_filePath);xxtea_long ret_len;unsigned char key[100]="lyctianya";unsigned char*ret_data=xxtea_decrypt(fileData.getBytes(), (xxtea_long)fileData.getSize(), key, strlen("lyctianya"), &ret_len);data.fastSet(ret_data, ret_len);}else{data = FileUtils::getInstance()->getDataFromFile(_filePath);}if (!data.isNull()){ret = initWithImageData(data.getBytes(), data.getSize());}return ret;}

顺便附上单独解密代码:

bool jiemiImg(string jiaMiFileName,string outFileName){string fileName=FileUtils::getInstance()->fullPathForFilename(jiaMiFileName);if (fileName.empty()) {return false;}Data fileData=FileUtils::getInstance()->getDataFromFile(fileName);xxtea_long ret_len;unsigned char key[100]="lyctianya";unsigned char*ret_data=xxtea_decrypt(fileData.getBytes(),(xxtea_long)fileData.getSize(), key, strlen("lyctianya"), &ret_len);if (ret_data==NULL) {return false;}FILE*fp=fopen(outFileName.c_str(),"wb+");if (fp==NULL) {return false;}fwrite(ret_data, ret_len, 1, fp);fflush(fp);fclose(fp);CC_SAFE_DELETE(ret_data);return true;}

真正的爱,应该超越生命的长度、心灵的宽度、灵魂的深度

lyctianya的专栏

相关文章:

你感兴趣的文章:

标签云: