cocos2d-x异或加密解密

来源:互联网 发布:董小飒外设淘宝店 编辑:程序博客网 时间:2024/05/21 17:18

XORCrypto.h代码

#pragma once#include <string>#include "cocos2d.h"using namespace std;class XORCrypto{private:XORCrypto(void);~XORCrypto(void);public:static XORCrypto *instance();void encode(char *pstr,int *pkey, int keyLength);void decode(char *pstr,int *pkey, int keyLength);char encodeChar(char c, int key);char decodeChar(char c, int key);unsigned char* encode(const char* pszFileName, unsigned long * pSize, int *pkey, int keyLength);unsigned char* decode(const char* pszFileName, unsigned long * pSize, int *pkey, int keyLength);};

XORCrypto.cpp代码

#include "XORCrypto.h"#include "ccMacros.h"USING_NS_CC;XORCrypto::XORCrypto(void){}XORCrypto::~XORCrypto(void){}XORCrypto * XORCrypto::instance(){static XORCrypto instance;return &instance;}char XORCrypto::encodeChar( char c, int key ){return c^key;}char XORCrypto::decodeChar( char c, int key ){return c^key;}void XORCrypto::encode( char *pstr,int *pkey, int keyLength ){int len=strlen(pstr);for(int i=0;i<len;i++){*(pstr+i)=encodeChar(*(pstr+i),pkey[i%keyLength]);}}void XORCrypto::decode( char *pstr,int *pkey, int keyLength ){int len=strlen(pstr);for(int i=0;i<len;i++){*(pstr+i)=decodeChar(*(pstr+i),pkey[i%keyLength]);}}unsigned char* XORCrypto::encode( const char* pszFileName, unsigned long * pSize, int *pkey, int keyLength ){if(pszFileName==NULL || pSize==NULL){CCLOG("Invalid parameters.");return NULL;}unsigned char * pBuffer = NULL;unsigned char * temp = new unsigned char[1];const char* pszMode = "r";size_t size = sizeof(unsigned char);*pSize = 0;do{// read the file from hardwarestd::string fullPath = cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFileName);FILE *fp = fopen(fullPath.c_str(), pszMode);CC_BREAK_IF(!fp);fseek(fp,0,SEEK_END);*pSize = ftell(fp);fseek(fp,0,SEEK_SET);pBuffer = new unsigned char[*pSize];for(int i=0; i<*pSize; i++){fseek(fp,i,SEEK_SET);fread(temp,size, 1,fp);*temp = *temp^pkey[i%keyLength];*(pBuffer+i) = *temp;}//*pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp);fclose(fp);} while (0);if (! pBuffer){std::string msg = "Get data from file(";msg.append(pszFileName).append(") failed!");CCLOG("%s", msg.c_str());}return pBuffer;}unsigned char* XORCrypto::decode( const char* pszFileName, unsigned long * pSize, int *pkey, int keyLength ){return encode(pszFileName, pSize, pkey, keyLength);}

测试代码:

const char *des = NULL;std::string jsonpath;cs::CSJsonDictionary *jsonDict = NULL;string prefix("db/");prefix.append("fileName");jsonpath = cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(prefix.c_str());unsigned long size = 0;int key[]={1,2,3,4,5};des = (char*)XORCrypto::instance()->decode(jsonpath.c_str(), &size,key, 5);std::string strDes(des);

对应的java实现,XORCrypto.java

public class XORCrypto {public static String encode(String str, String key) {byte[] data = str.getBytes();byte[] keyData = key.getBytes();int keyIndex = 0;for (int x = 0; x < str.length(); x++) {data[x] = (byte) (data[x] ^ keyData[keyIndex]);if (++keyIndex == keyData.length) {keyIndex = 0;}}return new String(data);}public static String decode(String str, String key) {return encode(str, key);}public static String encodeByCpp(String str, int key[]) {char strArray[] = str.toCharArray();int len=strArray.length;int keyLen = key.length;    for(int i=0;i<len;i++) {//    *(pstr+i)=MakecodeChar(*(pstr+i),pkey[i%5]);    int charInt = strArray[i];    charInt = charInt ^ key[i%keyLen];    strArray[i] = (char)charInt;    } return new String(strArray);}public static String decodeByCpp(String str, int key[]) {char strArray[] = str.toCharArray();int len=strArray.length;int keyLen = key.length;for(int i=0;i<len;i++) {//*(pstr+i)=CutcodeChar(*(pstr+i),pkey[i%5]);int charInt = strArray[i];    charInt = charInt ^ key[i%keyLen];    strArray[i] = (char)charInt;}return new String(strArray);  }public static void main(String[] args) {//String aa = "yang123";//String key = "yang";//String encode = encode(aa, key);//System.out.println("密文:"+encode);//String decode = decode(encode, key);//System.out.println("原文:"+decode);//String aa = "yang123";//int key[] = {1, 2, 3, 4, 5};//String encode = encodeByCpp(aa, key);//System.out.println("密文:"+encode);//String decode = decodeByCpp(encode, key);//System.out.println("原文:"+decode);}}




原创粉丝点击