cocos2dx lua 加密

cocos2dx lua 加密
        cocos2dx lua已经集成了对lua脚本的加解密,见AppDelegate.cpp.
    LuaStack* stack = engine->getLuaStack();
    stack->setXXTEAKeyAndSign("123", strlen("123"), "cloud", strlen("cloud"));
它是通过XXTEA来加解密的。参数,key,keyLen,signment,signmentLen。它的签名作用可能是用来判断文件是否经过加密的。
       好,我们来对文件加密。打开cocos2d-x\external\xxtea文件夹,调用相关函数xxtea_encrypt进行加密,最后在文件开始位置,写上签名就可以了。我把相关操作封装成一个python文件。可以直接调用。
import xxteaModule
import os

def ReadFile(filePath):
    file_object = open(filePath,'rb')
    all_the_text = file_object.read()
    file_object.close()
    return all_the_text

def WriteFile(filePath,all_the_text):    
    file_object = open(filePath,'wb')    
    file_object.write(all_the_text)
    file_object.close()
    
def BakFile(filePath,all_the_text):
    file_bak = filePath[:len(filePath)-3] + 'bak'   
    WriteFile(file_bak,all_the_text)



def ListLua(path):
    fileList = [] 
    for root,dirs,files in os.walk(path):
       for eachfiles in files:
           if eachfiles[-4:] == '.lua' :               
               fileList.append(root + '/' + eachfiles)
    return fileList

def EncodeWithXxteaModule(filePath,key,signment):    
    all_the_text = ReadFile(filePath)    

    if all_the_text[:len(signment)] == signment :
        return
    #bak lua
    BakFile(filePath,all_the_text)
       
    encrypt = xxteaModule.encrypt(all_the_text,key)
    signment = signment + encrypt
    WriteFile(filePath,signment)    
    
def EncodeLua(projectPath,key,signment):
    path = projectPath + '/src'
    fileList = ListLua(path)
    for files in fileList:
        EncodeWithXxteaModule(files,key,signment)

def FixCpp(projectPath,key,signment):
    filePath = projectPath + '/frameworks/runtime-src/Classes/AppDelegate.cpp'
    all_the_text = ReadFile(filePath)

    #bak cpp
    BakFile(filePath,all_the_text)    


    pos = all_the_text.find('stack->setXXTEAKeyAndSign')
    left = all_the_text.find('(',pos)
    right = all_the_text.find(';',pos)   

    word = str.format('("%s", strlen("%s"), "%s", strlen("%s"))' % (key,key,signment,signment))
    
    all_the_text = all_the_text[:left] + word + all_the_text[right:-1]
    
    WriteFile(filePath,all_the_text) 
    
    
    
projectPath = "D:/cocosIDEWork/aseGame/"
key = "123"
signment = "cloud"


EncodeLua(projectPath,key,signment)
FixCpp(projectPath,key,signment)
print "encode ok"

整个工程是用cocosIDE生成的。这个工具会自动加密src下的lua,并在 AppDelegate.cpp中设置相应的密码与签名。xxTeaModule是对cocos2d-x\external\xxtea\xxtea.cpp的一个python封装。
相关工具我已打包上传,见地址:http://download.csdn.net/detail/cloud95/7675821




你可能感兴趣的:(cocos2dx lua 加密)