cocos2dx lua已经集成了对lua脚本的加解密,见AppDelegate.cpp.
- LuaStack*stack=engine->getLuaStack();
- stack->setXXTEAKeyAndSign("123",strlen("123"),"cloud",strlen("cloud"));
它是通过XXTEA来加解密的。参数,key,keyLen,signment,signmentLen。它的签名作用可能是用来判断文件是否经过加密的。
好,我们来对文件加密。打开cocos2d-xexternalxxtea文件夹,调用相关函数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
-
- 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); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> path=projectPath+'/src'
- fileList=ListLua(path)
- forfilesinfileList:
- EncodeWithXxteaModule(files,153); background-color:inherit; font-weight:bold">defFixCpp(projectPath,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> filePath=projectPath+'/frameworks/runtime-src/Classes/AppDelegate.cpp'
- all_the_text=ReadFile(filePath)
-
- 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); background-color:inherit; font-weight:bold">print"encodeok"
整个工程是用cocosIDE生成的。这个工具会自动加密src下的lua,并在
AppDelegate.cpp中设置相应的密码与签名。xxTeaModule是对cocos2d-xexternalxxteaxxtea.cpp的一个python封装。
相关工具我已打包上传,见地址:http://download.csdn.net/detail/cloud95/7675821
转载:http://blog.csdn.net/cloud95/article/details/38110009
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|