cocos2d-x 图片xxtea加密

作者: 醉影 | 来源:发表于2018-10-15 17:13 被阅读0次

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


image

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

//导入相应库
#include "xxtea/xxtea.h"
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
//方法实现
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<std::string> HelloWorld::getFilePathAtVec(std::string filePath) {
    std::vector<std::string> 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<std::string> 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 2.X iOS lua代码XXTEA 加密后

    Cocos2d-x 2.2.6 iOS 版,直接跳过了XXTEA 解密,导致XXTEA加密的代码无法运行,以下是改...

  • cocos2d-x 图片xxtea加密

    图片加密1.导入相应头文件 2.加密方法HelloWorld.h文件bool picJm(std::string ...

  • python 对称加密案例

    网络传输中往往为了防止三方人员监听 篡改数据分析往往需要加密算法。今天简单介绍些对称加密 xxtea。 xxtea...

  • Lua xxtea 解密脚本

    众所周知,使用 cocos 引擎编写的游戏,如果直接使用 xxtea 加密 Lua 脚本,实际上是很容易被解密的,...

  • cocos资料

    Cocos 资料大全 Cocos2d-x Lua运行流程 加密 cocos2dx资源和脚本加密quick-lua3...

  • TEA、XTEA、XXTEA加密解密算法

    在密码学中,微型加密算法(Tiny Encryption Algorithm,TEA)是一种易于描述和执行的块密码...

  • BUUCTF re [2019红帽杯]xx WP

    使用findcrypt插件发现为tea系列,跟进知道加密算法为xxtea 第一部分 第二部分,v4存放着Code的...

  • XXTEA加密算法在iOS中的运用

    用的是 andot 的源码,特此鸣谢。 源码在 https://github.com/xuanyi0627/xxt...

  • 加解密算法总结

    加解密算法 大体上可以分为两种 双向加密对称加密非对称加密 单向加密输入图片说明 对称加密 对称加密算法即:加密和...

  • 惘蚊浆糊

    网络图片加密平台 “惘蚊浆糊”http://wangwenjianghu.com 是一个可以对图片进行加密的网络平...

网友评论

    本文标题:cocos2d-x 图片xxtea加密

    本文链接:https://www.haomeiwen.com/subject/uyafzftx.html