http://cn.cocos2d-x.org/tutorial/show?id=2739
图片加密使用xxtea来加密,加密秘钥自己定,思路就是自己使用代码首先将图片加密,在程序中使用的时候,在加载图片资源处再将资源解密。
加密代码如下:
1、首先要加载头文件
2、将图片加密
booljiamiImg(stringinputFileName,stringoutFileName) { stringfileName=FileUtils::getInstance()->fullPathForFilename(inputFileName); if(fileName.empty()) { returnfalse; } DatafileData=FileUtils::getInstance()->getDataFromFile(fileName); xxtea_longret_len; unsignedcharkey[100]="lyctianya"; unsignedchar*ret_data=xxtea_encrypt(fileData.getBytes(),(xxtea_long)fileData.getSize(),key,(xxtea_long)strlen("lyctianya"),&ret_len); if(ret_data==NULL){ returnfalse; } FILE*fp=fopen(outFileName.c_str(),"wb+"); if(fp==NULL){ returnfalse; } fwrite(ret_data,ret_len,1,fp); fflush(fp); fclose(fp); CC_SAFE_DELETE(ret_data); returntrue; }
3、加密部分
/*************jiami************/ /* std::stringoutFileName="/Users/liyongchuang/Desktop/cocosTool/code/myLuaTest/res/jiamiStar.png"; booljiamiRet=jiamiImg("Star.png",outFileName.c_str()); if(jiamiRet){ printf("-----success----- "); } else { printf("------false------ "); } */
4、加密后生成的文件
5、修改加载图片资源处,并使用解密
修改
加入并修改如下代码:头文件自己加
boolisEndWith(std::stringinputStr,std::stringendStr) { if(inputStr.empty()||endStr.empty()) { returnfalse; } std::stringnewEndStr=inputStr.substr(inputStr.find_last_of(".")); if(endStr.compare(newEndStr)==0) { returntrue; } else { returnfalse; } } boolImage::initWithImageFile(conststd::string&path) { boolret=false; _filePath=FileUtils::getInstance()->fullPathForFilename(path); #ifdefEMSCRIPTEN //Emscriptenincludesare-implementationofSDLthatusesHTML5canvas //operationsunderneath.Consequently,loadingimagesviaIMG_Load(anSDL //API)willbealotfasterthanrunninglibpngetalascompiledwith //Emscripten. SDL_Surface*iSurf=IMG_Load(fullPath.c_str()); intsize=4*(iSurf->w*iSurf->h); ret=initWithRawData((constunsignedchar*)iSurf->pixels,size,iSurf->w,iSurf->h,8,true); unsignedint*tmp=(unsignedint*)_data; intnrPixels=iSurf->w*iSurf->h; for(inti=0;i<nrPixels;i++) { unsignedchar*p=_data+i*4; tmp[i]=CC_RGB_PREMULTIPLY_ALPHA(p[0],p[1],p[2],p[3]); } SDL_FreeSurface(iSurf); #else Datadata; if(isEndWith(_filePath,".lyc")) { DatafileData=FileUtils::getInstance()->getDataFromFile(_filePath); xxtea_longret_len; unsignedcharkey[100]="lyctianya"; unsignedchar*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//EMSCRIPTEN returnret; } boolImage::initWithImageFileThreadSafe(conststd::string&fullpath) { boolret=false; _filePath=fullpath; Datadata; if(isEndWith(_filePath,".lyc")) { DatafileData=FileUtils::getInstance()->getDataFromFile(_filePath); xxtea_longret_len; unsignedcharkey[100]="lyctianya"; unsignedchar*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()); } returnret; }
顺便附上单独解密代码:
booljiemiImg(stringjiaMiFileName,stringoutFileName) { stringfileName=FileUtils::getInstance()->fullPathForFilename(jiaMiFileName); if(fileName.empty()){ returnfalse; } DatafileData=FileUtils::getInstance()->getDataFromFile(fileName); xxtea_longret_len; unsignedcharkey[100]="lyctianya"; unsignedchar*ret_data=xxtea_decrypt(fileData.getBytes(),(xxtea_long)fileData.getSize(),key,strlen("lyctianya"),&ret_len); if(ret_data==NULL){ returnfalse; } FILE*fp=fopen(outFileName.c_str(),"wb+"); if(fp==NULL){ returnfalse; } fwrite(ret_data,ret_len,1,fp); fflush(fp); fclose(fp); CC_SAFE_DELETE(ret_data); returntrue; }
/*************jiemi************/ /* std::stringoutFileName="/Users/liyongchuang/Desktop/cocosTool/code/myLuaTest/res/jiemiStar.png"; booljiaemiRet=jiemiImg("jiamiStar.png",outFileName.c_str()); if(jiaemiRet){ printf("-----success----- "); } else { printf("------false------ "); } */
修改lua中的代码,并使用资源:
localfunctionmain() localgameScene=cc.Scene:create() locally=cc.LayerColor:create(cc.c4b(0,255,255,255)) gameScene:addChild(ly) locallb=cc.Label:createWithSystemFont("Helloworld","Arial",20) ly:addChild(lb) lb:setPosition(480,320) localsp=cc.Sprite:create("res/jiamiStar.lyc") ly:addChild(sp,10) sp:setPosition(cc.p(480,280)) ifcc.Director:getInstance():getRunningScene()then cc.Director:getInstance():replaceScene(gameScene) else cc.Director:getInstance():runWithScene(gameScene) end end
下面看结果:
顺便提一下,大家有没有发现一个问题,我的一些资源用了没有释放哦!
unsignedchar*zipFileData=FileUtils::getInstance()->getFileData(zipFilePath.c_str(),"rb",&size); free(zipFileData);