In game development, a script as a resource file, like a picture video, is referenced by the engine, the advantage of using a script to play the game is that it can be updated online patch, especially for the Apple App Store audit period is very long.
If the script is not encrypted, malicious people to extract the script file, give you a moment to copy a game out.
1. Xor or encryption decryption
The simplest form of encryption, though simple, is also more practical. However, the anti-cracking aspect is true, if you have other stringent requirements for imitation, you can replace this part of the encryption algorithm your own complex algorithm, but to ensure the decryption efficiency. The following is a simple implementation of C + + to encrypt the file to save to the original file (note that the original unencrypted file backup)
The code is as follows |
Copy Code |
#include "stdafx.h" #include <iostream> #include <ctime> #include <fstream> using namespace Std; void Makecode (char *pstr,int *pkey); void Cutecode (char *pstr,int *pkey); void Encode_file (char *f); int _tmain (int argc, _tchar* argv[]) { Encode_file ("E:/src/resultscene.lua"); /* Encode_file ("E:/src/readyscene.lua"); Encode_file ("E:/src/gamescene.lua"); Encode_file ("E:/src/putheadscene.lua"); Encode_file ("E:/src/tutorialsscene.lua"); Encode_file ("E:/src/wordscategoryscene.lua"); Encode_file ("E:/src/common/dicthelper.lua"); Encode_file ("E:/src/common/lj.lua"); Encode_file ("E:/src/common/dqueue.lua"); Encode_file ("E:/src/common/uihelper.lua"); */ int C; cin>>c; return 0; } void Encode_file (char *f) { FILE *FP = NULL; Fopen_s (&FP, F, "RB"); Fseek (Fp,0,seek_end); Navigate to end of file int nfilelen = Ftell (FP); File length cout << "File len =" << nfilelen << Endl; Fseek (FP, 0, Seek_set); char *filecontent = NULL; Filecontent = (char *) malloc ((Nfilelen + 1) * sizeof (char));//Add one memset (filecontent, 0, Nfilelen + 1); Filecontent[nfilelen] = ';//Last position is end bit fread_s (Filecontent,nfilelen, 1, Nfilelen, FP); Fread (Buf,nfilelen, 1, FP); cout<< "before decryption:" <<fileContent<<endl; Fclose (FP); cout<< "File:" <<f<<endl; cout<< "File size before decryption:" <<strlen (filecontent) <<endl; int Key[]={1, 2, 6, 1, 2, 6};//encrypted characters Char *p=filecontent; cout<< "=====" <<endl; Makecode (Filecontent,key);//encryption cout<< after "Encrypted:" <<p<<endl; cout<< "Encrypted File Size:" <<strlen (filecontent) <<endl; FILE *stream = NULL; Fopen_s (&stream, F, "WB"); if (stream = NULL)/* Open File test.$$$ * * { fprintf (stderr, "Cannot open output file.") "); } else { Fwrite (P, Nfilelen, 1, stream); /* Write the struct file * * Fclose (stream); /* Close File/* } cout<< "=====" <<endl; Cutecode (Filecontent,key);//Decrypt cout<< "After decryption:" <<fileContent<<endl; } Single character XOR or operation Char Makecodechar (char c,int key) { return c=c^key; } Single character decryption Char Cutcodechar (char c,int key) { return c^key; } Encryption void Makecode (char *pstr,int *pkey) { int Len=strlen (PSTR);//Get length for (int i=0;i<len;i++) * (pstr+i) =makecodechar (* (Pstr+i), pkey[i%5]); } Decrypt void Cutecode (char *pstr,int *pkey) { int Len=strlen (PSTR); for (int i=0;i<len;i++) * (pstr+i) =cutcodechar (* (Pstr+i), pkey[i%5]); } |
2. Modify the portal that loads the Lua script file (or JS file) in the Cocos2d-x engine to decrypt it when it is loaded. It is possible that different versions of the engine have different entry files, in the cocos2d-x3.0, corresponding to the int cocos2dx_lua_loader (lua_state *l) method in the file Cocos2dxLuaLoader.cpp file. It is modified to read as follows:
The code is as follows |
Copy Code |
#include "Cocos2dxLuaLoader.h" #include <string> #include <algorithm> #include <iostream> using namespace cocos2d; extern "C" { Single character XOR or operation Char Makecodechar (char c,int key) { return c=c^key; } Single character decryption Char Cutcodechar (char c,int key) { return c^key; } Encryption void Makecode (char *pstr,int *pkey) { int Len=strlen (PSTR);//Get length for (int i=0;i<len;i++) * (pstr+i) =makecodechar (* (Pstr+i), pkey[i%5]); } Decrypt void Cutecode (char *pstr,int *pkey) { int Len=strlen (PSTR); for (int i=0;i<len;i++) * (pstr+i) =cutcodechar (* (Pstr+i), pkey[i%5]); } int Cocos2dx_lua_loader (lua_state *l) { std::string filename (lual_checkstring (L, 1)); size_t pos = Filename.rfind (". Lua"); if (POS!= std::string::npos) { filename = filename.substr (0, POS); }
pos = filename.find_first_of ("."); while (POS!= std::string::npos) { Filename.replace (POS, 1, "/"); pos = filename.find_first_of ("."); } Filename.append (". Lua");
Data data = Fileutils::getinstance ()->getdatafromfile (filename);
if (!data.isnull ()) {
====code Decode start================================== Log ("===encode filename:%s===", Filename.c_str ()); if filename = = ' Main.lua ', decrypt Char *filecontent = (char*) data.getbytes (); int Key[]={1, 2, 6, 1, 2, 6};//encrypted characters char *filecontentdecoded = NULL; if (strcmp (Filename.c_str (), "Readyscene.lua") ==0 | | strcmp (Filename.c_str (), "Gamescene.lua") ==0 | | strcmp (Filename.c_str (), "Putheadscene.lua") ==0 | | strcmp (Filename.c_str (), "Tutorialsscene.lua") ==0 | | strcmp (Filename.c_str (), "Wordscategoryscene.lua") ==0 | | strcmp (Filename.c_str (), "Dicthelper.lua") ==0 | | strcmp (Filename.c_str (), "Lj.lua") ==0 | | strcmp (Filename.c_str (), "Resultscene.lua") ==0 | | strcmp (Filename.c_str (), "Dqueue.lua") ==0 | | strcmp (Filename.c_str (), "Uihelper.lua") ==0) { if (Data.getsize () < strlen (filecontent)) { filecontentdecoded = (char *) malloc (data.getsize () + 1) * sizeof (char));//Add one bit memset (filecontentdecoded, 0, Data.getsize () + 1); Filecontentdecoded[data.getsize ()] = ';//Last position is end bit strncpy (Filecontentdecoded,filecontent,data.getsize ()); Filecontent = NULL; } else { filecontentdecoded = filecontent; } Cutecode (Filecontentdecoded,key);//Decrypt } else { filecontentdecoded = filecontent; } ====code Decode end==================================
if (Lual_loadbuffer (L, filecontentdecoded, Data.getsize (), Filename.c_str ())!= 0) { Lual_error (L, "Error loading module%s from file%s: %s ", Lua_tostring (L, 1), Filename.c_str (), lua_tostring (L,-1)); } } Else { Log ("Can not get file data of%s", Filename.c_str ()); } return 1; } } |
Note that encryption and decryption key guarantees consistent.
Code comments should be pretty perfect, no explanations, enjoy~.