您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 7 技巧

Cocos2d-x 的 Lua/JavaScript 腳本加密實作

編輯:iOS 7 技巧

下面為各位介紹一個 Cocos2d-x 中 Lua/JavaScript 腳本加密的實作範例,有需要的朋友可以參考。

在遊戲開發中,腳本作為一種資源檔,就像圖片、影片一樣被引擎引用。使用腳本開發遊戲的好處在於可以線上 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");
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] = '\0';//最後一位置為結束位
fread_s(fileContent,nFileLen, 1, nFileLen, fp);
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<<"加密後文件大小:"<<strlen(fileContent)<<endl;
FILE *stream = NULL;
fopen_s(&stream, f,"wb");
if (stream == NULL)
    { fprintf(stderr, "Cannot open output file.\n"); }
else { fwrite(p, nFileLen, 1, stream); fclose(stream); }
cout<<"====="<<endl;
    Cutecode(fileContent,key);//解密
}
//單個字符異或運算
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-x 3.0 中對應的是 Cocos2dxLuaLoader.cpp 裡的 int cocos2dx_lua_loader(lua_State *L) 方法,將其修改為如下(重點是在 data 讀取後、載入前進行異或解密):

    Data data = FileUtils::getInstance()->getDataFromFile(filename);
    if (!data.isNull()) {
        //====code decode start==================================
        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) {
            fileContentDecoded = (char *) malloc ((data.getSize() + 1) * sizeof(char));
            memset(fileContentDecoded, 0, data.getSize() + 1);
            strncpy(fileContentDecoded,fileContent,data.getSize());
            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)); }
    }

注意加密與解密的 key 必須保持一致。程式碼註解應該相當完整,就不再贅述了,Enjoy~

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved