对Cocos2dX使用c++版的base64编码文件:
#include "DataBase64Himi.h" static const std::string dataChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; static inline bool baseData(unsigned char c) { return (isalnum(c) || (c == '+') || (c == '/')); } std::string himiSaveData(unsigned char const* bytes_to_encode, unsigned int in_len) { std::string ret; int i = 0; int j = 0; unsigned char char_array_3[3]; unsigned char char_array_4[4]; while (in_len--) { char_array_3[i++] = *(bytes_to_encode++); if (i == 3) { char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f; for(i = 0; (i <4) ; i++) ret += dataChars[char_array_4[i]]; i = 0; } } if (i) { for(j = i; j < 3; j++) char_array_3[j] = '\0'; char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f; for (j = 0; (j < i + 1); j++) ret += dataChars[char_array_4[j]]; while((i++ < 3)) ret += '='; } return ret; } std::string himiParseData(std::string const& encoded_string) { int in_len = encoded_string.size(); int i = 0; int j = 0; int in_ = 0; unsigned char char_array_4[4], char_array_3[3]; std::string ret; while (in_len-- && ( encoded_string[in_] != '=') && baseData(encoded_string[in_])) { char_array_4[i++] = encoded_string[in_]; in_++; if (i ==4) { for (i = 0; i <4; i++) char_array_4[i] = dataChars.find(char_array_4[i]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (i = 0; (i < 3); i++) ret += char_array_3[i]; i = 0; } } if (i) { for (j = i; j <4; j++) char_array_4[j] = 0; for (j = 0; j <4; j++) char_array_4[j] = dataChars.find(char_array_4[j]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; } return ret; }
然后Himi封装好了两个函数:(这里我们假设存储游戏金币)
首先.h中,定义枚举变量(备注1)
enum
{
DATA_GOLD,
//金币
};
然后.h定义两个函数:(备注2)
//存储数据 void saveDataByKeyValue(int iKey, string sValue); //读取数据 int loadDataByKey(int iKey);
最后.cpp中函数具体实现代码:(备注3)
//存储 void HelloWorld::saveDataByKeyValue(int iKey, string sValue){ char buffer[32]; sprintf(buffer, "%s%d", "Himi",iKey); CCLog("----------存储"); CCLog("存储之前数据 key:index: Himi%i, value: %s ",iKey,sValue.c_str()); string sKey = himiSaveData(reinterpret_cast<const unsigned char*>(sValue.c_str()), sValue.length()); CCLog("存储加密后的数据 key:index: Himi%i, value: %s ",iKey,sKey.c_str()); CCUserDefault::sharedUserDefault()->setStringForKey(buffer, sKey); CCUserDefault::sharedUserDefault()->flush(); } //读取 int HelloWorld::loadDataByKey(int iKey){ CCLog("----------读取"); char buffer[32]; sprintf(buffer, "%s%d", "Himi",iKey); string s = CCUserDefault::sharedUserDefault()->getStringForKey(buffer); CCLog("解密前的数据: %s ",s.c_str()); string parseKey = himiParseData(s); CCLog("解密后的数据: %s ",parseKey.c_str()); return atoi(parseKey.c_str()); }
OK,这里开始逐步解释一下:
首先说下备注2,这里定义的两个方法,第一个函数:
//存储数据
void
saveDataByKeyValue(
int
iKey, string sValue);
其中第一个函数传入一个int值,为什么要这样,原因是这样方便与结合枚举变量(备注1)来进行存储,这样使用的时候非常简化你的代码和步骤;第二个函数是值,这里我封装成string,这样为了更好的结合我们的base64编码!这点在(备注3)中体现!
第二个函数:
//读取数据
int
loadDataByKey(
int
iKey);
这个函数没什么好说的,传入的参数int,枚举变量即可!至于返回值是int 是方便程序中使用!
OK,那么备注3对于两个封装函数中的实现需要说明的有3点:
1. 将传入的int值(key)转化成char*类型,因为cocos2dx中并没有itoa这样的方法(原因上一篇有介绍),所以这里我采用 char buffer[32]; sprintf(buffer, “%s%d”, “Himi”,iKey); 方式进行将int转化char*类型;
2. 存储的时候我们步骤是:string值进行base64编码,然后存储到xml中;
3. 将int转化string的时候我在枚举变量前加入”Himi”,大家不喜欢可以换成自己喜欢的,但是千万不要删除!因为一旦你删除掉,然后将存储后的形式为: <1>数据</1> 这样会造成你读取数据出错!
读取数据步骤:根据key读取xml的编码过的string,然后base64解码出原始值,最终atoi转化成int扔出!
这其中应该说没什么难于理解的,所以这里不再赘述了;
那么下面我们来看这两个函数如何使用:
//base64编码存储
this
->saveDataByKeyValue(DATA_GOLD,
"10000"
);
//base64读取数据
this
->loadDataByKey(DATA_GOLD);
代码是不是用起来很方面呢!哈哈,这就对啦,不能因为编码弄得乱78糟~OK,运行项目,控制台你将看到如下显示:
Cocos2d: cocos2d: cocos2d-1.0.1-x-0.12.0
Cocos2d: cocos2d: GL_VENDOR: ImaginationTechnologies
Cocos2d: cocos2d: GL_RENDERER: PowerVR SGX 543
Cocos2d: cocos2d: GL_VERSION: OpenGL ES-CM 1.1 IMGSGX543-63.14.2
Cocos2d: cocos2d: GL_MAX_TEXTURE_SIZE:4096
Cocos2d: cocos2d: GL_MAX_MODELVIEW_STACK_DEPTH:16
Cocos2d: cocos2d: GL supports PVRTC:YES
Cocos2d: cocos2d: GL supports BGRA8888 textures:NO
Cocos2d: cocos2d: GL supports NPOT textures:YES
Cocos2d: cocos2d: GL supports discard_framebuffer:YES
Cocos2d: cocos2d: compiled with NPOT support:NO
Cocos2d: cocos2d: compiled with VBO support inTextureAtlas:NO
Cocos2d:此项目已存在保存数据的xml文件
Cocos2d:存储之前数据 key:index:0, value:10000
Cocos2d:存储编码后的数据 key:index:0, value:MTAwMDA=
Cocos2d:解码前的数据:MTAwMDA=
Cocos2d:解码后的数据:10000
存储的文件内容截图如下:
OK,最后呢,我要说一点,虽然我们使用base64可以对数据进行编码,但是对于不懂程序的用户来说基本无法修改,但是对于稍微有了解 base64编码的童鞋,那么直接可以在线解码你的这个编码,所以呢!最后Himi提醒大家,使用base64当中,大家可以书写一些算法结合 base64来使用!这样即使明文也不怕,至于怎么做,Himi就不再多说了 哈哈!