Cocos2d-x Lua/Javascript指令碼代碼加密實現

來源:互聯網
上載者:User

在遊戲開發中,指令碼作為一種資源檔,就像圖片視頻一樣,被引擎所引用,使用指令碼做遊戲的好處就在於可以線上patch更新,特別對於蘋果App Store審核期很長的情況。
如果不對指令碼進行加密,不懷好意的人鬆鬆解壓出指令檔,給你瞬間複製一個遊戲出來。

1.異或加密解密

最簡單的一種加密方式,雖然簡單,但是也比較實用。但是防破解方面確實一般,如果你有其他嚴格的仿破解需求,可以將這部分密碼編譯演算法換成你自己的複雜演算法,不過保證解密效率。下面是採用C++簡單實現的對檔案進行加密之後儲存到原檔案中(注意對原始未加密檔案進行備份)

 代碼如下 複製代碼
#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); //定位到檔案末
int nFileLen = ftell(fp); //檔案長度
cout << "file len = " << nFileLen << endl;
fseek(fp, 0, SEEK_SET);
char *fileContent = NULL;
fileContent = (char *) malloc ((nFileLen + 1) * sizeof(char));//增加一位
memset(fileContent, 0, nFileLen + 1);
fileContent[nFileLen] = '';//最後一位置為結束位
fread_s(fileContent,nFileLen, 1, nFileLen, fp);
//fread(buf,nFileLen, 1, fp);
//cout<<"解密前:"<<fileContent<<endl;
fclose(fp);
cout<<"檔案:"<<f<<endl;
cout<<"解密前檔案大小:"<<strlen(fileContent)<<endl;
    int key[]={1, 2, 6, 1, 2, 6};//加密字元
    char *p=fileContent;
cout<<"====="<<endl;
    Makecode(fileContent,key);//加密
    //cout<<"加密後:"<<p<<endl;
cout<<"加密後檔案大小:"<<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); /* 寫的struct檔案*/
fclose(stream); /*關閉檔案*/
}
cout<<"====="<<endl;
    Cutecode(fileContent,key);//解密
    //cout<<"解密後:"<<fileContent<<endl;
}
//單個字元異或運算
char MakecodeChar(char c,int key){
    return c=c^key;
}
//單個字元解密
char CutcodeChar(char c,int key){
    return c^key;
}
//加密
void Makecode(char *pstr,int *pkey){
    int len=strlen(pstr);//擷取長度
    for(int i=0;i<len;i++)
        *(pstr+i)=MakecodeChar(*(pstr+i),pkey[i%5]);
}
//解密
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.修改Cocos2d-x引擎中載入lua指令檔(或者js檔案)的入口,在載入的時候對其進行解密。可能不同版本引擎有不同的入口檔案,在Cocos2d-x3.0中,對應的是檔案Cocos2dxLuaLoader.cpp檔案中的int cocos2dx_lua_loader(lua_State *L)方法,對其進行修改成如下:

 代碼如下 複製代碼
#include "Cocos2dxLuaLoader.h"
#include <string>
#include <algorithm>
#include<iostream>
using namespace cocos2d;
extern "C"
{
    //單個字元異或運算
    char MakecodeChar(char c,int key){
        return c=c^key;
    }
    //單個字元解密
    char CutcodeChar(char c,int key){
        return c^key;
    }
    //加密
    void Makecode(char *pstr,int *pkey){
        int len=strlen(pstr);//擷取長度
        for(int i=0;i<len;i++)
            *(pstr+i)=MakecodeChar(*(pstr+i),pkey[i%5]);
    }
    //解密
    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());
            //如果filename == 'main.lua',則解密
            char *fileContent = (char*)data.getBytes();
            int key[]={1, 2, 6, 1, 2, 6};//加密字元
            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));//增加一位
            memset(fileContentDecoded, 0, data.getSize() + 1);
            fileContentDecoded[data.getSize()] = '';//最後一位置為結束位
            strncpy(fileContentDecoded,fileContent,data.getSize());
            fileContent = NULL;
            }
            else {
            fileContentDecoded = fileContent;
            }
            Cutecode(fileContentDecoded,key);//解密
            }
            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;
    }
}


注意加密解密的key保證一致。
代碼注釋應該挺完善的,不進行解釋了,Enjoy~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.