cocos2dx-lua or quick 2dx 中的图片资源加密

网上关于cocos2dx c++版本中的图片加密方式已经很充分了。采用的是texturepacker工具的加密功能。

具体步骤是参考:http://www.codeandweb.com/texturepacker/contentprotection

中文翻译:http://acoder.me/encrypted-picture-resources-by-texturepacker.html

由于D:cocos2d-x-2.1.5cocos2dxsupportzip_support目录中已经包含了相关的.cpp文件,所以直接调用代码就好了。

这里是ZipUtils.h文件中的部分解释:

* Sets thepvr.ccz encryption key parts separately for added
        * security.
        *
        * Example: If the key used to encryptthe pvr.ccz file is
        * 0xaaaaaaaabbbbbbbbccccccccddddddddyou will call this function 4
        * different times, preferably from 4different source files, as follows
        *
        *ZipUtils::ccSetPvrEncryptionKeyPart(0, 0xaaaaaaaa);
        * ZipUtils::ccSetPvrEncryptionKeyPart(1,0xbbbbbbbb);
        *ZipUtils::ccSetPvrEncryptionKeyPart(2, 0xcccccccc);
        *ZipUtils::ccSetPvrEncryptionKeyPart(3, 0xdddddddd);
        *
        * Splitting the key into 4 parts andcalling the function
        * from 4 different source filesincreases the difficulty to
        * reverse engineer the encryption key.Be aware that encrpytion
        * is *never* 100% secure and the keycode can be cracked by
        * knowledgable persons.
        *
        * IMPORTANT: Be sure to callccSetPvrEncryptionKey or
        * ccSetPvrEncryptionKeyPart with all ofthe key parts *before* loading
        * the spritesheet or decryption willfail and the spritesheet
        * will fail to load.
        *


基本流程是:

texturePacker的32位16进制的加密key,在程序中使用32位key进行解密即可,程序可以参考

D:cocos2d-x-2.1.5samplesCppTestCppClassesTexturePackerEncryptionTestTextureAtlasEncryptionTest.cpp

相关解密pvr.ccz代码如下

// Load theencrypted atlas
    // 1) Set the encryption keys or step 2will fail
    // In this case the encryption key0xaaaaaaaabbbbbbbbccccccccdddddddd is
    // split into four parts. See the headerdocs for more information.
    ZipUtils::ccSetPvrEncryptionKeyPart(0,0xaaaaaaaa);
    ZipUtils::ccSetPvrEncryptionKeyPart(1,0xbbbbbbbb);
    ZipUtils::ccSetPvrEncryptionKeyPart(2,0xcccccccc);
    ZipUtils::ccSetPvrEncryptionKeyPart(3,0xdddddddd);
   
    // Alternatively, you can call the functionthat accepts the key in a single
    // function call.
    // This is slightly less secure because theentire key is more easily
    // found in the compiled source. See theheader docs for more information.
    //ZipUtils::ccSetPvrEncryptionKey(0xaaaaaaaa, 0xbbbbbbbb, 0xcccccccc,0xdddddddd);
 
    // 2) Load the encrypted atlas
   CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Images/encryptedAtlas.plist","Images/encryptedAtlas.pvr.ccz");
   
    // 3) Create a sprite from the encryptedatlas
CCSprite *encryptedSprite =CCSprite::createWithSpriteFrameName("powered.png");


C++中的资源加密解密是这样完成的,但是Lua中的tolua文件没有实现ZipUtils,所以我们不能使用c++的方式去操作。

我采用的是将Cocos2dx文件中的ZipUtils的API进行tolua++的操作,生成可供lua使用的ZipUtils方法。

因为是在quick x下面操作,我也是用quick x下面的tolua工具来导出C++的API。首先编写自己的类文件(.h and .cpp),

根据.h文件写出.tolua文件,由于我使用的是最新版本的quick x 2.2.1-rc,所以我自己摸索了半天,终于生成了自己的LuaCocos2d.cpp文件,其中也已经包含了我的类的定义和方法声明,具体操作主要是在

D:quick-xquick-cocos2d-xlibcocos2d-x 目录 和 D:quick-xquick-cocos2d-xlibluabinding目录,放置好自己的.h and .cpp文件和.tolua文件到这两个目录中,通过点击luabinding目录下的build.bat文件来生成LuaCocos2d.cpp

至此也都没什么问题,然后是重新编译自己的x-player,不过报错了。

lua调用C++API的原理我是这样理解的:

1)lua文件中的类首先要通过Luacocos2d.cpp来找到对应的c++文件

2)c++文件执行并返回相应的结果


tolua++生成的LuaCocos2d.cpp更像是一个中间层,负责交互的任务。

.tolua文件的作用是用来生成LuaCocos2d.cpp文件的,所以我们的.tolua文件的书写需要有点差异,这个网上有说明;

.tolua主要就是声明提供的方法,这样才能让Luacocos2d.cpp知道某个类有哪些方法可以调用,而Luacocos2d.cpp的作用就是进行lua方法到调用C++方法的转换。

在生成LuaCocos2d.cpp文件时单纯的需要.tolua文件就可以了。

在lua代码中调用C++的方法时,也就是只需要LuaCocos2d.cpp文件和自己类的.cpp and .h 文件

上面遗留的报错问题待解决.....................................................T_T

相关参考网址聚合:

http://blog.csdn.net/xiaominghimi/article/details/8770396

http://blog.csdn.net/aryang/article/details/9611883

http://www.codeo4.cn/archives/746

http://blog.csdn.net/stalendp/article/details/8920018

http://cn.quick-x.com/?p=235

http://www.2cto.com/kf/201304/201704.html


网友的对这一块的解释截图:



原文地址:https://www.cnblogs.com/vokie/p/3602102.html