Today’s topic is how to encrypt and decrypt lua script files.

After consideration, I summed up two solutions for your reference.

IOS development exchange technology group: 563513413, no matter you are big bull or small white are welcome to enter, share BAT, Ali interview questions, interview experience, discuss technology, we exchange learning and growth together!

1, lightweight solution, before APK packaging, use tools to encrypt all lua files, specifically lua files read to the memory, and then use zip and other compression encryption library compression encryption, and then compress the encrypted data to save the file with the same name as the source file. After the lua file is packaged, luA data is read, decrypted, and the decrypted stream data is sent to the Lua VM.

2, heavyweight solution, this solution is an extension of the last solution, but also the commercial game of the solution, to achieve a game file package, before packaging resources and scripts are packaged into a file using tools, can be packed when encryption compression, also can not encryption compression. Then read the corresponding file data directly from the package at run time, and then decrypt and decompress, and then provide the game engine for use. This is also a common technique used in end games, and most mobile games are starting to use this technique.

This article will focus on the first option briefly, and the second option will allow time for another blog post. All right, let’s get down to business.

The first is to compress the lua file, the code is as follows:

view sourceprint?

01.int write_file_content(const char* folder)

02. {

03.// Get file data and compress the file

04.FILE* fpin = fopen(folder, “wb+”);

05.if (fpin == NULL)

06. {

07. Printf (” cannot read file: %s

08.”, folder);

09.return 0;

10.}

11.

12.// Get the file size

13.fseek(fpin, 0, SEEK_END);

14.unsigned int size = ftell(fpin);

15.

16.// Read the contents of the file

17.fseek(fpin, 0, SEEK_SET);

18.void* con = malloc(size);

19.int r = fread(con, size, 1, fpin);

20.

21.// Perform encryption

22.unsigned long zip_con_size = size * 2;

23.void* zip_con = malloc(zip_con_size);

24.if (Z_OK ! = compress((Bytef*)zip_con, &zip_con_size, (Bytef*)con, size))

25. {

26. Printf (” error while compressing %s

27.”,folder);

28.}

Printf (“%s size before compression: %ld Size after compression: %ld

30.”, folder, size, zip_con_size);

31.

32.// Write the file contents

33.fseek(fpin, 0, SEEK_SET);

34.int len = fwrite(zip_con, zip_con_size, 1, fpin);

35.

36.// Release resources

37.fclose(fpin);

38.free(zip_con);

39.free(con);

40.return 0;

41.}

The decryption operation is followed by the following code:

01.void* read_file_content(const char* folder, int& bufflen)

02. {

03.FILE* file = fopen(folder, “wb+”);

04.if (file)

05. {

06. {

07. Printf (” cannot read file: %s

08.”, folder);

09.return 0;

10.}

11.

12.// Get the file size

13.fseek(file, 0, SEEK_END);

14.unsigned int size = ftell(file);

15.

16.// Read the contents of the file

17.void* con = malloc(size);

18.fseek(file, 0, SEEK_SET);

19.int len = fread(con, size, 1, file);

20.

21.// Decompress operation

22.unsigned long zip_size = size * 4;

23.void* zip_con = malloc(zip_size);

24.int code = uncompress((Bytef*)zip_con, &zip_size, (Bytef*)con, size);

25.if (Z_OK ! = code)

26. {

27. Printf (” error extracting %s :%d

28.”, folder, code);

29.return 0;

30.}

31.

32.// Release resources

33.fclose(file);

34.free(con);

35.

36.//zip_con is released externally

37.bufflen = zip_size;

38. The return zip_con;

39.}

Finally, you just stuff the stream file to lua’s virtual machine and stream the Lua code.

For Android app, dex source file security is the most important, therefore, the DEX source code encryption protection is actually very necessary, in this respect, can do love encryption platform, different types of applications also have different encryption protection scheme, the details can be understood here: www.ijiami.cn/appprotect_… Modify lua file loader, customize lua file loading