473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

"Stream does not support seeking" on CryptoStream

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
7 13321
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steven T. Hatton | last post by:
I've seen people here write that C++ doesn't support modules. What does that mean? 'Module' is a very nebulous term in my book. It probably means something quite different to me than what it does...
0
by: Klaus Alexander Seistrup | last post by:
Hi, I need a Python NNTP module that is capable of doing "MODE STREAM" as client and as server. Does anyone here know of such a module except the twisted.protocols.nntp module? Cheers, --...
2
by: Claus - Arcolutions | last post by:
I got a word document as a stream, and I want to get the text from the word document. But I cant seem to find anything to use for that purpose. The "Microsoft office ?.object" com reference, only...
2
by: CVerma | last post by:
I'm using an html input control (System.web.UI.HTMLControls.HTMLInputFile) to upload files such as msword, excel, jpg, and pdf. I have the encType property set in the form:...
6
by: Tom Kaminski [MVP] | last post by:
I can do this in ASP, but not sure how to handle in ASP.NET: How To Use the ADODB.Stream Object to Send Binary Files to the Browser through ASP http://support.microsoft.com/?kbid=276488 Do I...
4
by: John Friedland | last post by:
'printf' has a '%a' conversion for floating-point output: For example, printing '123456' with "|%13.4a|" produces | 0x1.e240p+16| I've looked through Josuttis and the header files, but I...
3
by: Nick Gilbert | last post by:
Hi, In my VS.NET 2005, if I choose Build Clean Solution, the BIN folder is not touched. Shouldn't it delete all the dll and pdb files in that folder first? Instead, I'm finding I have to do it...
5
by: chandanlinster | last post by:
When I was reading the man-page of fflush, I came across this statement --- "The function fflush forces a write of all user-space buffered data for the given output or update stream via the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.