public override void WriteMessage(Message message, Stream stream) { #if Encrypt if (RSAProvider.NoRSA()) { this._inner.WriteMessage(message, stream); } MemoryStream ms = new MemoryStream(); this._inner.WriteMessage(message, ms); byte[] buffer = ms.ToArray(); byte[] key = null; byte[] iv = null; RSACryptoServiceProvider rsa = RSAProvider.UseRSA(); if (null == rsa) { buffer = AESEncrypt.Encrypt(buffer, 0, buffer.Length); } else { byte[] keyData; byte[] ivData; BuildAESKey(out keyData, out ivData); buffer = AESEncrypt.Encrypt(buffer, 0, buffer.Length, keyData, ivData); key = rsa.Encrypt(keyData, true); iv = rsa.Encrypt(ivData, true); } if (null == key) { stream.WriteByte(0); stream.WriteByte(0); stream.Write(buffer, 0, buffer.Length); } else { stream.Write(BitConverter.GetBytes((ushort)key.Length), 0, 2); stream.Write(key, 0, key.Length); stream.Write(BitConverter.GetBytes((ushort)iv.Length), 0, 2); stream.Write(iv, 0, iv.Length); stream.Write(buffer, 0, buffer.Length); } #else this._inner.WriteMessage(message, stream); #endif }
public override ArraySegment <byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset) { #if Encrypt if (RSAProvider.NoRSA()) { return(this._inner.WriteMessage(message, maxMessageSize, bufferManager)); } ArraySegment <byte> bytes = this._inner.WriteMessage(message, maxMessageSize, bufferManager); if (bytes.Count == 0) { return(bytes); } byte[] buffer; byte[] key = null; byte[] iv = null; RSACryptoServiceProvider rsa = RSAProvider.UseRSA(); if (null == rsa) { buffer = AESEncrypt.Encrypt(bytes.Array, bytes.Offset, bytes.Count); } else { byte[] keyData; byte[] ivData; BuildAESKey(out keyData, out ivData); buffer = AESEncrypt.Encrypt(bytes.Array, bytes.Offset, bytes.Count, keyData, ivData); key = rsa.Encrypt(keyData, true); iv = rsa.Encrypt(ivData, true); } if (null == key) { int totalLength = 2 + buffer.Length + messageOffset; byte[] totalBytes = bufferManager.TakeBuffer(totalLength); int index = messageOffset; totalBytes[index] = 0; totalBytes[index + 1] = 0; index += 2; Array.Copy(buffer, 0, totalBytes, index, buffer.Length); ArraySegment <byte> byteArray = new ArraySegment <byte>(totalBytes, messageOffset, totalLength - messageOffset); return(byteArray); } else { int totalLength = 4 + key.Length + iv.Length + buffer.Length + messageOffset; byte[] totalBytes = bufferManager.TakeBuffer(totalLength); int index = messageOffset; Array.Copy(BitConverter.GetBytes((ushort)key.Length), 0, totalBytes, index, 2); index += 2; Array.Copy(key, 0, totalBytes, index, key.Length); index += key.Length; Array.Copy(BitConverter.GetBytes((ushort)iv.Length), 0, totalBytes, index, 2); index += 2; Array.Copy(iv, 0, totalBytes, index, iv.Length); index += iv.Length; Array.Copy(buffer, 0, totalBytes, index, buffer.Length); ArraySegment <byte> byteArray = new ArraySegment <byte>(totalBytes, messageOffset, totalLength - messageOffset); return(byteArray); } #else return(this._inner.WriteMessage(message, maxMessageSize, bufferManager, messageOffset)); #endif }