|
加密函数
bool CEncode::Encode_Weapon()
{
//======================================================================//
// Weapon 数据
FILE_TYPE_WEAPON _dataArry[MAXDATABUFLEN];
int i=0;
{ // 读取数据
std::ifstream fs;
fs.open(m_FilePath.c_str());
// fs.ignore(std::numeric_limits <std::streamsize>::max(),'\n');
while(fs.good() )
{
ITEM_WEAPON item;
ZeroMemory(&item, sizeof(item));
//读取武器物品数据
item.Serialize(fs);
fs>>item.action_type;
fs>>item.figure_model>>item.inventory_model;
fs>>item.drop_model>>item.put_in_sound>>item.drop_sound>>item.brandish_sound>>item.hit_sound>>item.item_name;
fs.ignore(1);
fs.get(item.item_desc,256);
_dataArry=item;
i++;
}
i--;//fs读出的最后一个是无用的
// 关闭文件
fs.close();
}
// 打开文件
FILE* pFP = NULL;
//加密准备
BYTE* pEncodeBuf=NULL;
pEncodeBuf=new BYTE[sizeof(ITEM_WEAPON)*i];
ZeroMemory(pEncodeBuf,sizeof(ITEM_WEAPON)*i);
PacketEncode(FILE_ENCODE,(char*)pEncodeBuf,(char*)_dataArry,sizeof(ITEM_WEAPON)*i);
if(pEncodeBuf==NULL) return false;
//修改保存路径
std::string savePath;
char point='.';
const char *pChr=strrchr(m_FilePath.c_str(),point);
if(pChr==NULL) return false;
DWORD strlength=pChr -m_FilePath.c_str();
char tempchar[MAXFILEPATH]={0};
strncpy(tempchar,m_FilePath.c_str(),strlength);
savePath=tempchar; savePath+=FILE_EXT;
//写入加密后的文件
pFP = fopen(savePath.c_str(), "wb");
if(pFP == NULL)
{
return false;
}
FILE_HEADER header;
header.ver= FILE_VER;
header.num=i;
fwrite(&header,sizeof(header),1,pFP);
fwrite(pEncodeBuf,sizeof(ITEM_WEAPON)*i,1,pFP);
// 关闭文件
fclose(pFP);
pFP = NULL;
if(pEncodeBuf)
{
delete []pEncodeBuf;
pEncodeBuf=NULL;
}
return true;
}
// 加密算法
void CEncode: acketEncode(unsigned char addcode,char* buf,char* src,int len)
{
memcpy(buf,src,len);
DWORD index = 1;
BYTE lastvalue = buf[0];
BYTE temp = 0;
while(len > index){
temp = buf[index];
buf[index] = buf[index]^(lastvalue + buf[0] + addcode);
lastvalue = temp;
index++;
}
buf[0] = (buf[len-1]+addcode)^buf[0];
}
// 解密包算法
void PacketDecode(unsigned char addcode,char* buf,char* src,int len)
{
memcpy(buf,src,len);
buf[0] = buf[0]^(buf[len-1]+addcode);
for(int i = 1; i < len; i++){
buf = (buf[i-1] + buf[0] + addcode)^buf;
}
}
|
|