cocos2dx游戲資源加密之XXTEA
來源:程序員人生 發布時間:2015-02-03 08:39:28 閱讀次數:5620次
在手機游戲當中,游戲的資源加密保護是1件很重要的事情。
我花了兩天的時間整理了自己在游戲當中的資源加密問題,實現了跨平臺的資源流加密,這個都是偉人的肩膀之上的。在手機游戲資源加密這塊,能做到安全加密保護的確切不多,有研究過專業平臺愛加密的手機游戲加密解決方案,有興趣的可以點此了解:http://www.ijiami.cn/appprotect_mobile_games
大概的思路是這樣的,游戲資源通過XXTEA加密方法對流的加密方式,有自己的密鑰和標識,通過標識可知是不是有加密,密鑰是自己程序當中的。除非有密鑰,否則很難通過解出正確的文件。經過加密后,加密文件也就是游戲資源放在resource的自己文件夾中,否則在xcode編譯到趁機是會辨認不了文件。在程序中cocos2dx底層加入解密進程,就能夠把文件正確讀取出來,讓程序顯示。經實驗,已可以讀取,png,plist,json文件。
現在記錄下實現的步驟
鏈接: http://pan.baidu.com/s/1ntx98VZ 密碼: qyqe 去下載加密資源的腳本,這個是quick-cocos2d-x提取出來的打包工具。
pack_files.sh -i olddir -o newdir -ek XXTEA -es decodetest
把ResourcesDecode和xxtea4個文件加入到cocos2dx/platform下;
把platform/ResourcesDecode.cpp
platform/xxtea.c 加入到cocos2dx/platform的android.mk文件中,加入android編譯。
寫1個單例用來保存密碼和對流解密進程,代碼以下:
<span style="font-family:Arial;font-size:14px;">CCAssert(buf != NULL, "decodeData buf not NULL");
unsigned char* buffer = NULL;
ResourcesDecode* decode = ResourcesDecode::sharedDecode();
bool isXXTEA = decode && decode->m_xxteaEnabled;
for (unsigned int i = 0; isXXTEA && i < decode->m_xxteaSignLen && i < size; ++i)
{
isXXTEA = buf[i] == decode->m_xxteaSign[i];
}
if (isXXTEA)
{
//decrypt XXTEA
xxtea_long len = 0;
buffer = xxtea_decrypt(buf+decode->m_xxteaSignLen, (xxtea_long)size -(xxtea_long)decode->m_xxteaSignLen, (unsigned char*)decode->m_xxteaKey, (xxtea_long)decode->m_xxteaKeyLen, &len);
delete [] buf;
buf = NULL;
size = len;
}
else
{
buffer = buf;
}
if (pSize)
{
*pSize = size;
}
return buffer;</span>
buffer就是經過XXTEA解密后正確的流。
在CCFileUtils::getFileData()當中return返回之前調用解密pBuffer =ResourcesDecode::sharedDecode()->decodeData(pBuffer, size, pSize);這里是跨平臺的讀取資源的方法。
在ZipFile::getFileData()當中也加入解密方法pBuffer =ResourcesDecode::sharedDecode()->decodeData(pBuffer, fileInfo.uncompressed_size, pSize);這個是android讀取plist的地方,我也不太清楚為何android會在這里讀取資源。
在bool CCSAXParser::parse(const char *pszFile)中把本來的rt改成rb : char* pBuffer = (char*)CCFileUtils::sharedFileUtils()->getFileData(pszFile,/*"rt"*/"rb", &size);
ios的修改地方 不1樣
在CCFileUtilsIOS中的createCCDictionaryWithContentsOfFile修改以下,注釋掉的是本來的,后面是新增的。
<span style="font-family:Arial;font-size:14px;">CCDictionary* CCFileUtilsIOS::createCCDictionaryWithContentsOfFile(const std::string& filename)
{
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(filename.c_str());
// NSString* pPath = [NSString stringWithUTF8String:fullPath.c_str()];
// NSDictionary* pDict = [NSDictionary dictionaryWithContentsOfFile:pPath];
unsigned long fileSize = 0;
unsigned char* pFileData = CCFileUtils::sharedFileUtils()->getFileData(fullPath.c_str(), "rb", &fileSize);
NSData *data = [[[NSData alloc] initWithBytes:pFileData length:fileSize] autorelease];
delete []pFileData;
NSPropertyListFormat format;
NSString *error;
NSMutableDictionary *pDict = (NSMutableDictionary *)[
NSPropertyListSerialization propertyListFromData:data
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&error];</span>
在CCImage.mm當中修改,一樣是注釋是本來的,后面是新增的。
<span style="font-family:Arial;font-size:14px;">static bool _initWithFile(const char* path, tImageInfo *pImageinfo)
{
CGImageRef CGImage;
UIImage *jpg;
UIImage *png;
bool ret;
// convert jpg to png before loading the texture
// NSString *fullPath = [NSString stringWithUTF8String:path];
// jpg = [[UIImage alloc] initWithContentsOfFile: fullPath];
unsigned long fileSize = 0;
unsigned char* pFileData = cocos2d::CCFileUtils::sharedFileUtils()->getFileData(path, "rb", &fileSize);
NSData *adata = [[NSData alloc] initWithBytes:pFileData length:fileSize];
delete []pFileData;
jpg = [[UIImage alloc] initWithData:adata];</span>
android平臺
在CCImageCommon_cpp當中修改以下
<span style="font-family:Arial;font-size:14px;">bool CCImage::initWithImageFileThreadSafe(const char *fullpath, EImageFormat imageType)
{
bool bRet = false;
unsigned long nSize = 0;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
CCFileUtilsAndroid *fileUitls = (CCFileUtilsAndroid*)CCFileUtils::sharedFileUtils();
// unsigned char *pBuffer = fileUitls->getFileDataForAsync(fullpath, "rb", &nSize);
unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData(fullpath, "rb", &nSize);</span>
到此,基本結束了。
在自己程序當中加入資源前把設置密鑰和標識和自己加密資源時的1樣:ResourcesDecode::sharedDecode()->setXXTeaKey("XXTEA",strlen("XXTEA"),"decodetest",strlen("decodetest"));
其它就正常的讀取和顯示。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈