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);}}
- cocos2d-x异或加密解密
- cocos2d-x lua 加密解密
- 【Cocos2d-x】数据加密解密
- 异或加密解密
- Cocos2d-x 资源加密解密实践总结
- Cocos2d-x 资源加密解密实践总结
- Cocos2d-x数据加密解密详解
- Cocos2d-x 资源加密解密实践总结
- cocos2d-x 3.x学习笔记<三> 加密解密 xxtea
- IOS 异或加密解密
- 一个异或加密解密函数
- Qt 使用异或进行加密解密
- 异或(XOR)运算加密/解密算法
- 异或运算实现加密解密
- 简单的异或加密解密算法
- qt中用异或加密解密
- quick-cocos2d-x 加密
- Cocos2d-X 学习笔记 15 cocos2dx之CCUserDefault及其数据进行加密解密
- UVa 11019 Matrix Matcher AC自动机 二维匹配
- 很不错的一个awk快速入门
- Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/Cacheable
- IOS Blocks示例
- GDB + gdbserver 远程调试android native code
- cocos2d-x异或加密解密
- java 线程安全问题之静态变量、实例变量、局部变量
- Extjs 模板组合
- ubuntu13.04代理下载并编译android4.3源码
- Mahout贝叶斯算法源码分析(4)
- spring的切入点指示符
- url参数中有+、空格、=、%、&、#等特殊符号的处理
- AWK单行脚本快速参考
- sanlock及其用法