cocos2dx lua 加密

前端之家收集整理的这篇文章主要介绍了cocos2dx lua 加密前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
cocos2dx lua已经集成了对lua脚本的加解密,见AppDelegate.cpp.
  1. LuaStack*stack=engine->getLuaStack();
  2. stack->setXXTEAKeyAndSign("123",strlen("123"),"cloud",strlen("cloud"));
它是通过XXTEA来加解密的。参数,key,keyLen,signment,signmentLen。它的签名作用可能是用来判断文件是否经过加密的。
好,我们来对文件加密。打开cocos2d-x\external\xxtea文件夹,调用相关函数xxtea_encrypt进行加密,最后在文件开始位置,写上签名就可以了。我把相关操作封装成一个python文件。可以直接调用
[python]
    importxxteaModule
  1. importos
  2. defReadFile(filePath):
  3. file_object=open(filePath,'rb')
  4. all_the_text=file_object.read()
  5. file_object.close()
  6. returnall_the_text
  7. defWriteFile(filePath,all_the_text):
  8. 'wb')
  9. file_object.write(all_the_text)
  10. defBakFile(filePath,all_the_text):
  11. file_bak=filePath[:len(filePath)-3]+'bak'
  12. WriteFile(file_bak,all_the_text)
  13. defListLua(path):
  14. fileList=[]
  15. forroot,dirs,filesinos.walk(path):
  16. foreachfilesinfiles:
  17. ifeachfiles[-4:]=='.lua':
  18. fileList.append(root+'/'+eachfiles)
  19. returnfileList
  20. defEncodeWithXxteaModule(filePath,key,signment):
  21. all_the_text=ReadFile(filePath)
  22. ifall_the_text[:len(signment)]==signment:
  23. return
  24. #baklua
  25. 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)
  26. signment=signment+encrypt
  27. WriteFile(filePath,signment)
  28. defEncodeLua(projectPath,248)"> path=projectPath+'/src'
  29. fileList=ListLua(path)
  30. forfilesinfileList:
  31. EncodeWithXxteaModule(files,153); font-weight:bold; background-color:inherit">defFixCpp(projectPath,248)"> filePath=projectPath+'/frameworks/runtime-src/Classes/AppDelegate.cpp'
  32. all_the_text=ReadFile(filePath)
  33. #bakcpp
  34. BakFile(filePath,all_the_text)
  35. pos=all_the_text.find('stack->setXXTEAKeyAndSign')
  36. left=all_the_text.find('(',pos)
  37. right=all_the_text.find(';',pos)
  38. word=str.format('("%s",strlen("%s"),"%s",strlen("%s"))'%(key,signment))
  39. all_the_text=all_the_text[:left]+word+all_the_text[right:-1]
  40. projectPath="D:/cocosIDEWork/aseGame/"
  41. key="123"
  42. signment="cloud"
  43. EncodeLua(projectPath,signment)
  44. FixCpp(projectPath,153); font-weight:bold; background-color:inherit">print"encodeok"

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

猜你在找的Cocos2d-x相关文章