cocos2d-x 图片xxtea加密

图片加密
1.导入相应头文件


image

2.加密方法
HelloWorld.h文件
bool picJm(std::string inputFileName,std::string outFileName);
.cpp文件

//导入相应库
#include "xxtea/xxtea.h"
#include 
#include 
#include 
#include 
//方法实现
bool HelloWorld::picJm(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]="miyao";
    unsigned char* ret_data= xxtea_encrypt(fileData.getBytes(), (xxtea_long)fileData.getSize(),key, (xxtea_long)strlen("miyao"), &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;
 
}

//方法调用
    std::string outFileName="/Users/qzp/Desktop/Cocos2d/MyGame/Resources/test.png";

    bool jiamiRet=picJm("dw.png",outFileName.c_str());
    if (jiamiRet) {
        printf("-----加密成功success-----\n");
    }
    else
    {
        printf("------加密失败false------\n");
    }




解密
cocos2d/cocos/platform/CCImage.cpp 文件
修改

bool Image::initWithImageFile(const std::string& path)
{
    bool ret = false;
    _filePath = FileUtils::getInstance()->fullPathForFilename(path);
    ssize_t len;
    unsigned char* data = FileUtils::getInstance()->getFileData(_filePath, "rb", &len);
    xxtea_long ret_len;
    unsigned char key[100] = "miyao";
    unsigned char* ret_data = xxtea_decrypt(data, (xxtea_long)len, key, strlen("miyao"), &ret_len);
    Data result;
    result.fastSet(ret_data, ret_len);
    
    if (!result.isNull())
    {
        ret = initWithImageData(result.getBytes(), result.getSize());
    }
    return ret;
}


bool Image::initWithImageFileThreadSafe(const std::string& fullpath)
{

    bool ret = false;
    _filePath = fullpath;
    
    ssize_t len;
    unsigned char* data = FileUtils::getInstance()->getFileData(_filePath, "rb", &len);
    xxtea_long ret_len;
    unsigned char key[100] = "miyao";
    unsigned char* ret_data = xxtea_decrypt(data, (xxtea_long)len, key, strlen("miyao"), &ret_len);
    Data result;
    result.fastSet(ret_data, ret_len);
    
    if (!result.isNull())
    {
        ret = initWithImageData(result.getBytes(), result.getSize());
    }
    
    return ret;
}
扩展----同时加密文件夹下所有图片
//获取指定路径下所有文件名称
std::vector HelloWorld::getFilePathAtVec(std::string filePath) {
    std::vector path_vec;
    const char* path = filePath.c_str();
    char *dir = (char*)malloc(filePath.size() + 1);
    sprintf(dir,  "%s", path);

    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;
    int i=0;
    
    if((dp=opendir(dir))==NULL)
    {
        fprintf(stderr,"cannot open %s",dir);
        exit(1);
    }
    chdir(dir);
    
    while((entry=readdir(dp))!=NULL&&i<255)
    {
        stat(entry->d_name,&statbuf);
        if(!S_ISREG(statbuf.st_mode))
            continue;
        path_vec.push_back(StringUtils::format("%s",entry->d_name));
    }
    
    return path_vec;  
}



//----------循环调用--------------
   //遍历的文件夹目录
    string oldFileName = "/Users/qzp/Desktop/Cocos2d/WillJm_pic";
    std::vector temps = getFilePathAtVec(oldFileName);

    //输出目录
    std::string outFileName="/Users/qzp/Desktop/Cocos2d/qzp2018_jm_pic";
    for (int i = 0; i < temps.size(); i++) {
        string name = StringUtils::format("jm_%s", temps.at(i).c_str());
        string oldPicPath = oldFileName + "/" + temps.at(i); //原始图片路径
        string newPicPath = outFileName + "/" + name; //加密后路径
        picJm(oldPicPath, newPicPath);

    }

你可能感兴趣的:(cocos2d-x 图片xxtea加密)