cocos2dx lua已经集成了对lua脚本的加解密,见AppDelegate.cpp.
- LuaStack*stack=engine->getLuaStack();
- stack->setXXTEAKeyAndSign("123",strlen("123"),"cloud",strlen("cloud"));
好,我们来对文件加密。打开cocos2d-x\external\xxtea文件夹,调用相关函数xxtea_encrypt进行加密,最后在文件开始位置,写上签名就可以了。我把相关操作封装成一个python文件。可以直接调用。
- importxxteaModule
- importos
- defReadFile(filePath):
- file_object=open(filePath,'rb')
- all_the_text=file_object.read()
- file_object.close()
- returnall_the_text
- defWriteFile(filePath,all_the_text):
- 'wb')
- file_object.write(all_the_text)
- defBakFile(filePath,all_the_text):
- file_bak=filePath[:len(filePath)-3]+'bak'
- WriteFile(file_bak,all_the_text)
- defListLua(path):
- fileList=[]
- forroot,dirs,filesinos.walk(path):
- foreachfilesinfiles:
- ifeachfiles[-4:]=='.lua':
- fileList.append(root+'/'+eachfiles)
- returnfileList
- defEncodeWithXxteaModule(filePath,key,signment):
- all_the_text=ReadFile(filePath)
- ifall_the_text[:len(signment)]==signment:
- return
- #baklua
- BakFile(filePath,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> encrypt=xxteaModule.encrypt(all_the_text,key)
- signment=signment+encrypt
- WriteFile(filePath,signment)
- defEncodeLua(projectPath,248)"> path=projectPath+'/src'
- fileList=ListLua(path)
- forfilesinfileList:
- EncodeWithXxteaModule(files,153); font-weight:bold; background-color:inherit">defFixCpp(projectPath,248)"> filePath=projectPath+'/frameworks/runtime-src/Classes/AppDelegate.cpp'
- all_the_text=ReadFile(filePath)
- #bakcpp
- 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,signment))
- all_the_text=all_the_text[:left]+word+all_the_text[right:-1]
- projectPath="D:/cocosIDEWork/aseGame/"
- key="123"
- signment="cloud"
- EncodeLua(projectPath,signment)
- FixCpp(projectPath,153); font-weight:bold; background-color:inherit">print"encodeok"