stoneage8.5/石器时代8.5客户端最新源代码/石器源码/systeminc/EncryptClient.h

97 lines
2.5 KiB
C
Raw Normal View History

2020-06-23 15:27:59 +08:00
#ifndef ENCRYPTCLIENT_H
#define ENCRYPTCLIENT_H
#include <stdlib.h>
template <unsigned char a1, unsigned char b1, unsigned char c1, unsigned char fst1,
unsigned char a2, unsigned char b2, unsigned char c2, unsigned char fst2>
class CEncryptClient
{
public:
CEncryptClient(){ Init(); }
public:
void Init() { m_nPos1 = m_nPos2 = 0; }
void Encrypt(unsigned char * bufMsg, int nLen, bool bMove = true);
void ChangeCode(DWORD dwData);
protected:
int m_nPos1;
int m_nPos2;
protected:
class CEncryptCode
{
public:
CEncryptCode()
{
//* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ELSE<53><EFBFBD><E3B7A8>ͬ<EFBFBD><CDAC>ֻΪ<D6BB><CEAA>ǿ<EFBFBD><C7BF><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD>)
unsigned char nCode = fst1;
int i;
for(i = 0; i < 256; i++)
{
m_bufEncrypt1[i] = nCode;
int nTemp = (a1*nCode) % 256;
nCode = (c1 + nTemp*nCode + b1*nCode) % 256;
}
//@@@ assert(nCode == fst1);
nCode = fst2;
for( i = 0; i < 256; i++)
{
m_bufEncrypt2[i] = nCode;
int nTemp = a2*nCode;
nCode = ((b2 + nTemp)*nCode + c2) & 0xFF;
}
//@@@ assert(nCode == fst2);
}
unsigned char m_bufEncrypt1[256];
unsigned char m_bufEncrypt2[256];
}m_cGlobalEncrypt; //??? Ӧ<>ijɾ<C4B3>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD>Լ<EFBFBD><D4BC>Դ
};
template <unsigned char a1, unsigned char b1, unsigned char c1, unsigned char fst1,
unsigned char a2, unsigned char b2, unsigned char c2, unsigned char fst2>
inline void CEncryptClient<a1, b1, c1, fst1, a2, b2, c2, fst2>::Encrypt(unsigned char * bufMsg, int nLen, bool bMove /*= true*/)
{
int nOldPos1 = m_nPos1;
int nOldPos2 = m_nPos2;
for(int i = 0; i < nLen; i++)
{
bufMsg[i] ^= m_cGlobalEncrypt.m_bufEncrypt1[m_nPos1];
bufMsg[i] ^= m_cGlobalEncrypt.m_bufEncrypt2[m_nPos2];
if(++m_nPos1 >= 256)
{
m_nPos1 = 0;
if(++m_nPos2 >= 256)
m_nPos2 = 0;
}
//@@@ assert(m_nPos1 >=0 && m_nPos1 < 256);
//@@@ assert(m_nPos2 >=0 && m_nPos2 < 256);
}
if(!bMove)
{
// <20>ָ<EFBFBD>ָ<EFBFBD><D6B8>
m_nPos1 = nOldPos1;
m_nPos2 = nOldPos2;
}
/*@@@ LOGCATCH("Encrypt fail."); exit(3);*/
}
template <unsigned char a1, unsigned char b1, unsigned char c1, unsigned char fst1,
unsigned char a2, unsigned char b2, unsigned char c2, unsigned char fst2>
inline void CEncryptClient<a1, b1, c1, fst1, a2, b2, c2, fst2>::ChangeCode(DWORD dwData)
{
DWORD dwData2 = dwData*dwData;
for(int i = 0; i < 256; i += 4)
{
*(DWORD*)(&m_cGlobalEncrypt.m_bufEncrypt1[i]) ^= dwData;
*(DWORD*)(&m_cGlobalEncrypt.m_bufEncrypt2[i]) ^= dwData2;
}
/*LOGCATCH("ChangeCode fail.");*/
}
#endif