//解密数据 WORD CServerSocketItem::CrevasseBuffer(BYTE pcbDataBuffer[], WORD wDataSize) { WORD i = 0; //效验参数 ASSERT(wDataSize>=sizeof(CMD_Head)); ASSERT(((CMD_Head *)pcbDataBuffer)->CmdInfo.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(CMD_Head)+sizeof(DWORD))); if (wDataSize<(sizeof(CMD_Head)+sizeof(DWORD))) throw TEXT("数据包解密长度错误"); m_dwRecvXorKey=*(DWORD *)(pcbDataBuffer+sizeof(CMD_Head)); m_dwSendXorKey=m_dwRecvXorKey; MoveMemory(pcbDataBuffer+sizeof(CMD_Head),pcbDataBuffer+sizeof(CMD_Head)+sizeof(DWORD), wDataSize-sizeof(CMD_Head)-sizeof(DWORD)); wDataSize-=sizeof(DWORD); ((CMD_Head *)pcbDataBuffer)->CmdInfo.wPacketSize-=sizeof(DWORD); } //解密数据 DWORD dwXorKey=m_dwRecvXorKey; DWORD * pdwXor=(DWORD *)(pcbDataBuffer+sizeof(CMD_Info)); WORD * pwSeed=(WORD *)(pcbDataBuffer+sizeof(CMD_Info)); WORD wEncrypCount=(wDataSize+wSnapCount-sizeof(CMD_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; } //效验码与字节映射 CMD_Head * pHead=(CMD_Head *)pcbDataBuffer; BYTE cbCheckCode=pHead->CmdInfo.cbCheckCode;; for (i=sizeof(CMD_Info);i<wDataSize;i++) { pcbDataBuffer[i]=MapRecvByte(pcbDataBuffer[i]); cbCheckCode+=pcbDataBuffer[i]; } if (cbCheckCode!=0) throw TEXT("数据包效验码错误"); return wDataSize; }
//解密数据 WORD CCNetDelegate::CrevasseBuffer(BYTE* pcbDataBuffer,WORD wDataSize) { //调整长度 WORD wSnapCount=0; if ((wDataSize%sizeof(DWORD))!=0) { wSnapCount=sizeof(DWORD)-wDataSize%sizeof(DWORD); memset(pcbDataBuffer+wDataSize,0,wSnapCount); } //解密数据 DWORD dwXorKey=m_dwRecvXorKey; DWORD * pdwXor=(DWORD *)(pcbDataBuffer+4); WORD * pwSeed=(WORD *)(pcbDataBuffer+4); WORD wEncrypCount=(wDataSize+wSnapCount-4)/4; for (WORD i=0;i<wEncrypCount;i++) { if ((i==(wEncrypCount-1))&&(wSnapCount>0)) { BYTE * pcbKey=((BYTE *)&m_dwRecvXorKey)+sizeof(DWORD)-wSnapCount; memcpy(pcbDataBuffer+wDataSize,pcbKey,wSnapCount); } dwXorKey=SeedRandMap(*pwSeed++); dwXorKey|=((DWORD)SeedRandMap(*pwSeed++))<<16; dwXorKey^=g_dwPacketKey; *pdwXor++^=m_dwRecvXorKey; m_dwRecvXorKey=dwXorKey; } //效验码与字节映射 struct CMD_Head * pHead=(struct CMD_Head *)pcbDataBuffer; BYTE cbCheckCode=pHead->CmdInfo.cbCheckCode; for (int i=4;i<wDataSize;i++) { pcbDataBuffer[i]=MapRecvByte(pcbDataBuffer[i]); cbCheckCode+=pcbDataBuffer[i]; } // if (cbCheckCode!=0) throw TEXT("数据包效验码错误"); return wDataSize; }