public static bool DeEncryption(CircularBuffer <byte> data, NetPackage mPackage) { if (data.Length <= 8) { return(false); } for (int i = 0; i < 4; i++) { if (data [i] != mCheck [i]) { return(false); } } byte[] commandBytes = new byte[2]; data.CopyTo(4, commandBytes, 0, 2); mPackage.command = BitConverter.ToUInt16(commandBytes, 0); byte[] bodyLengthBytes = new byte[2]; data.CopyTo(6, bodyLengthBytes, 0, 2); UInt16 nBodyLength1 = BitConverter.ToUInt16(bodyLengthBytes, 0); if (nBodyLength1 < 0 || nBodyLength1 + 8 > data.Length) { return(false); } mPackage.buffer = new byte[nBodyLength1]; data.CopyTo(8, mPackage.buffer, 0, nBodyLength1); data.ClearBuffer(nBodyLength1 + 8); return(true); }
public static bool DeEncryption(CircularBuffer <byte> data, NetPackage mPackage) { if (data.Length <= 8) { return(false); } for (int i = 0; i < 4; i++) { if (data [i] != mCheck [i]) { return(false); } } int nBodyLength1 = data [4] | data [5] << 8 | data [6] << 16 | data [7] << 24; if (nBodyLength1 <= 0 || nBodyLength1 + 8 > data.Length) { return(false); } if (nBodyLength1 > mReceiveBuffer.Length) { mReceiveBuffer = new byte[nBodyLength1]; } data.CopyTo(8, mReceiveBuffer, 0, nBodyLength1); data.ClearBuffer(nBodyLength1 + 8); byte[] msg = mAES.Decryption(mReceiveBuffer, 0, nBodyLength1); if (msg == null) { DebugSystem.LogBitStream("解包失败: ", mReceiveBuffer); return(false); } int command = msg [0] | msg [1] << 8 | msg [2] << 16 | msg [3] << 24; int nBodyLength2 = msg.Length - 4; byte[] buffer = new byte[nBodyLength2]; Array.Copy(msg, 4, buffer, 0, nBodyLength2); mPackage.command = command; mPackage.buffer = buffer; return(true); }
public static bool DeEncryption(CircularBuffer <byte> data, NetReceivePackage mPackage) { if (data.Length <= 8) { return(false); } for (int i = 0; i < 4; i++) { if (data [i] != mCheck [i]) { return(false); } } byte[] commandBytes = new byte[2]; data.CopyTo(4, commandBytes, 0, 2); mPackage.nUniqueId = BitConverter.ToUInt16(commandBytes, 0); byte[] bodyLengthBytes = new byte[2]; data.CopyTo(6, bodyLengthBytes, 0, 2); UInt16 nBodyLength1 = BitConverter.ToUInt16(bodyLengthBytes, 0); if (nBodyLength1 < 0 || nBodyLength1 + 8 > data.Length) { return(false); } data.CopyTo(8, mPackage.buffer.Array, mPackage.buffer.Offset, nBodyLength1); data.ClearBuffer(nBodyLength1 + 8); ArraySegment <byte> mChe = new ArraySegment <byte> (mPackage.buffer.Array, mPackage.buffer.Offset, nBodyLength1); mPackage.buffer = mChe; return(true); }