Cocos2d-x PNG Image Resource encryption

Source: Internet
Author: User
Tags emscripten

Implementation principle

If you are not interested in implementing the principle, just want to use it directly, you can skip this section. First of all, I assume that the reader has been aware of the PNG image file structure, if not, it is suggested here to see the "secret Data decryption Key Technology" 5th chapter, this chapter specifically on the PNG image format is introduced. Or look at the PNG file format detailed article.

The principle of implementation is very simple, only the file header of the PNG file and the name of each block of data is removed, special data blocks (such as IHDR and iend) even the contents of the data block are also removed. After that, the feature information for PNG has not been found. But how do you decrypt it? I will remove the information according to a certain format to save, and then AES encryption, and finally append the encrypted data to the end of the file, must be the correct key to decrypt the success of the case. One of the benefits of doing this is that it's more efficient, and a file is probably just about dozens of bytes encrypted without having to encrypt the entire file, which is important for the mobile platform.

How to use

First, the simplest way to encrypt and decrypt. First go here to download my compiled encryption and decryption program, if not run you can compile your own source code to build the program (must support C++11 compiler).

To encrypt a PNG image, place the EncryptPNG.exe file in the file directory where the image resides, and then enter the key to do so. It then automatically encrypts all PNG images in the directory and its subdirectories, and generates the corresponding. epng file.

If you want to verify that the file can be decrypted successfully, simply open the Command window, enter DecryptPNG.exe xxx.epng, and enter the key. If the key is correct, a. png file is generated. It's all very simple, isn't it?

Use on the Cocos2d-x

The use of cocos2d-x is much simpler, as is the way you usually create sprites. For example:

// add "HelloWorld" splash screen "Auto sprite = sprite::create ("helloworld.epng" ); // position the sprite on the center of the 2) + origin); // Add the sprite as a child to this layer this->addchild (sprite);

Just like using a normal PNG image? But before you do, you need to change the engine code. Here, take Cocos2d-x 3.6 as an example, first download this file . Then copy all the files inside the Cocos2d\cocos\base directory and add Base/ccaes.cpp and base/in Cocos2d\cocos\android.mk local_src_files. CCDecryptImage.cpp. Finally, you need to do one more thing, edit the Cocos2d\cocos\platform\ccimage.cpp file, add the header file #include "Base/ccdecryptimage.h", and then modify the Initwithimagefile and Initwithimagefilethreadsafe function, replace the contents of the IF (!data.isnull ()) with the following:

BOOLImage::initwithimagefile (ConstSTD::string&path) {    BOOLRET =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) would be a lot faster than running Libpng et al as compiled with//Emscripten.Sdl_surface *isurf =Img_load (Fullpath.c_str ()); intSize =4* (ISURF-&GT;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);#elseData Data= Fileutils::getinstance ()Getdatafromfile (_filepath); if(!Data.isnull ()) {        if(Splitext (path) [1] ==". Epng") {Auto Image_data=decryptimage (path, data); RET= Initwithimagedata (&image_data[0], image_data.size ()); }        Else{ret=Initwithimagedata (Data.getbytes (), data.getsize ()); }    }#endif //Emscripten    returnret;}BOOLImage::initwithimagefilethreadsafe (ConstSTD::string&FullPath) {    BOOLRET =false; _filepath=FullPath; Data Data= Fileutils::getinstance ()Getdatafromfile (FullPath); if(!Data.isnull ()) {        if(Splitext (FullPath) [1] ==". Epng") {Auto Image_data=decryptimage (fullpath, data); RET= Initwithimagedata (&image_data[0], image_data.size ()); }        Else{ret=Initwithimagedata (Data.getbytes (), data.getsize ()); }    }    returnret;}

Okay, it's done. The key is set in the CCDecryptImage.cpp file Deault_key (default is 123456) and can now run the first code to try to decrypt it successfully.

SOURCE download

C + + implementation of the encryption and decryption of the source code

Https://github.com/zhangpanyi/EncryptPNG/tree/master/cpp

Reprint Please specify source: http://www.cnblogs.com/zhangpanyi/

Cocos2d-x PNG Image Resource encryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.