http://cn.cocos2d-x.org/tutorial/show?id=2739
Image encryption using Xxtea to encrypt, encryption keys themselves, the idea is that they use the code first to encrypt the image, in the program when used, in the load image resources and then decrypt the resources.
The encryption code is as follows:
1, first to load the header file
2. Encrypt the image
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. Encryption part
/*************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. Files generated after encryption
5, modify the load image resources, and use the decryption
Modify
Add and modify the following code: header file
Bool isendwith (STD::STRING&NBSP;INPUTSTR,STD::STRING&NBSP;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 &NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;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 * &NBSP;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); #else 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 ()); } # endif // emscripten return 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 ()) { &nBsp;ret = initwithimagedata (Data.getbytes (), data.getsize ()); } return ret; }
The
is accompanied by a separate decryption code:
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; &nBSP;} file*fp=fopen (Outfilename.c_str (), "wb+"); if (fp==null) { return false; } fwrite (ret_data, ret_len, 1 , &NBSP;FP); fflush (FP); fclose (FP); cc_safe_delete (Ret_data); return true; }
/*************jiemi************//* std::string outfilename= "/users/liyongchuang/desktop/cocostool/code/myluatest /res/jiemistar.png "; BOOL Jiaemiret=jiemiimg ("Jiamistar.png", Outfilename.c_str ()); if (Jiaemiret) {printf ("-----Success-----\ n"); } else {printf ("------false------\ n"); } */
Modify the code in LUA and use the resources:
Local function main () local gamescene=cc. Scene:create () local ly=cc. Layercolor:create (cc.c4b (0,255,255,255)) gamescene:addchild (ly) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOCAL&NBSP;LB=CC. Label:createwithsystemfont ("Hello world", "Arial",) ly:addchild (LB) lb:setposition (480,320) LOCAL&NBSP;SP=CC. Sprite:create ("Res/jiamistar.lyc") ly:addchild (sp, 10) sp:setposition (CC.P (480,280)) &NBSP;&NBSP;&NBSP;IF&NBSP;CC. Director:getinstance (): Getrunningscene () then &NBSP;CC. Director:getinstance (): Replacescene (Gamescene) &nbsP;&NBSP;&NBSP;&NBSP;ELSE&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CC. Director:getinstance (): Runwithscene (Gamescene) end end
Here's the result:
By the way, we have not found a problem, some of my resources have not been released Oh!
unsigned char *zipfiledata =fileutils::getinstance ()->getfiledata (Zipfilepath.c_str (), "RB", &size); Zipfiledata);
Cocos2d-x Image Resource Encryption