void HandReceiveData(TCP_Info tInfo, byte[] recvByte) { wRealySize = recvByte.Length; MyDebug.SocketLog("解密前wRealySize:" + wRealySize); if (wRealySize == 0) { SocketConnet(); return; } var tmpBuf = new byte[wRealySize + 3]; Array.Copy(recvByte, tmpBuf, wRealySize); wRealySize = NetUtil.CrevasseBuffer(tInfo, tmpBuf, wRealySize); MyDebug.SocketLog("解密后wRealySize:" + wRealySize); //解释数据 var wDataSize = wRealySize - 4; var command = (TCP_Command)NetUtil.BytesToStruct(tmpBuf, typeof(TCP_Command), 4); var buff = new byte[wDataSize]; Array.Copy(tmpBuf, 4, buff, 0, wDataSize); MyDebug.SocketLog("HandReceiveData 11:"); if (command.wMainCmdID == (int)CMD_TYPE.MDM_KN_COMMAND && command.wSubCmdID == (int)CMD_TYPE.SUB_KN_DETECT_SOCKET) { } else { MyDebug.SocketLog("REV- --main command: " + command.wMainCmdID + "-- Sub Command:" + command.wSubCmdID); //处理消息 socketEvent.OnEventTCPSocketRead(command.wMainCmdID, command.wSubCmdID, buff, wDataSize); } MyDebug.SocketLog("HandReceiveData Over:"); }
private void ReadBuffer(BinaryReader buffers) { var infoBytes = buffers.ReadBytes(4); var tInfo = new TCP_Info(); tInfo = NetUtil.BytesToStruct <TCP_Info>(infoBytes); int lens = tInfo.wPacketSize; disConnectCount = 0; if (!hasStartTimer && lens == 16) { // StartTimer(); hasStartTimer = true; } if (lens > buffers.BaseStream.Length) { waitLen = lens; isWait = true; buffers.BaseStream.Position = 0; var dd = new byte[buffers.BaseStream.Length]; var temp = buffers.ReadBytes((int)buffers.BaseStream.Length); Array.Copy(temp, 0, dd, 0, (int)buffers.BaseStream.Length); if (sources == null) { sources = dd; } return; } MyDebug.SocketLog("tInfo.wPacketSize:" + tInfo.wPacketSize); var buffer = buffers.ReadBytes(tInfo.wPacketSize - 4); MyDebug.SocketLog("ArrayCopy Over!!!!"); HandReceiveData(tInfo, buffer); if (buffers.BaseStream.Position < buffers.BaseStream.Length) { ReadBuffer(buffers); } MyDebug.SocketLog("readBuffer Over!!!!"); }
public static bool mappedBuffer(byte[] buffer, int wDataSize) { //变量定义 var pInfo = new TCP_Info(); var cbCheckCode = 0; //映射数据 for (var i = Marshal.SizeOf(pInfo); i < wDataSize; i++) { cbCheckCode += buffer[i]; buffer[i] = ENCODE_MAP[buffer[i]]; } //设置数据 pInfo.cbCheckCode = (byte)(~cbCheckCode + 1); pInfo.wPacketSize = (ushort)wDataSize; pInfo.cbDataKind = 0x05; var bInfo = StructToBytes(pInfo); Array.Copy(bInfo, buffer, bInfo.Length); return(true); }
public static short CrevasseBuffer(TCP_Info tInfo, byte[] pcbDataBuffer, int wDataSize) { var wSnapCount = 0; if ((wDataSize % sizeof(int)) != 0) { wSnapCount = sizeof(int) - wDataSize % sizeof(int); } //数据包长度错误 if (wDataSize < (Marshal.SizeOf(typeof(TCP_Command)))) { return(0); } //映射 for (var i = 0; i < wDataSize; i++) { var bt = DECODE_MAP[pcbDataBuffer[i]]; pcbDataBuffer[i] = bt; } return((short)wDataSize); }
public ushort EncryptBuffer(byte[] pcbDataBuffer, ushort wDataSize) { //调整长度 ushort wEncrptySize = (ushort)(wDataSize - 4); ushort wSnapCount = 0; if ((wEncrptySize % 4 != 0)) { wSnapCount = (ushort)(4 - wEncrptySize % 4); } //校验码与字节映射 byte cbCheckCode = 0; ushort i = 0; for (i = 4; i < wDataSize; i++) { cbCheckCode += pcbDataBuffer[i]; pcbDataBuffer[i] = MapSendByte(pcbDataBuffer[i]); } //填写信息头 TCP_Info tcpHead = new TCP_Info(); tcpHead.cbCheckCode = (byte)(~cbCheckCode + 1); tcpHead.wPacketSize = wDataSize; tcpHead.cbDataKind = 0x02; int tcpHeadSize = System.Runtime.InteropServices.Marshal.SizeOf(tcpHead); byte[] headBuffer = StructBytesInterop.StructureToByteArraySmallEndian(tcpHead); System.Buffer.BlockCopy(headBuffer, 0, pcbDataBuffer, 0, tcpHeadSize); //创建密钥 uint dwXorKey = m_dwSendXorKey; if (m_dwSendPacketCount == 0) { //随机映射种子 dwXorKey = GetTimeStamp(); dwXorKey = SeedRandMap((ushort)dwXorKey); dwXorKey |= ((uint)SeedRandMap((ushort)(dwXorKey >> 16))) << 16; dwXorKey ^= g_dwPacketKey; m_dwSendXorKey = dwXorKey; m_dwRecvXorKey = dwXorKey; } //加密数据 ushort pwSeed = 4; uint pdwXor = 4; ushort wEncryptCount = (ushort)((wEncrptySize + wSnapCount) / 4); for (ushort j = 0; j < wEncryptCount; j++) { uint tempX = BitConverter.ToUInt32(pcbDataBuffer, (int)pdwXor); tempX ^= dwXorKey; byte[] tempBuffer = BitConverter.GetBytes(tempX); System.Buffer.BlockCopy(tempBuffer, 0, pcbDataBuffer, (int)pdwXor, 4); pdwXor += 4; ushort tempSeed = BitConverter.ToUInt16(pcbDataBuffer, (int)pwSeed); dwXorKey = SeedRandMap(tempSeed); pwSeed += 2; tempSeed = BitConverter.ToUInt16(pcbDataBuffer, (int)pwSeed); dwXorKey |= (uint)(SeedRandMap(tempSeed) << 16); pwSeed += 2; dwXorKey ^= g_dwPacketKey; } //插入密钥 if (m_dwSendPacketCount == 0) { System.Buffer.BlockCopy(pcbDataBuffer, 8, pcbDataBuffer, 12, wDataSize); byte[] keyBuffer = System.BitConverter.GetBytes(m_dwSendXorKey); System.Buffer.BlockCopy(keyBuffer, 0, pcbDataBuffer, 8, 4); tcpHead.wPacketSize += 4; headBuffer = StructBytesInterop.StructureToByteArraySmallEndian(tcpHead); System.Buffer.BlockCopy(headBuffer, 0, pcbDataBuffer, 0, tcpHeadSize); wDataSize += 4; } m_dwSendPacketCount++; m_dwSendXorKey = dwXorKey; return(wDataSize); }