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] = ''; } 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] = ''; } } 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 );}总结
以上是内存溢出为你收集整理的cocos rc4加解密全部内容,希望文章能够帮你解决cocos rc4加解密所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)