cocos create目前不提供加密资源的功能, 只能自己修改引擎来解决
虽说是要改引擎, 其实很简单, 此方案不适用于为web平台
1.首先修改CCImage.h类定义添加
ssize_t mydecode(const unsigned char * data, ssize_t dataLen, unsigned char ** outBuffer);
2.跳到CCImage.cpp
添加函数实现
ssize_t Image::mydecode(const unsigned char * data, ssize_t dataLen, unsigned char ** outBuffer) { if(dataLen<=7 || memcmp(data, "aabbcc", 6) != 0) //是否加密的标记 { return dataLen; } ssize_t len = dataLen-7; *outBuffer = (unsigned char*)malloc( len ); unsigned char key = data[6]; for(ssize_t i=0;i<len;i++) { (*outBuffer)[i] = data[i+7] ^ key; //最简单的xor, 自己改算法 } return len; }
3.修改CCImage.cpp中的initWithImageData函数
将
unsigned char* unpackedData = nullptr;
ssize_t unpackedLen = 0;
改为
unsigned char* decodeData = nullptr;
unsigned char* unpackedData = nullptr;
ssize_t unpackedLen = 0;
dataLen = mydecode(data, dataLen, &decodeData); if(decodeData) { data = decodeData; }
并在后面添加释放
if(decodeData)
{
free(decodeData);
}
最后就是写个小程序来加密图片文件了,
encode.zip (597 字节)