cocos2dx lua 加密

  cocos2dx lua已经集成了对lua脚本的加解密,见AppDelegate.cpp.
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import xxteaModule  
  2. import os  
  3.   
  4. def ReadFile(filePath):  
  5.     file_object = open(filePath,'rb')  
  6.     all_the_text = file_object.read()  
  7.     file_object.close()  
  8.     return all_the_text  
  9.   
  10. def WriteFile(filePath,all_the_text):      
  11.     file_object = open(filePath,'wb')      
  12.     file_object.write(all_the_text)  
  13.     file_object.close()  
  14.       
  15. def BakFile(filePath,all_the_text):  
  16.     file_bak = filePath[:len(filePath)-3] + 'bak'     
  17.     WriteFile(file_bak,all_the_text)  
  18.   
  19.   
  20.   
  21. def ListLua(path):  
  22.     fileList = []   
  23.     for root,dirs,files in os.walk(path):  
  24.        for eachfiles in files:  
  25.            if eachfiles[-4:] == '.lua' :                 
  26.                fileList.append(root + '/' + eachfiles)  
  27.     return fileList  
  28.   
  29. def EncodeWithXxteaModule(filePath,key,signment):      
  30.     all_the_text = ReadFile(filePath)      
  31.   
  32.     if all_the_text[:len(signment)] == signment :  
  33.         return  
  34.     #bak lua  
  35.     BakFile(filePath,all_the_text)  
  36.          
  37.     encrypt = xxteaModule.encrypt(all_the_text,key)  
  38.     signment = signment + encrypt  
  39.     WriteFile(filePath,signment)      
  40.       
  41. def EncodeLua(projectPath,key,signment):  
  42.     path = projectPath + '/src'  
  43.     fileList = ListLua(path)  
  44.     for files in fileList:  
  45.         EncodeWithXxteaModule(files,key,signment)  
  46.   
  47. def FixCpp(projectPath,key,signment):  
  48.     filePath = projectPath + '/frameworks/runtime-src/Classes/AppDelegate.cpp'  
  49.     all_the_text = ReadFile(filePath)  
  50.   
  51.     #bak cpp  
  52.     BakFile(filePath,all_the_text)      
  53.   
  54.   
  55.     pos = all_the_text.find('stack->setXXTEAKeyAndSign')  
  56.     left = all_the_text.find('(',pos)  
  57.     right = all_the_text.find(';',pos)     
  58.   
  59.     word = str.format('("%s", strlen("%s"), "%s", strlen("%s"))' % (key,key,signment,signment))  
  60.       
  61.     all_the_text = all_the_text[:left] + word + all_the_text[right:-1]  
  62.       
  63.     WriteFile(filePath,all_the_text)   
  64.       
  65.       
  66.       
  67. projectPath = "D:/cocosIDEWork/aseGame/"  
  68. key = "123"  
  69. signment = "cloud"  
  70.   
  71.   
  72. EncodeLua(projectPath,key,signment)  
  73. FixCpp(projectPath,key,signment)  
  74. print "encode ok"  

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值