In the development of software, we will be in the project when the code is encrypted, to prevent the theft of criminals to profit. Different languages have different encryption methods, more famous are Packers, code confusion and so on. when Lua develops COCOS2DX , the framework will have scripts that provide encryption . Let me talk about the steps to encrypt Windows
1. To know the storage path of the source code to be encrypted, and specify the backup path
2. Copy the code to the backup path
3. Go to BOM processing for all scripts
4. Use PHP command compile_scripts.php for encryption processing.
According to the above four points, we post the UTF8 to the BOM code and the whole code of encryption
1.utf-8
#! /usr/bin/python#-*-coding:utf-8-*-import osimport sysimport codecsdef convert (dirname, filename): if Os.path.splitext (filename) [1] in ['. txt ', '. Py, ', '. Lua ', '. Sh ']:p ath = os.path.join (dirname, filename) print ' converting%s '% (path) f = open (path, ' r B ') content = F.read (3) if content! = ' \xef\xbb\xbf ':p rint '%s is not utf-8 BOM '% (path) returncontent = F.read () f.close () F = Open (Path, ' WB ') f.write (content) f.close () print ' Convert%s finish '% (path) def useage (): If Len (sys.argv) = = 1:print ' Useage:utf8 file or dir ... ' return truedef Exist_error (f): If not os.path.exists (f):p rint ' ERROR:%s was not exist '% (f) return Truedef main (): If Useage () : Returnfor F in Sys.argv[1:]:if Exist_error (f): Breakif Os.path.isdir (f): For Root, dirs, files in Os.walk (f): For I in Files : Convert (root, i) Else:convert (", f) if __name__ = = ' __main__ ': Main ()
2.compile_scripts.php
<?phpdefine (' DS ', directory_separator);d efine (' Luajit ', true); class luapackager{Private $quiet = false; Private $packageName = '; Private $rootdir = '; Private $rootdirLength = 0; Private $suffixName = ' zip '; Private $files = Array (); Private $modules = Array (); Private $excludes = Array (); function __construct ($config) {$this->quiet = $config [' quiet ']; $this->rootdir = Realpath ($config [' srcdir ']); $this->rootdirlength = strlen ($this->rootdir) + 1; $this->packagename = Trim ($config [' PackageName '], '. '); $this->suffixname = $config [' Suffixname ']; $this->excludes = $config [' excludes ']; if (!empty ($this->packagename)) {$this->packagename = $this->packagename. ‘.‘; }} function Dumpzip ($outputFileBasename) {$this->files = array (); $this->modules = arrAy (); if (! $this->quiet) {print ("Compile script files\n"); } $this->compile (); if (Empty ($this->files)) {printf ("Error.\nerror:not found script files in%s\n", $this->rootdir) ; Return } $zipFilename = $outputFileBasename. ‘.‘ . $this->suffixname; $zip = new Ziparchive (); if ($zip->open ($zipFilename, Ziparchive::overwrite | Ziparchive::cm_store) {if (! $this->quiet) {printf ("Create ZIP bundle fil E:%s\n ", $zipFilename); } foreach ($this->modules as $module) {$zip->addfromstring ($module [' ModuleName '], $module [' bytes ']); } $zip->close (); if (! $this->quiet) {printf ("done.\n\n"); }} if (! $this->quiet) {print <<<eot### How to use # # #1. ADD CodeTo your Lua script:cclualoadchunksfromzip ("${zipfilename}") EOT; }} function Dump ($outputFileBasename) {$this->files = array (); $this->modules = Array (); if (! $this->quiet) {print ("Compile script files\n"); } $this->compile (); if (Empty ($this->files)) {printf ("Error.\nerror:not found script files in%s\n", $this->rootdir) ; Return } $headerFilename = $outputFileBasename. '. h '; if (! $this->quiet) {printf ("Create C header file:%s\n", $headerFilename); } file_put_contents ($headerFilename, $this->renderheaderfile ($outputFileBasename)); $sourceFilename = $outputFileBasename. '. c '; if (! $this->quiet) {printf ("Create C source file:%s\n", $sourceFilename); } file_put_contents ($sourceFilename, $this->rendersourcefile ($outputFileBasename)); if (! $this->quiet) {printf ("done.\n\n"); } $outputFileBasename = basename ($outputFileBasename); Print <<<eot### How to use # # #1. ADD code to AppDelegate.cpp:extern "C" {#include "${outputfilebasename}.h"}2. ADD code to Appdelegate::applicationdidfinishlaunching () ccscriptengineprotocol* pengine = Ccscriptenginemanager:: Sharedmanager ()->getscriptengine (); Luaopen_${outputfilebasename} (Pengine->getluastate ()); Pengine->executestring ("Require (\" main\ ")"); EOT; } private Function Compile () {if (file_exists ($this->rootdir) && is_dir ($this->rootdir)) {$this->files = $this->getfiles ($this->rootdir); } foreach ($this->files as $path) {$filename = substr ($path, $this->rootdirlength); $fi = PathInfo ($filename); if ($fi [' extension ']! = ' lua ') continue; $basename = LTrim ($fi [' dirname ']. Ds.$fi [' filename '], '/\\. '); $moduleName = $this->packagename. Str_replace (DS, '. ', $basename); $found = false; foreach ($this->excludes as $k = + $v) {if (substr ($moduleName, 0, strlen ($v)) = = = $v) {$found = true; Break }} if ($found) continue; if (! $this->quiet) {printf (' Compile module:%s ... ', $moduleName); } $bytes = $this->compilefile ($path); if ($bytes = = False) {print ("error.\n"); } else {if (! $this->quiet) {print ("ok.\n"); } $bytesName = ' lua_m_ '. Strtolower (Str_replace ('. ', ' _ ', $moduleName)); $this->modules[] = Array (' modulename ' = = $moduleName, ' Bytesname ' and $bytesName, ' functionname ' = ' luaopen_ '. $bytesName, ' basename ' and $basename, ' bytes ' = $bytes, ); }}} Private Function GetFiles ($dir) {$files = array (); $dir = RTrim ($dir, "/\\"). DS; $DH = Opendir ($dir); if ($dh = = False) {return $files;} while (($file = Readdir ($DH))!== false) {if ($file {0} = = '. ') {continue;} $path = $dir. $file; if (Is_dir ($path)) {$files = Array_merge ($files, $this->getfiles ($path)); } elseif (Is_file ($path)) {$files [] = $path; }} closedir ($DH); return $files; The Private Function Compilefile ($path) {$tmpfile = $path. '. bytes '; if (file_exists ($tmpfile)) unlink ($tmpfile); if (Luajit) {$command = sprintf (' Luajit-b-S "%s" "%s" ', $path, $tmpfile); } else {$command = sprintf (' Luac-o "%s" "%s" ', $tmpfile, $path); } passthru ($command); if (!file_exists ($tmpfile)) return false; $bytes = file_get_contents ($tmpfile); Unlink ($tmpfile); return $bytes; } Private Function Renderheaderfile ($outputFileBasename) {$headerSign = ' __lua_modules_ '. Strtoupper (MD5 (TI Me ()). ' _h_ '; $outputFileBasename = basename ($outputFileBasename); $contents = Array (); $contents [] = <<<eot/* ${outputfilebasename}.h */#ifndef ${headersign} #define ${HEADERSIGN} #if __ Cplusplusextern "C" {#endif # include "Lua.h" Void Luaopen_${outputfilebasename} (lua_state* L); #if __cplusplus}# Endifeot; $contents [] = '/* '; foreach ($this->modules as $module) {$contents [] = sprintf (' int%s (lua_state* L); ', $module [' Functio Nname ']); } $contents[] = ' * * '; $contents [] = <<<EOT#ENDIF/* ${headersign} */eot; return implode ("\ n", $contents); } Private Function Rendersourcefile ($outputFileBasename) {$outputFileBasename = basename ($outputFileBasename ); $contents = Array (); $contents [] = <<<eot/* ${outputfilebasename}.c */#include "lua.h" #include "lauxlib.h" #include "${ Outputfilebasename}.h "EOT; foreach ($this->modules as $module) {$contents [] = sprintf ('/*%s,%s.lua */', $module [' ModuleName '] , $module [' basename ']); $contents [] = sprintf (' Static const unsigned char%s[] = {', $module [' bytesname ']); $contents [] = $this->encodebytes ($module [' bytes ']); $contents [] = $this->encodebytesfast ($module [' bytes ']); $contents [] = '}; '; $contents [] = "; } $contents [] = '; foreach ($this->modules as $module) {$functionName = $module [' FunctioNname ']; $bytesName = $module [' Bytesname ']; $basename = $module [' basename ']; $contents [] = <<<eotint ${functionname} (lua_state *l) {int arg = lua_gettop (L); Lual_loadbuffer (L, (const char*) ${bytesname}, sizeof (${bytesname}), "${basename}.lua"); Lua_insert (l,1); Lua_call (l,arg,1); return 1;} EOT; } $contents [] = '; $contents [] = "static Lual_reg ${outputfilebasename}_modules[] = {"; foreach ($this->modules as $module) {$contents [] = sprintf (' {"%s",%s}, ', $module ["ModuleName"], $module ["functionname"]); } $contents [] = <<<eot {NULL, null}};void luaopen_${outputfilebasename} (lua_state* L) {lual_reg* lib = ${outputfilebasename}_modules; for (; lib->func; lib++) {Lua_getglobal (L, "package"); Lua_getfIeld (L,-1, "preload"); Lua_pushcfunction (L, Lib->func); Lua_setfield (L,-2, lib->name); Lua_pop (L, 2); }}eot; return implode ("\ n", $contents); } Private Function Encodebytes ($bytes) {$len = strlen ($bytes); $contents = Array (); $offset = 0; $buffer = Array (); while ($offset < $len) {$buffer [] = Ord (substr ($bytes, $offset, 1)); if (count ($buffer) = =) {$contents [] = $this->encodebytesblock ($buffer); $buffer = Array (); } $offset + +; } if (!empty ($buffer)) {$contents [] = $this->encodebytesblock ($buffer); } return implode ("\ n", $contents); } Private Function Encodebytesfast ($bytes) {$len = strlen ($bytes); $output = Array (); for ($i = 0; $i < $len; $i + +) {$output [] = sprintf ('%d, ', Ord ($bytes {$i})); } return Implode (", $output); } Private Function Encodebytesblock ($buffer) {$output = array (); $len = count ($buffer); for ($i = 0; $i < $len; $i + +) {$output [] = sprintf ('%d, ', $buffer [$i]); } return Implode (", $output); }}function Help () {echo <<<eotusage:php compile_scripts.php [options] dirname output_filenameoptions:-zip Package to Zip-suffix package file extension name-p prefix package name-x exclude packages, eg:-X framework. Server, Framework.tests-q Quieteot;} if ($ARGC < 3) {help (); Exit (1);} Array_shift ($ARGV); $config = Array (' packagename ' = = ', ' excludes ' = = Array (), ' Srcdir ' = = ', ' outputfilebasename ', ' = ', ' zip ' = = False, ' suffixname ' = ' Zip ', ' quiet ' and false,);d o{if ($argv [0] = = '-P ') {$config [' packagename '] = $argv [1]; Array_shift ($ARGV); } else if ($argv [0] = = '-X ') {$excludes = explode (', ', $argv [1]); foreach ($excludes as $k = = $v) {$v = Trim ($v); if (empty ($v)) {unset ($excludes [$k]); } else {$excludes [$k] = $v; }} $config [' excludes '] = $excludes; Array_shift ($ARGV); } else if ($argv [0] = = '-Q ') {$config [' quiet '] = true; } else if ($argv [0] = = '-zip ') {$config [' zip '] = true; } else if ($argv [0] = = '-suffix ') {$config [' suffixname '] = $argv [1]; Array_shift ($ARGV); } else if ($config [' srcdir '] = = ') {$config [' srcdir '] = $argv [0]; } else {$config [' outputfilebasename '] = $argv [0]; } array_shift ($ARGV);} while (count ($ARGV) > 0), $packager = new Luapackager ($config), if ($config [' zip ']) {$packager->dumpzip ($config [' O Utputfilebasename‘]);} else{$packager->dump ($config [' outputfilebasename ']);}
3. Encryption scripts
@echo onset _p_=bbframeworkset _t_=script_bakcd. mkdir%_t_%mkdir%_t_%\scriptsmkdir%_p_%\updatescriptsecho "Copy Scripts to Bak floder" xcopy/e%_p_%\scripts%_T_%\SCRI PTS CD%_p_%echo "UTF-8 transition" python. \bin\utf8\main.py Scriptsecho "Handle <<updatescripts>> Begin ..." move/y Scripts/app updatescripts/appphp. \bin\__lib__\compile_scripts.php-zip-suffix "Bin" updatescripts Res\updatepause
These are the steps for encrypting LUA Code on Windows
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4988378.html
[Automation-Scripting]002.cocos2dx-lua LUA code Windows encryption batch