Solution to Lua file encryption in cocos2dx mobile games

Source: Internet
Author: User

Many people who use cocos2dx + Lua to play games will think of a problem. Once my game is published, how can we ensure that my script code will not be 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 or modified by others.

Today's topic is how to encrypt and decrypt Lua script files.

I have checked on the network and there is no mature solution. After consideration, I will summarize two solutions for you to take the exam.

1. lightweight solution: before packaging the APK, use tools to encrypt all the Lua files, read the Lua files into the memory in detail, and then 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 the Lua file is packaged and executed, the Lua data is read, decrypted, And the decrypted stream data is transmitted 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, which can be encrypted or compressed during packaging. Then, the data of the corresponding file is read directly from the package during execution, 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 also starting to use this technology.

This article briefly describes the first scheme, and the other 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 the file data and compress the 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); // 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 % s is compressed \ 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 the resource fclose (fpin); free (zip_con); free (CON); Return 0 ;}

The Code is as follows:

Void * read_file_content (const char * folder, Int & bufflen) {file * file = fopen (folder, "WB +"); If (file) {printf ("file not readable: % 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); // unsigned long zip_size = size * 4; void * zip_con = malloc (zip_size); int code = uncompress (B Ytef *) zip_con, & zip_size, (bytef *) Con, size); If (z_ OK! = Code) {printf ("error occurred when extracting % s: % d \ n", folder, Code); Return 0 ;}// release the resource fclose (File ); free (CON); // zip_con is released externally by bufflen = zip_size; return zip_con ;}

Finally, you can plug the stream file into the virtual machine of Lua, that is, execute the Lua code in the stream mode.

Of course, the more advanced method is to directly rewrite Lua's file loading policy. For details, I can refer to another blog article, in the next article, I will introduce the technology of packaging resources and scripts into the Pak format.

Modify the Lua file loader and customize the Lua file loading.

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.