internal ClientEncryptionSink(IClientChannelSink next, EncryptionHelper encrHelper) { nextSink = next; this.encrHelper = encrHelper; }
void IClientChannelSink.ProcessMessage(IMessage msg, ITransportHeaders requestHeaders, System.IO.Stream requestStream, out ITransportHeaders responseHeaders, out System.IO.Stream responseStream) { if (!encrHelper.HasKey) { ITransportHeaders encrHeaders = new TransportHeaders(); System.IO.MemoryStream encrStream = new System.IO.MemoryStream(); encrHelper.Initiate(encrHeaders); ITransportHeaders encredHeaders; System.IO.Stream encredStream; try { nextSink.ProcessMessage(msg, encrHeaders, encrStream, out encredHeaders, out encredStream); encrHelper.Finalize(encredHeaders); } catch (Exception ex) { encrHelper = new EncryptionHelper(); } } /*System.IO.MemoryStream memStream = EncryptionHelper.ToMemoryStream(requestStream); string text = new string(System.Text.UTF8Encoding.UTF8.GetChars(memStream.ToArray())); requestStream = memStream;*/ encrHelper.Encrypt(requestHeaders, ref requestStream); nextSink.ProcessMessage(msg, requestHeaders, requestStream, out responseHeaders, out responseStream); if (EncryptionHelper.IsEncrypted(responseHeaders)) { encrHelper.Decrypt(responseHeaders, ref responseStream); } else { encrHelper = new EncryptionHelper(); } /* System.IO.MemoryStream respStream = EncryptionHelper.ToMemoryStream(responseStream); text = new string(System.Text.UTF8Encoding.UTF8.GetChars(respStream.ToArray())); responseStream = respStream;*/ }
ServerProcessing IServerChannelSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, System.IO.Stream requestStream, out IMessage responseMsg, out ITransportHeaders responseHeaders, out System.IO.Stream responseStream) { if (EncryptionHelper.IsInitiation(requestHeaders)) { EncryptionHelper helper = new EncryptionHelper(); responseHeaders = new TransportHeaders(); responseStream = new System.IO.MemoryStream(); responseMsg = requestMsg; helper.Respond(requestHeaders, responseHeaders); contexts.Add(helper.Ticket, helper); return ServerProcessing.Complete; } else if (EncryptionHelper.IsEncrypted(requestHeaders) && contexts.ContainsKey(EncryptionHelper.GetTicket(requestHeaders)) && contexts[EncryptionHelper.GetTicket(requestHeaders)].HasKey) { EncryptionHelper helper = contexts[EncryptionHelper.GetTicket(requestHeaders)]; helper.Decrypt(requestHeaders, ref requestStream); /*System.IO.MemoryStream memStream = EncryptionHelper.ToMemoryStream(requestStream); string text = new string(System.Text.UTF8Encoding.UTF8.GetChars(memStream.ToArray()));*/ ServerProcessing result = nextSink.ProcessMessage(sinkStack, requestMsg, requestHeaders, requestStream, out responseMsg, out responseHeaders, out responseStream); /*System.IO.MemoryStream respStream = EncryptionHelper.ToMemoryStream(responseStream); string text = new string(System.Text.UTF8Encoding.UTF8.GetChars(respStream.ToArray())); responseStream = respStream;*/ helper.Encrypt(responseHeaders, ref responseStream); return result; } else throw new System.Security.Cryptography.CryptographicException("Message is not encrypted or no key could be found for this host."); }