This raises a wish I have. The CryptoStream should also take a byte[] (not
just MS). That way, you can remove the overhead of creating a MemStream if
you don't need it.
--
William Stacey [MVP]
"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:kn**************@TK2MSFTNGXA01.phx.gbl...
| Hello Semedao,
|
| The "Stream does not support seeking."exception you encountered is due to
| the CryptoStream class which does not support random stream postion seek.
| So accessing the Postion property will raise the exception(and CanSeek
| property of CryptoStream class always return false). So this is not
| specific to the underlying TcpStream.
|
| For your scenario, the data is received from Tcp network asynchornously, I
| think you can consider William's suggestion on solve all the data in a
| buffer/stream before you use CryptoStream to decrypt the data. For
| example, you can use the MemoryStream class(as buffer) to hold those data
| received from TcpStream before you've received all the data. And
| MemoryStream class support random postion access and seeking.
|
| After you've received all the data, you can create the CryptoStream
against
| the memorystream instance and decrypt the full data blocks in the
| memorystream. Do you think it a possible approach for your case?
|
| Here is a test code snippet demonstrate using MemoryStream to store the
| network data and decrypt data from it later(through CryptoStream):
|
|
|
| ===============================
| static void ReadData(NetworkStream stream)
| {
| Console.WriteLine("ReadData....................... ");
|
|
| RijndaelManaged RMCrypto = new RijndaelManaged();
| RMCrypto.Padding = PaddingMode.PKCS7;
|
| byte[] Key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
| 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
| byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
| 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
|
|
| byte[] buf = new byte[128];
|
| MemoryStream bufstream = new MemoryStream();
|
|
|
| int iRead = 0;
| while ((iRead = stream.Read(buf, 0, buf.Length)) 0)
| {
| Console.WriteLine("buffer Position: " +
| bufstream.Position);
|
| bufstream.Write(buf, 0, iRead);
|
| if (iRead < buf.Length)
| break;
| }
|
|
|
| bufstream.Position = 0;
|
|
| CryptoStream CryptStream = new CryptoStream(bufstream,
| RMCrypto.CreateDecryptor(Key, IV),
| CryptoStreamMode.Read);
|
|
| StreamReader reader = new StreamReader(CryptStream,
| Encoding.UTF8);
|
| string str = reader.ReadToEnd();
|
| Console.WriteLine("Data: " + str);
|
|
|
| reader.Close();
| CryptStream.Close();
| bufstream.Close();
|
| }
| ==================================
|
| If you feel necessary, I can send you the complete test solution with a
| client and server console app.
|
| Hope this helps.
|
| Sincerely,
|
| Steven Cheng
|
| Microsoft MSDN Online Support Lead
|
|
|
| ==================================================
|
| Get notification to my posts through email? Please refer to
|
http://msdn.microsoft.com/subscripti...ult.aspx#notif
| ications.
|
|
|
| Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
| where an initial response from the community or a Microsoft Support
| Engineer within 1 business day is acceptable. Please note that each follow
| up response may take approximately 2 business days as the support
| professional working with you may need further investigation to reach the
| most efficient resolution. The offering is not appropriate for situations
| that require urgent, real-time or phone-based interactions or complex
| project analysis and dump analysis issues. Issues of this nature are best
| handled working with a dedicated Microsoft Support Engineer by contacting
| Microsoft Customer Support Services (CSS) at
|
http://msdn.microsoft.com/subscripti...t/default.aspx.
|
| ==================================================
|
|
|
| This posting is provided "AS IS" with no warranties, and confers no
rights.
|