⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crypt.cpp

📁 A3服务端AccountServer源代码
💻 CPP
字号:
#include "stdinc.h"

//////////////////////////////////////////////////////////////////////////////
//
//	Simple Crypt Base Class
//
//////////////////////////////////////////////////////////////////////////////


CSimpleCrypt::CSimpleCrypt()
{
	m_ConstKey1 = 2366183;
	m_ConstKey2 = 1432754;
	m_DynamicKey = 79984829;
}

CSimpleCrypt::~CSimpleCrypt()
{
}

void CSimpleCrypt::Encrypt32bit(const char * pSrc, char * pDest)
{
	int DynamicKey = m_DynamicKey;

	for (int i = 0 ; i < 4 ; i++)
	{
		pDest[i] = (char)((BYTE)(pSrc[i]) ^ (DynamicKey >> 8));
		DynamicKey = ((BYTE)(pDest[i]) + DynamicKey) * m_ConstKey1 + m_ConstKey2;
	}
}

void CSimpleCrypt::Decrypt32bit(const char * pSrc, char * pDest)
{
	int DynamicKey = m_DynamicKey;

	for (int i = 0 ; i < 4 ; i++)
	{
		pDest[i] = (char)((BYTE)(pSrc[i]) ^ (DynamicKey >> 8));
		DynamicKey = ((BYTE)(pSrc[i]) + DynamicKey) * m_ConstKey1 + m_ConstKey2;
	}
}

void CSimpleCrypt::SetKey(int ConstKey1, int ConstKey2, int DynamicKey)
{
	m_ConstKey1 = ConstKey1;
	m_ConstKey2 = ConstKey2;
	m_DynamicKey = DynamicKey;
}

void CSimpleCrypt::Encrypt(void * pData, int nSize)
{
	char pBufSrc[4], pBufDest[4];
	char * p = (char*)pData;

	for(int i = 0 ; i < (int)(nSize / 4) ; i++) 
	{
		for(int j = 0 ; j < 4 ; j++) 
		{
			pBufSrc[j] = *(p + j);
		}
		
		Encrypt32bit(pBufSrc, pBufDest);

		for(j = 0 ; j < 4 ; j++) 
		{
			*(p + j) = pBufDest[j];
		}

		p = p + 4;
	}	
}

void CSimpleCrypt::Decrypt(void * pData, int nSize)
{
	char pBufSrc[4], pBufDest[4];
	char * p = (char*)pData;

	for(int i = 0 ; i < (int)(nSize / 4) ; i++) 
	{
		for(int j = 0 ; j < 4 ; j++) 
		{
			pBufSrc[j] = *(p + j);
		}
		
		Decrypt32bit(pBufSrc, pBufDest);

		for(j = 0 ; j < 4 ; j++) 
		{
			*(p + j) = pBufDest[j];
		}

		p = p + 4;
	}	
}



//////////////////////////////////////////////////////////////////////////////
//
//	Optimized Crypt Class for A3
//
//////////////////////////////////////////////////////////////////////////////


CMsgCrypt::CMsgCrypt()
{
	m_iMsgHeaderSize = 12;
}

CMsgCrypt::~CMsgCrypt()
{
}

void	CMsgCrypt::SetMsgHeaderSize(int MsgHeaderSize)
{
	m_iMsgHeaderSize = MsgHeaderSize;
}

void	CMsgCrypt::EncryptMsgBody(void * pData, int nSize)
{
	char * pMsgBody = (char *)pData;
	pMsgBody += m_iMsgHeaderSize;
	
	Encrypt(pMsgBody, nSize - m_iMsgHeaderSize);
}

void	CMsgCrypt::DecryptMsgBody(void * pData, int nSize)
{
	char * pMsgBody = (char *)pData;
	pMsgBody += m_iMsgHeaderSize;
	
	Decrypt(pMsgBody, nSize - m_iMsgHeaderSize);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -