About the solution of cocos2dx mobile game lua file encryption, cocos2dxlua

Source: Internet
Author: User

About the solution of cocos2dx mobile game lua file encryption, cocos2dxlua
Many people who use cocos2dx + lua to play games will think of a problem. Once my game is released, how can we ensure that my script code is not cracked or leaked. Although this is not in line with the principles of open source and sharing, code is also the result of coder's work and should be protected. This is especially true for commercial games. They do not want to be cracked and modified by others.
Today's topic is how to encrypt and decrypt lua script files.
I checked on the network and found the solution.
1. lightweight solution: Before APK is packaged, use tools to encrypt all lua files. Specifically, read the lua files to the memory and use zip and other compressed encryption libraries for compression and encryption, save the compressed and encrypted data as a file with the same name as the source file. When you package and run the lua file, read the lua data first, decrypt it, and pass the decrypted stream data to the lua virtual machine.
2. heavyweight solution. This solution is an extension of the previous solution and also a commercial game solution that implements a game file package, before packaging, you can use tools to package resources and scripts into a file. You can encrypt and compress the files during packaging, or do not compress the files. Then, the data of the corresponding file is directly read from the package during running, decrypted and decompressed, and provided to the game engine for use. This is also a technology widely used by client games. Most mobile games are now using this technology.
This article briefly describes the first scheme, and the second scheme has time to write another blog. Now, let's get started.
First, compress the lua file. The Code is as follows:
Int write_file_content (const char * folder ){
// Obtain file data and compress the file
FILE * fpin = fopen (folder, "wb + ");
If (fpin = NULL)
{
Printf ("file cannot be read: % s \ n", folder );
Return 0;
}
// Get the file size
Fseek (fpin, 0, SEEK_END );
Unsigned int size = ftell (fpin );
// Read the file content
Fseek (fpin, 0, SEEK_SET );
Void * con = malloc (size );
Int r = fread (con, size, 1, fpin );
// Perform Encryption
Unsigned long zip_con_size = size * 2;
Void * zip_con = malloc (zip_con_size );
If (Z_ OK! = Compress (Bytef *) zip_con, & zip_con_size, (Bytef *) con, size )){
Printf ("error occurred when compressing % s \ n", folder );
}
Printf ("% s size before compression: % ld size after compression: % ld \ n", folder, size, zip_con_size); // write the File Content
Fseek (fpin, 0, SEEK_SET );
Int len = fwrite (zip_con, zip_con_size, 1, fpin); // release resources
Fclose (fpin );
Free (zip_con );
Free (con );
Return 0;
}
Copy code
The Code is as follows:
Void * read_file_content (const char * folder, int & bufflen ){
FILE * file = fopen (folder, "wb + ");
If (file)
{
{
Printf ("file cannot be read: % s \ n", folder );
Return 0;
}
// Get the file size
Fseek (file, 0, SEEK_END );
Unsigned int size = ftell (file );
// Read the file content
Void * con = malloc (size );
Fseek (file, 0, SEEK_SET );
Int len = fread (con, size, 1, file );
// Extract
Unsigned long zip_size = size * 4;
Void * zip_con = malloc (zip_size );
Int code = uncompress (Bytef *) zip_con, & zip_size, (Bytef *) con, size); if (Z_ OK! = Code)
{
Printf ("decompression % s error: % d \ n", folder, code); return 0;
}
// Release resources
Fclose (file );
Free (con );
// Zip_con is released from outside
Bufflen = zip_size;
Return zip_con;
}
Copy code
Finally, you can plug the stream file into the virtual machine of lua, that is, run the lua code in the stream mode.
For Android apps, dex source code file security is the most important. Therefore, this dex source code encryption protection is actually necessary. In this aspect, we can achieve the platform of loving encryption, different types of applications have different encryption protection solutions. For details, refer to http://www.ijiami.cn/appprotect_mobile_gamesto modify the Lua file and customize Lua file loading.
Cocos2dx-lua can not use AMF3 to encrypt lua source files

Are you using cocos2dx 3.2? If yes, encryption will be simpler.

For official documentation, see cocos2d-x.org/wiki/Cocos_luacompile.

The. lua file is compiled into the. luac file [. lua to. luac]
Try it in the project file directory
Cocos luacompile-h to view help information
Cocos luacompile-s./projects/MyLuaGame/src-d./projects/MyLuaGame/src-e-k MyLuaKey-B MyLuaSign

./Projects/MyLuaGame/src -- "lua source file directory

./Projects/MyLuaGame/src -- generate luac storage directory

For more information about MyLuaKey and MyLuaSign settings, see AppDelegate. cpp.

Stack-> setXXTEAKeyAndSign ("2 dxLua", strlen ("2 dxLua"), "XXTEA", strlen ("XXTEA "));

Modify the corresponding key and sign.

Teach you a question about porting android to the lua project in cocos2d-x

Build_native.sh




Related Article

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.