|
//随机映射WORD CServerSocketItem::SeedRandMap(WORD wSeed){ DWORD dwHold = wSeed; return (WORD)((dwHold = dwHold * 241103L + 2533101L) >> 16);}//映射发送数据BYTE CServerSocketItem::MapSendByte(BYTE const cbData){ BYTE cbMap = g_SendByteMap[(BYTE)(cbData+m_cbSendRound)]; m_cbSendRound += 3; return cbMap;}//映射接收数据BYTE CServerSocketItem::MapRecvByte(BYTE const cbData){ BYTE cbMap = g_RecvByteMap[cbData] - m_cbRecvRound; m_cbRecvRound += 3; return cbMap;}
//加密数据WORD CServerSocketItem::EncryptBuffer(BYTE pcbDataBuffer[], WORD wDataSize, WORD wBufferSize){ WORD i = 0; //效验参数 ASSERT(wDataSize >= sizeof(TCP_Head)); ASSERT(wDataSize <= (sizeof(TCP_Head) + SOCKET_TCP_BUFFER)); ASSERT(wBufferSize >= (wDataSize + 2*sizeof(DWORD))); //调整长度 WORD wEncryptSize = wDataSize - sizeof(TCP_Info), wSnapCount = 0; if ((wEncryptSize % sizeof(DWORD)) != 0) { wSnapCount = sizeof(DWORD) - wEncryptSize % sizeof(DWORD); memset(pcbDataBuffer + sizeof(TCP_Info) + wEncryptSize, 0, wSnapCount); } //效验码与字节映射 BYTE cbCheckCode = 0; for (i = sizeof(TCP_Info); i < wDataSize; i++) { cbCheckCode += pcbDataBuffer; pcbDataBuffer = MapSendByte(pcbDataBuffer); } //填写信息头 TCP_Head * pHead = (TCP_Head *)pcbDataBuffer; pHead->TCPInfo.cbCheckCode = ~cbCheckCode + 1; pHead->TCPInfo.wPacketSize = wDataSize; pHead->TCPInfo.cbVersion = SOCKET_TCP_VER; //加密数据 DWORD dwXorKey = m_dwSendXorKey; WORD * pwSeed = (WORD *)(pcbDataBuffer + sizeof(TCP_Info)); DWORD * pdwXor = (DWORD *)(pcbDataBuffer + sizeof(TCP_Info)); WORD wEncrypCount = (wEncryptSize + wSnapCount) / sizeof(DWORD); for (i = 0; i < wEncrypCount; i++) { *pdwXor++ ^= dwXorKey; dwXorKey = SeedRandMap(*pwSeed++); dwXorKey |= ((DWORD)SeedRandMap(*pwSeed++)) << 16; dwXorKey ^= g_dwPacketKey; } //设置变量 m_dwSendPacketCount++; m_dwSendXorKey = dwXorKey; return wDataSize;}//解密数据WORD CServerSocketItem::CrevasseBuffer(BYTE pcbDataBuffer[], WORD wDataSize){ WORD i = 0; //效验参数 ASSERT(wDataSize >= sizeof(TCP_Head)); ASSERT(((TCP_Head *)pcbDataBuffer)->TCPInfo.wPacketSize == wDataSize); //调整长度 WORD wSnapCount = 0; if ((wDataSize % sizeof(DWORD)) != 0) { wSnapCount = sizeof(DWORD) - wDataSize % sizeof(DWORD); memset(pcbDataBuffer + wDataSize, 0, wSnapCount); } //提取密钥 if (m_dwRecvPacketCount == 0) { ASSERT(wDataSize >= (sizeof(TCP_Head) + sizeof(DWORD))); if (wDataSize < (sizeof(TCP_Head) + sizeof(DWORD))) throw TEXT("数据包解密长度错误"); m_dwRecvXorKey = *(DWORD *)(pcbDataBuffer + sizeof(TCP_Head)); m_dwSendXorKey = m_dwRecvXorKey; MoveMemory(pcbDataBuffer + sizeof(TCP_Head), pcbDataBuffer + sizeof(TCP_Head) + sizeof(DWORD), wDataSize - sizeof(TCP_Head) - sizeof(DWORD)); wDataSize -= sizeof(DWORD); ((TCP_Head *)pcbDataBuffer)->TCPInfo.wPacketSize -= sizeof(DWORD); } //解密数据 DWORD dwXorKey = m_dwRecvXorKey; DWORD * pdwXor = (DWORD *)(pcbDataBuffer + sizeof(TCP_Info)); WORD * pwSeed = (WORD *)(pcbDataBuffer + sizeof(TCP_Info)); WORD wEncrypCount = (wDataSize + wSnapCount - sizeof(TCP_Info)) / 4; for (i = 0; i < wEncrypCount; i++) { if ((i == (wEncrypCount - 1)) && (wSnapCount > 0)) { BYTE * pcbKey = ((BYTE *) & m_dwRecvXorKey) + sizeof(DWORD) - wSnapCount; CopyMemory(pcbDataBuffer + wDataSize, pcbKey, wSnapCount); } dwXorKey = SeedRandMap(*pwSeed++); dwXorKey |= ((DWORD)SeedRandMap(*pwSeed++)) << 16; dwXorKey ^= g_dwPacketKey; *pdwXor++ ^= m_dwRecvXorKey; m_dwRecvXorKey = dwXorKey; } //效验码与字节映射 TCP_Head * pHead = (TCP_Head *)pcbDataBuffer; BYTE cbCheckCode = pHead->TCPInfo.cbCheckCode;; for (i = sizeof(TCP_Info); i < wDataSize; i++) { pcbDataBuffer = MapRecvByte(pcbDataBuffer); cbCheckCode += pcbDataBuffer; } if (cbCheckCode != 0) throw TEXT("数据包效验码错误"); return wDataSize;}
|
|