Read Encrypt Soap message in BeforeDeserialize
<p>I try read encrypt soap message in BeforeDeserialize and write soap message as string in txt files. But output is empty. </p>

<p>If I don't send encrypt soap message, I can read soap message stream and also output in txt file is correct.</p>

<p>Can somebody help me ? I don't know what is bad. 
I use TDES to encrypt and decrypt soap, message. But I want read encrypt message. Encrypt and Decrypt Soap message is correct. Only if I try read encrypt message stream and write in txt file, the output is empty...</p>

<pre><code>private void CopyStream(Stream from, Stream to)
        {
            TextReader reader = new StreamReader(from);
            TextWriter writer = new StreamWriter(to);
            writer.Write(reader.ReadToEnd());
            writer.Flush();
        }

public override void ProcessMessage(SoapMessage message)
{
    switch (message.Stage)
    {
        case SoapMessageStage.BeforeSerialize:
            break;
        case SoapMessageStage.AfterSerialize:
            EncryptXML();
            break;
        case SoapMessageStage.BeforeDeserialize:
            {
                MemoryStream tempStream = new MemoryStream();
                CopyStream(message.Stream, tempStream);
                tempStream.Position = 0;

                string a = Encoding.UTF8.GetString(tempStream.ToArray());

                TextWriter tw = new StreamWriter("D:\\xml.txt");
                tw.WriteLine(a);
                tw.Close();
                message.Stream.Position = 0;

                DecryptXML();  
            }
            break;
        case SoapMessageStage.AfterDeserialize:
            break;
        default:
            throw new ArgumentException("Chyba pri serializacii SOAP msg.");
    }
}

 public void EncryptXML()
        {
            computeKeyAndIV();

            newStream.Position = 0;
            XmlTextReader reader = new XmlTextReader(newStream);
            XmlDocument doc = new XmlDocument();
            doc.Load(reader);
            XmlNamespaceManager nsMan = new XmlNamespaceManager(doc.NameTable);
            nsMan.AddNamespace("soap",
                "http://schemas.xmlsoap.org/soap/envelope/");

            foreach (string xPathQuery in soapElement)
            {
                XmlNodeList nodesToEncrypt =
                    doc.SelectNodes(xPathQuery, nsMan);
                foreach (XmlNode nodeToEncrypt in nodesToEncrypt)
                {
                    nodeToEncrypt.InnerXml =
                        EncryptString(nodeToEncrypt.InnerXml);
                }
            }
            newStream.Position = 0;
            XmlTextWriter writer = new XmlTextWriter(newStream, Encoding.UTF8);
            doc.Save(writer);

            newStream.Position = 0;
            CopyStream(newStream, oldStream);
        }


        public void DecryptXML()
        {
            computeKeyAndIV();

            MemoryStream tempStream = new MemoryStream();
            CopyStream(oldStream, tempStream);
            tempStream.Position = 0;
            XmlTextReader reader = new XmlTextReader(tempStream);
            XmlDocument doc = new XmlDocument();
            doc.Load(reader);
            XmlNamespaceManager nsMan = new XmlNamespaceManager(doc.NameTable);
            nsMan.AddNamespace("soap",
                "http://schemas.xmlsoap.org/soap/envelope/");

            foreach (string xPathQuery in soapElement)
            {
                XmlNodeList nodesToEncrypt =
                    doc.SelectNodes(xPathQuery, nsMan);
                foreach (XmlNode nodeToEncrypt in nodesToEncrypt)
                {
                    nodeToEncrypt.InnerXml =
                        DecryptString(nodeToEncrypt.InnerXml);
                }
            }
            tempStream = new MemoryStream();
            XmlTextWriter writer =
                new XmlTextWriter(tempStream, Encoding.UTF8);
            doc.Save(writer);
            tempStream.Position = 0;
            CopyStream(tempStream, newStream);
            newStream.Position = 0;
        }


        private string EncryptString(string _string)
        {
            MemoryStream encryptedStream = new MemoryStream();

            TripleDESCryptoServiceProvider encryptor = new TripleDESCryptoServiceProvider();
            CryptoStream encryptionStream = new CryptoStream(encryptedStream, encryptor.CreateEncryptor(key, iv), CryptoStreamMode.Write);

            byte[] sourceBytes = Encoding.UTF8.GetBytes(_string);
            encryptionStream.Write(sourceBytes, 0, sourceBytes.Length);
            encryptionStream.FlushFinalBlock();

            XmlSerializer serializer = new XmlSerializer(typeof(byte[]));
            StringBuilder sb = new StringBuilder();
            TextWriter writer = new StringWriter(sb);
            serializer.Serialize(writer, encryptedStream.ToArray());
            writer.Flush();

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(sb.ToString());
            return doc.DocumentElement.InnerXml;
        }

        private string DecryptString(string _string)
        {
            _string =
                "&lt;?xml version=\"1.0\" encoding=\"utf-16\"?&gt;&lt;base64Binary&gt;" +
                _string + "&lt;/base64Binary&gt;";
            XmlSerializer deSerializer = new XmlSerializer(typeof(byte[]));
            TextReader reader = new StringReader(_string);
            byte[] sourceBytes = deSerializer.Deserialize(reader) as byte[];

            MemoryStream tempStream = new MemoryStream();

            TripleDESCryptoServiceProvider decryptor = new TripleDESCryptoServiceProvider();
            CryptoStream decryptionStream = new CryptoStream(tempStream, decryptor.CreateDecryptor(key, iv), CryptoStreamMode.Write);

            decryptionStream.Write(sourceBytes, 0, sourceBytes.Length);
            decryptionStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(tempStream.ToArray());
        }
</code></pre>
