By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

"Stream does not support seeking" on CryptoStream

P: n/a
Hi,
I am using cryptostream on both sides on tcp connection that pass data.
I am also use asyc socket , so , the data that recieved in the callback
method not always have the length of the buffer I sent.
for ex
I want to send 10000 bytes
I can get it in 10 times of 1000 bytes each.
so , I need to know when I complete the receiving , I want to write inside
cryptostream and check the position compare it to the length I already know
I should get , but the cryptostream throw exception of "Stream does not
support seeking."

some solution (inside the cryptostream) ?
thanks

Sep 14 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Well, it is probably the TcpStream that doesn't support seeking, as
CryptoStream just sits on top of this...

If you already know the length, why not jut keep track of the number of
bytes received? (returned on each call to Read())? Alternatively, just keep
Read()ing until Read() doesn't return a +ve number...

Marc
Sep 14 '06 #2

P: n/a
Solve it before decrypt. I assume you have some message marker (such as
prepended len). So get (async) the whole message and decrypt that block.
So you will encrypt/decrypt in full blocks (i.e. messages). For large
streams (such as files), you still work in blocks. You pick the block size
and send in block size chunks.

--
William Stacey [MVP]

"semedao" <se*****@community.nospamwrote in message
news:eW*************@TK2MSFTNGP05.phx.gbl...
| Hi,
| I am using cryptostream on both sides on tcp connection that pass data.
| I am also use asyc socket , so , the data that recieved in the callback
| method not always have the length of the buffer I sent.
| for ex
| I want to send 10000 bytes
| I can get it in 10 times of 1000 bytes each.
| so , I need to know when I complete the receiving , I want to write inside
| cryptostream and check the position compare it to the length I already
know
| I should get , but the cryptostream throw exception of "Stream does not
| support seeking."
|
| some solution (inside the cryptostream) ?
| thanks
|
|
|
Sep 14 '06 #3

P: n/a
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.

Sep 15 '06 #4

P: n/a
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.
|
Sep 15 '06 #5

P: n/a
Thanks for your reply and the suggestion William,

As for the CryptoStream, I think the reason why it dosn't provide a
internal buffer itself(to expose a byte arrary) is that CryptoStream is not
an real stream which will hold underlying data(like FileStream,
NetworkStream or MemoryStream... ). CryptoStream is more like a
combination of StreamReader and StreamWriter whch rely on another
underlying stream object. Do you think so?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 15 '06 #6

P: n/a
Hi Semedao,

Have you got any further progress or ideas on this issue or does our
suggestion helps you some?

Please feel free to post here if there is still anything we can help.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 19 '06 #7

P: n/a
I solve it by.... change my code to first get all buffer of the cryptostream
....
thanks
"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:lJ*************@TK2MSFTNGXA01.phx.gbl...
Hi Semedao,

Have you got any further progress or ideas on this issue or does our
suggestion helps you some?

Please feel free to post here if there is still anything we can help.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no
rights.

Sep 21 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.