cocos rc4加解密

前端之家收集整理的这篇文章主要介绍了cocos rc4加解密前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
static Data getData(const std::string& filename,bool forString)
{
    if (filename.empty())
    {
        return Data::Null;
    }

    Data ret;
    unsigned char* buffer = nullptr;
    size_t size = 0;
    size_t readsize;
    const char* mode = nullptr;

    if (forString)
        mode = "rt";
    else
        mode = "rb";

    auto fileutils = FileUtils::getInstance();
    do
    {
        // Read the file from hardware
        std::string fullPath = fileutils->fullPathForFilename(filename);
        FILE *fp = fopen(fileutils->getSuitableFOpen(fullPath).c_str(),mode);
        CC_BREAK_IF(!fp);
        fseek(fp,SEEK_END);
        size = ftell(fp);
        fseek(fp,SEEK_SET);

        if (forString)
        {
            buffer = (unsigned char*)malloc(sizeof(unsigned char) * (size + 1));
            buffer[size] = '\0';
        }
        else
        {
            buffer = (unsigned char*)malloc(sizeof(unsigned char) * size);
        }

        readsize = fread(buffer,sizeof(unsigned char),size,fp);
        fclose(fp);

        if (forString && readsize < size)
        {
            buffer[readsize] = '\0';
        }
    } while (0);

    if (nullptr == buffer || 0 == readsize)
    {
        CCLOG("Get data from file %s Failed",filename.c_str());
    }
    else
    {
#ifdef CRYPT_RES
        pack_crpyt( buffer,(unsigned long )readsize );
#endif

        ret.fastSet(buffer,readsize);
    }

    return ret;
}



bool AppDelegate::applicationDidFinishLaunching()
{
    char key[256]={"hellodytqwe123!@#&*"};
    pack_crpyt_init( (unsigned char*)key,strlen( key ) );
    
    // set default FPS
    Director::getInstance()->setAnimationInterval(1.0 / 60.0f);
    
    Director::getInstance()->setClearColor(Color4F(0,0));

    // register lua module
    auto engine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(engine);
    lua_State* L = engine->getLuaStack()->getLuaState();
    lua_module_register(L);

    register_all_packages();
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    register_all_cocos2dx_TalkingDataGA(L);
    register_all_cocos2dx_TDGAAccount(L);
    register_all_cocos2dx_TDGAMission(L);
    register_all_cocos2dx_TDGAVirtualCurrency(L);
    register_all_cocos2dx_TDGAItem(L);
#endif


    LuaStack* stack = engine->getLuaStack();
    stack->setXXTEAKeyAndSign("2dxLua",strlen("2dxLua"),"XXTEA",strlen("XXTEA"));

    //register custom function
    //LuaStack* stack = engine->getLuaStack();
    //register_custom_function(stack->getLuaState());
    auto state = engine->getLuaStack()->getLuaState();
    if ( state )
    {
        lua_getglobal( state,"_G");

        register_all_cocos2dx_custom( state );
        register_all_cocos2dx_custom_manual( state );

        lua_settop( state,0);
    }

#if (COCOS2D_DEBUG > 0) && (CC_CODE_IDE_DEBUG_SUPPORT > 0)
    std::vector<std::string> searchDirs ;
    
    searchDirs.push_back( "" );
    searchDirs.push_back( "res" );
    searchDirs.push_back( "src" );
    
    cocos2d::FileUtils::getInstance()->setSearchPaths( searchDirs );

    Cocos2dUtils::getOrCreateGLProgram( "res/shader/highlight.vsh","res/shader/highlight.fsh" );

    // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
    auto runtimeEngine = RuntimeEngine::getInstance();
    runtimeEngine->addRuntime(RuntimeLuaImpl::create(),kRuntimeEngineLua);
    runtimeEngine->start();
#else
    std::vector<std::string> searchDirs ;
    
    searchDirs.push_back( "" );
    searchDirs.push_back( "res" );
    searchDirs.push_back( "src" );
    
    cocos2d::FileUtils::getInstance()->setSearchPaths( searchDirs );

    Cocos2dUtils::getOrCreateGLProgram( "res/shader/highlight.vsh","res/shader/highlight.fsh" );
    

    if (engine->executeScriptFile("src/main.lua"))
    {
        return false;
    }
#endif

    return true;
}



std::string FileUtils::getStringFromFile(const std::string& filename)
{
    Data data = getData(filename,true);
    if (data.isNull())
        return "";

    std::string ret((const char*)data.getBytes());
    return ret;
}

void Cocos2dUtils::readBufferFromFile( ByteBuffer* buffer,const std::string fileName )
{
    Data data =  FileUtils::getInstance()->getDataFromFile( fileName );
    if( data.getSize() > 0 )
    {
        buffer->writeBytes( ( char* )data.getBytes(),data.getSize() );
    }
}


//
//  RC4.h
//  knight
//
//  Created by yingtuodeng on 14-6-29.
//
//

#ifndef __knight__RC4__
#define __knight__RC4__

/*
 初始化函数
 参数1是一个256长度的char型数组,定义为: unsigned char sBox[256];
 参数2是密钥,其内容可以随便定义:char key[256];
 参数3是密钥的长度,Len = strlen(key);
 */
void rc4_init(unsigned char*s,unsigned char*key,unsigned long Len);


/*加解密
 其中,参数1是上边rc4_init函数中,被搅乱的S-Box;
 参数2是需要加密的数据data;
 参数3是data的长度.
 */
void rc4_crypt(unsigned char*s,unsigned char*Data,unsigned long Len);



void pack_crpyt( unsigned char* data,unsigned long len );


void pack_crpyt_init( unsigned char* key,unsigned long key_len );


#endif /* defined(__knight__RC4__) */



#include "RC4.h"
#include <string>

/*
 初始化函数
 参数1是一个256长度的char型数组,定义为: unsigned char sBox[256];
 参数2是密钥,其内容可以随便定义:char key[256];
 参数3是密钥的长度,Len = strlen(key);
 */
void rc4_init(unsigned char*s,unsigned long Len)
{
    int i=0,j=0;
    char k[256]={0};
    unsigned char tmp=0;
    for(i=0;i<256;i++)
    {
        s[i]=i;
        k[i]=key[i%Len];
    }
    for(i=0;i<256;i++)
    {
        j=(j+s[i]+k[i])%256;
        tmp=s[i];
        s[i]=s[j];//交换s[i]和s[j]
        s[j]=tmp;
    }
}



/*加解密
 其中,参数1是上边rc4_init函数中,被搅乱的S-Box;
 参数2是需要加密的数据data;
 参数3是data的长度.
 */
void rc4_crypt(unsigned char*s,j=0,t=0;
    unsigned long k=0;
    unsigned char tmp;
    for(k=0;k<Len;k++)
    {
        i=(i+1)%256;
        j=(j+s[i])%256;
        tmp=s[i];
        s[i]=s[j];//交换s[x]和s[y]
        s[j]=tmp;
        t=(s[i]+s[j])%256;
        Data[k]^=s[t];
    }
}


unsigned char __g_rc4_sBox[ 256 ] = { 0 };

void pack_crpyt( unsigned char* data,unsigned long len )
{
    char sBox[256];
    memcpy( sBox,__g_rc4_sBox,256 );
    
    rc4_crypt( (unsigned char*)sBox,data,len );
}

void pack_crpyt_init( unsigned char* key,unsigned long key_len )
{
    rc4_init( __g_rc4_sBox,key,key_len );
}
原文链接:https://www.f2er.com/cocos2dx/339288.html

猜你在找的Cocos2d-x相关文章