码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2dx 图片资源加密

时间:2015-04-20 15:07:16      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

图片加密使用xxtea来加密,加密秘钥自己定,思路就是自己使用代码首先将图片加密,在程序中使用的时候,在加载图片资源处再将资源解密

加密代码如下:

首先要加载头文件

技术分享

2、将图片加密

bool jiamiImg(string inputFileName,string outFileName)
{
    string fileName=FileUtils::getInstance()->fullPathForFilename(inputFileName);
    
    if(fileName.empty())
    {
        return  false;
    }
    
    Data fileData=FileUtils::getInstance()->getDataFromFile(fileName);
    xxtea_long ret_len;
    unsigned char key[100]="lyctianya";
    unsigned char* ret_data= xxtea_encrypt(fileData.getBytes(), (xxtea_long)fileData.getSize(),key, (xxtea_long)strlen("lyctianya"), &ret_len);
    
    if (ret_data==NULL) {
        return false;
    }
    
    FILE*fp=fopen(outFileName.c_str(), "wb+");
    if (fp==NULL) {
        return false;
    }
    fwrite(ret_data, ret_len, 1, fp);
    fflush(fp);
    fclose(fp);
    CC_SAFE_DELETE(ret_data);
    
    return true;
    
}
3.加密部分

    /*************jiami************/
    /*
    std::string outFileName="/Users/liyongchuang/Desktop/cocosTool/code/myLuaTest/res/jiamiStar.png";
    bool jiamiRet=jiamiImg("Star.png",outFileName.c_str());
    if (jiamiRet) {
        printf("-----success-----\n");
    }
    else
    {
        printf("------false------\n");
    }
     */
4、加密后生成的文件

技术分享

5、修改加载图片资源处,并使用解密

修改技术分享

加入并修改如下代码:头文件自己加

bool isEndWith(std::string inputStr,std::string endStr)
{
    if (inputStr.empty()||endStr.empty())
    {
        return false;
    }
    std::string newEndStr = inputStr.substr(inputStr.find_last_of("."));
    if (endStr.compare(newEndStr) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool Image::initWithImageFile(const std::string& path)
{
    bool ret = false;
    _filePath = FileUtils::getInstance()->fullPathForFilename(path);

#ifdef EMSCRIPTEN
    // Emscripten includes a re-implementation of SDL that uses HTML5 canvas
    // operations underneath. Consequently, loading images via IMG_Load (an SDL
    // API) will be a lot faster than running libpng et al as compiled with
    // Emscripten.
    SDL_Surface *iSurf = IMG_Load(fullPath.c_str());

    int size = 4 * (iSurf->w * iSurf->h);
    ret = initWithRawData((const unsigned char*)iSurf->pixels, size, iSurf->w, iSurf->h, 8, true);

    unsigned int *tmp = (unsigned int *)_data;
    int nrPixels = iSurf->w * iSurf->h;
    for(int i = 0; i < nrPixels; i++)
    {
        unsigned char *p = _data + i * 4;
        tmp[i] = CC_RGB_PREMULTIPLY_ALPHA( p[0], p[1], p[2], p[3] );
    }

    SDL_FreeSurface(iSurf);
#else
    Data data;
    if (isEndWith(_filePath, ".lyc"))
    {
        Data fileData=FileUtils::getInstance()->getDataFromFile(_filePath);
        xxtea_long ret_len;
        unsigned char key[100]="lyctianya";
        unsigned char*ret_data=xxtea_decrypt(fileData.getBytes(), (xxtea_long)fileData.getSize(), key, strlen("lyctianya"), &ret_len);
        data.fastSet(ret_data, ret_len);
    }
    else
    {
        data = FileUtils::getInstance()->getDataFromFile(_filePath);
    }

    if (!data.isNull())
    {
        ret = initWithImageData(data.getBytes(), data.getSize());
    }
#endif // EMSCRIPTEN

    return ret;
}

bool Image::initWithImageFileThreadSafe(const std::string& fullpath)
{
    bool ret = false;
    _filePath = fullpath;
    
    Data data;
    if (isEndWith(_filePath, ".lyc"))
    {
        Data fileData=FileUtils::getInstance()->getDataFromFile(_filePath);
        xxtea_long ret_len;
        unsigned char key[100]="lyctianya";
        unsigned char*ret_data=xxtea_decrypt(fileData.getBytes(), (xxtea_long)fileData.getSize(), key, strlen("lyctianya"), &ret_len);
        data.fastSet(ret_data, ret_len);
    }
    else
    {
        data = FileUtils::getInstance()->getDataFromFile(_filePath);
    }
    
    if (!data.isNull())
    {
        ret = initWithImageData(data.getBytes(), data.getSize());
    }
    
    return ret;
}

顺便附上单独解密代码:

bool jiemiImg(string jiaMiFileName,string outFileName)
{
    string fileName=FileUtils::getInstance()->fullPathForFilename(jiaMiFileName);
    if (fileName.empty()) {
        return false;
    }
    Data fileData=FileUtils::getInstance()->getDataFromFile(fileName);
    xxtea_long ret_len;
    unsigned char key[100]="lyctianya";
    unsigned char*ret_data=xxtea_decrypt(fileData.getBytes(),(xxtea_long)fileData.getSize(), key, strlen("lyctianya"), &ret_len);
    if (ret_data==NULL) {
        return false;
    }
    FILE*fp=fopen(outFileName.c_str(),"wb+");
    if (fp==NULL) {
        return false;
    }
    fwrite(ret_data, ret_len, 1, fp);
    fflush(fp);
    fclose(fp);
    CC_SAFE_DELETE(ret_data);
    return true;
}


    /*************jiemi************/
    /*
     std::string outFileName="/Users/liyongchuang/Desktop/cocosTool/code/myLuaTest/res/jiemiStar.png";
     bool jiaemiRet=jiemiImg("jiamiStar.png",outFileName.c_str());
     if (jiaemiRet) {
     printf("-----success-----\n");
     }
     else
     {
     printf("------false------\n");
     }
    */

修改lua中的代码,并使用资源

local function main()
    local gameScene=cc.Scene:create()
    local ly=cc.LayerColor:create(cc.c4b(0,255,255,255))
    gameScene:addChild(ly)
    
    local lb=cc.Label:createWithSystemFont("Hello world","Arial",20)
    ly:addChild(lb)
    lb:setPosition(480,320)

    local sp=cc.Sprite:create("res/jiamiStar.lyc")
    ly:addChild(sp, 10)

    sp:setPosition(cc.p(480,280))


    if cc.Director:getInstance():getRunningScene()  then
    	cc.Director:getInstance():replaceScene(gameScene)
    else
    	cc.Director:getInstance():runWithScene(gameScene)
    end


end

下面看结果:

技术分享


顺便提一下,大家有没有发现一个问题,我的一些资源用了没有释放哦!

unsigned char *zipFileData =FileUtils::getInstance()->getFileData(zipFilePath.c_str(), "rb", &size);

free(zipFileData);









cocos2dx 图片资源加密

标签:

原文地址:http://blog.csdn.net/lyctianya/article/details/45148241

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!