public override Message ReadMessage(ArraySegment <byte> buffer, BufferManager bufferManager, string contentType) { #if Encrypt if (buffer.Count == 0) { return(this._inner.ReadMessage(buffer, bufferManager, contentType)); } byte[] bytes; int index = buffer.Offset; ushort keyLen = BitConverter.ToUInt16(buffer.Array, index); index += 2; if (keyLen == 0) { bytes = AESEncrypt.Decrypt(buffer.Array, index, buffer.Count - index); } else { byte[] keyData = new byte[keyLen]; Array.Copy(buffer.Array, index, keyData, 0, keyLen); index += keyLen; ushort ivLen = BitConverter.ToUInt16(buffer.Array, index); index += 2; byte[] ivData = new byte[ivLen]; Array.Copy(buffer.Array, index, ivData, 0, ivLen); index += ivLen; byte[] key = RSAReceive.Decrypt(keyData, true); byte[] iv = RSAReceive.Decrypt(ivData, true); bytes = AESEncrypt.Decrypt(buffer.Array, index, buffer.Count - index, key, iv); } ArraySegment <byte> byteArray = new ArraySegment <byte>(bytes, 0, bytes.Length); return(this._inner.ReadMessage(byteArray, bufferManager, contentType)); #else return(this._inner.ReadMessage(buffer, bufferManager, contentType)); #endif }
public override Message ReadMessage(Stream stream, int maxSizeOfHeaders, string contentType) { #if Encrypt byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); int index = 0; ushort keyLen = BitConverter.ToUInt16(buffer, index); index += 2; if (keyLen == 0) { buffer = AESEncrypt.Decrypt(buffer, index, buffer.Length - index); } else { byte[] keyData = new byte[keyLen]; Array.Copy(buffer, index, keyData, 0, keyLen); index += keyLen; ushort ivLen = BitConverter.ToUInt16(buffer, index); index += 2; byte[] ivData = new byte[ivLen]; Array.Copy(buffer, index, ivData, 0, ivLen); index += ivLen; byte[] key = RSAReceive.Decrypt(keyData, true); byte[] iv = RSAReceive.Decrypt(ivData, true); buffer = AESEncrypt.Decrypt(buffer, index, buffer.Length - index, key, iv); } MemoryStream ms = new MemoryStream(buffer); return(this._inner.ReadMessage(ms, maxSizeOfHeaders, contentType)); #else return(this._inner.ReadMessage(stream, maxSizeOfHeaders, contentType)); #endif }