溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

cocos2dx游戲資源加密之XXTEA

發(fā)布時(shí)間:2020-07-02 00:49:06 來源:網(wǎng)絡(luò) 閱讀:926 作者:大水牛牛 欄目:移動(dòng)開發(fā)

在手機(jī)游戲當(dāng)中,游戲的資源加密保護(hù)是一件很重要的事情。

我花了兩天的時(shí)間整理了自己在游戲當(dāng)中的資源加密問題,實(shí)現(xiàn)了跨平臺(tái)的資源流加密,這個(gè)都是巨人的肩膀之上的。在手機(jī)游戲資源加密這塊,能做到安全加密保護(hù)的確實(shí)不多,有研究過專業(yè)平臺(tái)愛加密的手機(jī)游戲加密解決方案,有興趣的可以點(diǎn)此了解:http://www.ijiami.cn/appprotect_mobile_games

大概的思路是這樣的,游戲資源通過XXTEA加密方法對流的加密方式,有自己的密鑰和標(biāo)識(shí),通過標(biāo)識(shí)可知是否有加密,密鑰是自己程序當(dāng)中的。除非有密鑰,否則很難通過解出正確的文件。經(jīng)過加密后,加密文件也就是游戲資源放在resource的自己文件夾中,否則在xcode編譯到趁機(jī)是會(huì)識(shí)別不了文件。在程序中cocos2dx底層加入解密過程,就可以把文件正確讀取出來,讓程序顯示。經(jīng)試驗(yàn),已經(jīng)可以讀取,png,plist,json文件。

現(xiàn)在記錄下實(shí)現(xiàn)的步驟

鏈接: http://pan.baidu.com/s/1ntx98VZ  密碼: qyqe 去下載加密資源的腳本,這個(gè)是quick-cocos2d-x提取出來的打包工具。

pack_files.sh -i olddir -o newdir -ek XXTEA -es decodetest

把ResourcesDecode和xxtea四個(gè)文件加入到cocos2dx/platform下;

把platform/ResourcesDecode.cpp \
platform/xxtea.c \加入到cocos2dx/platform的android.mk文件中,加入android編譯。

寫一個(gè)單例用來保存密碼和對流解密過程,代碼如下:

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;

buffer就是經(jīng)過XXTEA解密后正確的流。

在CCFileUtils::getFileData()當(dāng)中return返回之前調(diào)用解密pBuffer =ResourcesDecode::sharedDecode()->decodeData(pBuffer, size, pSize);這里是跨平臺(tái)的讀取資源的方法。

在ZipFile::getFileData()當(dāng)中也加入解密方法pBuffer =ResourcesDecode::sharedDecode()->decodeData(pBuffer, fileInfo.uncompressed_size, pSize);這個(gè)是android讀取plist的地方,我也不太清楚為什么android會(huì)在這里讀取資源。

在bool CCSAXParser::parse(const char *pszFile)中把原先的rt改為rb  : char* pBuffer = (char*)CCFileUtils::sharedFileUtils()->getFileData(pszFile,/*"rt"*/"rb", &size);

ios的修改地方 不一樣

在CCFileUtilsIOS中的createCCDictionaryWithContentsOfFile修改如下,注釋掉的是原先的,后面是新增的。

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];

在CCImage.mm當(dāng)中修改,同樣是注釋是原先的,后面是新增的。

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];

android平臺(tái)

在CCImageCommon_cpp當(dāng)中修改如下

bool CCImage::initWithImageFileThreadSafe(const char *fullpath, EImageFormat p_w_picpathType)
{
    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);

到此,基本結(jié)束了。

在自己程序當(dāng)中加入資源前把設(shè)置密鑰和標(biāo)識(shí)和自己加密資源時(shí)的一樣:ResourcesDecode::sharedDecode()->setXXTeaKey("XXTEA",strlen("XXTEA"),"decodetest",strlen("decodetest"));

其它就正常的讀取和顯示。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI