Cocos2d-x图片资源加密

摘要:
Id=2739图像加密使用xxtea进行加密,加密密钥由用户确定。其思想是先使用代码对图像进行加密,然后在程序中使用资源时对其进行解密。

http://cn.cocos2d-x.org/tutorial/show?id=2739

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

加密代码如下:

1、首先要加载头文件

20150420115250269.jpg

2、将图片加密

booljiamiImg(stringinputFileName,stringoutFileName)
{
stringfileName=FileUtils::getInstance()->fullPathForFilename(inputFileName);

if(fileName.empty())
{
returnfalse;
}

DatafileData=FileUtils::getInstance()->getDataFromFile(fileName);
xxtea_longret_len;
unsignedcharkey[100]="lyctianya";
unsignedchar*ret_data=xxtea_encrypt(fileData.getBytes(),(xxtea_long)fileData.getSize(),key,(xxtea_long)strlen("lyctianya"),&ret_len);

if(ret_data==NULL){
returnfalse;
}

FILE*fp=fopen(outFileName.c_str(),"wb+");
if(fp==NULL){
returnfalse;
}
fwrite(ret_data,ret_len,1,fp);
fflush(fp);
fclose(fp);
CC_SAFE_DELETE(ret_data);

returntrue;

}

3、加密部分

/*************jiami************/
/*
std::stringoutFileName="/Users/liyongchuang/Desktop/cocosTool/code/myLuaTest/res/jiamiStar.png";
booljiamiRet=jiamiImg("Star.png",outFileName.c_str());
if(jiamiRet){
printf("-----success-----
");
}
else
{
printf("------false------
");
}
*/

4、加密后生成的文件

20150420115804304.jpg

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

修改

1429512504373337.png

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

boolisEndWith(std::stringinputStr,std::stringendStr)
{
if(inputStr.empty()||endStr.empty())
{
returnfalse;
}
std::stringnewEndStr=inputStr.substr(inputStr.find_last_of("."));
if(endStr.compare(newEndStr)==0)
{
returntrue;
}
else
{
returnfalse;
}
}

boolImage::initWithImageFile(conststd::string&path)
{
boolret=false;
_filePath=FileUtils::getInstance()->fullPathForFilename(path);

#ifdefEMSCRIPTEN
//Emscriptenincludesare-implementationofSDLthatusesHTML5canvas
//operationsunderneath.Consequently,loadingimagesviaIMG_Load(anSDL
//API)willbealotfasterthanrunninglibpngetalascompiledwith
//Emscripten.
SDL_Surface*iSurf=IMG_Load(fullPath.c_str());

intsize=4*(iSurf->w*iSurf->h);
ret=initWithRawData((constunsignedchar*)iSurf->pixels,size,iSurf->w,iSurf->h,8,true);

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

SDL_FreeSurface(iSurf);
#else
Datadata;
if(isEndWith(_filePath,".lyc"))
{
DatafileData=FileUtils::getInstance()->getDataFromFile(_filePath);
xxtea_longret_len;
unsignedcharkey[100]="lyctianya";
unsignedchar*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

returnret;
}

boolImage::initWithImageFileThreadSafe(conststd::string&fullpath)
{
boolret=false;
_filePath=fullpath;

Datadata;
if(isEndWith(_filePath,".lyc"))
{
DatafileData=FileUtils::getInstance()->getDataFromFile(_filePath);
xxtea_longret_len;
unsignedcharkey[100]="lyctianya";
unsignedchar*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());
}

returnret;
}

顺便附上单独解密代码:

booljiemiImg(stringjiaMiFileName,stringoutFileName)
{
stringfileName=FileUtils::getInstance()->fullPathForFilename(jiaMiFileName);
if(fileName.empty()){
returnfalse;
}
DatafileData=FileUtils::getInstance()->getDataFromFile(fileName);
xxtea_longret_len;
unsignedcharkey[100]="lyctianya";
unsignedchar*ret_data=xxtea_decrypt(fileData.getBytes(),(xxtea_long)fileData.getSize(),key,strlen("lyctianya"),&ret_len);
if(ret_data==NULL){
returnfalse;
}
FILE*fp=fopen(outFileName.c_str(),"wb+");
if(fp==NULL){
returnfalse;
}
fwrite(ret_data,ret_len,1,fp);
fflush(fp);
fclose(fp);
CC_SAFE_DELETE(ret_data);
returntrue;
}
/*************jiemi************/
/*
std::stringoutFileName="/Users/liyongchuang/Desktop/cocosTool/code/myLuaTest/res/jiemiStar.png";
booljiaemiRet=jiemiImg("jiamiStar.png",outFileName.c_str());
if(jiaemiRet){
printf("-----success-----
");
}
else
{
printf("------false------
");
}
*/

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

localfunctionmain()
localgameScene=cc.Scene:create()
locally=cc.LayerColor:create(cc.c4b(0,255,255,255))
gameScene:addChild(ly)

locallb=cc.Label:createWithSystemFont("Helloworld","Arial",20)
ly:addChild(lb)
lb:setPosition(480,320)

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

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


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


end

下面看结果:

20150420124101219.jpg

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

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

免责声明:文章转载自《Cocos2d-x图片资源加密》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇php生成RSA公钥私钥方法-OPENSSLoracle-plsql序列问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

【Promise】Promise实现请求超时处理(加强版)

  昨天闲来无事,于是把之前写过的promise优化了一下,代码如下: /*写文件、追加写、读文件*/ var fs = require('fs'); function wrapper(fn,context) { var args = Array.prototype.slice.call(arguments,2); return...

微信小程序上传图片 并和文字一起提交 (有接口)

实现功能如图 html代码 <viewclass="main"> <viewclass="card"> <viewclass="card-up"> <view>身份证</view> <view>上传身份证</view> </view> <image...

Google API快速生成QR二维码

Google API快速生成QR二维码现在来说生成二维码最简单的方法是使用Google Chart API来实现,再次膜拜Google大神~ Google Chart API是一套可以让你在线生成报表图的系统,通过URL你可以得到各种图表。举个例子:你在浏览器中输入 https://chart.googleapis.com/chart?cht=qr&...

互动直播中的前端技术——即时通讯

前言 在疫情期间,上班族开启了远程办公,体验了各种远程办公软件。老师做起了主播,学生们感受到了被钉钉支配的恐惧,歌手们开启了在线演唱会,许多综艺节目也变成了在线直播。在这全民互动直播的时期,我们来聊聊互动直播中的即时通讯技术在前端中的使用。 即时通讯技术 即时通讯(Instant Messaging,简称IM)是一个实时通信系统,允许两人或多人使用网络实时...

出现 "System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本" 错误的解决办法

出现 "System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本" 错误的解决办法 1.问题: 在Windows SP2 + VS2005 + Oracle 9i +IIS5.1环境中运行ASP.NET网页的时候出现如下错误: System.Data.OracleClient 需要 Oracle 客户端软...

数组中的filter函数,递归以及一些应用。

当我们用一个东西时候我们必须知道的是?why---where----how---when。一个东西我们为什么用?在哪用?怎么用?何时用?而不是被动的去接受一些东西。用在js里边我觉得也会试用。一直追求源生js,虽然也都背过好多东西,但是随着时间的流逝,工作的繁忙都忘了,有时甚至一点印象都没有,这让我开始思考我的学习方法了已经思维方式了。我们要记得不是简单的...