473,753 Members | 7,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HOW? Capture output stream as byte array

I am using Cryptography. You can encrypt or decrypt by providing an
output stream as a parameter to the CryptoStream constructor. But I
need byte arrays, as I am encrypting on the fly to a socket, and need
to manage all socket traffic.

My thought was a stateful call to my own Encrypt and Decrypt routines.
That I would pass a MemoryStream to the CryptoStream constructor. But
it appears that for each write CryptoStream does to my memory stream,
there is no way for me to get the bytes out of the MemoryStream and
and reset the MemoryStream content, so that its byte content does not
continue to grow. The files being encrypted, for example, might be as
large as 100MB. I could encrypt entirely to YATDF (yet another
temporary disk file), and the pass that to the socket. But if there
is a way, I would like to avoid a disk file.

Maybe MemoryStream is NOT the right object, because I don't see how to
do the above. Is there some other stream object, or perhaps, a way to
do what I want with MemoryStream?

TIA - best regards, Lee Gillie
Nov 20 '05 #1
5 8056
Lee Gillie <AN************ *******@odp.com > wrote:
I am using Cryptography. You can encrypt or decrypt by providing an
output stream as a parameter to the CryptoStream constructor. But I
need byte arrays, as I am encrypting on the fly to a socket, and need
to manage all socket traffic.

My thought was a stateful call to my own Encrypt and Decrypt routines.
That I would pass a MemoryStream to the CryptoStream constructor. But
it appears that for each write CryptoStream does to my memory stream,
there is no way for me to get the bytes out of the MemoryStream and
and reset the MemoryStream content, so that its byte content does not
continue to grow. The files being encrypted, for example, might be as
large as 100MB. I could encrypt entirely to YATDF (yet another
temporary disk file), and the pass that to the socket. But if there
is a way, I would like to avoid a disk file.

Maybe MemoryStream is NOT the right object, because I don't see how to
do the above. Is there some other stream object, or perhaps, a way to
do what I want with MemoryStream?


You could always just write your own Stream implementation - that
wouldn't be particularly hard, especially if you're just handing off
whatever is written.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 20 '05 #2
Yes, that's what I did. I did not realize it was so trivial to derive
from Stream. The one awkward part was that my implementation of
Write() was called one additional time when the cryptography stream
was closed. But it is coded and tested, and works well.

Thanks - Lee

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Lee Gillie <AN************ *******@odp.com > wrote:
I am using Cryptography. You can encrypt or decrypt by providing an output stream as a parameter to the CryptoStream constructor. But I need byte arrays, as I am encrypting on the fly to a socket, and need to manage all socket traffic.

My thought was a stateful call to my own Encrypt and Decrypt routines. That I would pass a MemoryStream to the CryptoStream constructor. But it appears that for each write CryptoStream does to my memory stream, there is no way for me to get the bytes out of the MemoryStream and and reset the MemoryStream content, so that its byte content does not continue to grow. The files being encrypted, for example, might be as large as 100MB. I could encrypt entirely to YATDF (yet another
temporary disk file), and the pass that to the socket. But if there is a way, I would like to avoid a disk file.

Maybe MemoryStream is NOT the right object, because I don't see how to do the above. Is there some other stream object, or perhaps, a way to do what I want with MemoryStream?


You could always just write your own Stream implementation - that
wouldn't be particularly hard, especially if you're just handing off
whatever is written.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 20 '05 #3
Lee,
In addition to the other comments.

If you are writing to a socket, why not just pass the NetworkStream itself
to the CryptoStream class?

You can use MemoryStream.Ge tBuffer & MemoryStream.To Array to get the bytes
out.

I believe you can use MemoryStream.Se tLength to reset the memory stream.
(based on reading the help, I have not tried it). However I would simply use
a new MemoryStream object each time I needed one.

I'm not so certain you need a MemoryStream, as I would simply pass the
NetworkStream itself to the CryptoStream.

Hope this helps
Jay

"Lee Gillie" <AN************ *******@odp.com > wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
I am using Cryptography. You can encrypt or decrypt by providing an
output stream as a parameter to the CryptoStream constructor. But I
need byte arrays, as I am encrypting on the fly to a socket, and need
to manage all socket traffic.

My thought was a stateful call to my own Encrypt and Decrypt routines.
That I would pass a MemoryStream to the CryptoStream constructor. But
it appears that for each write CryptoStream does to my memory stream,
there is no way for me to get the bytes out of the MemoryStream and
and reset the MemoryStream content, so that its byte content does not
continue to grow. The files being encrypted, for example, might be as
large as 100MB. I could encrypt entirely to YATDF (yet another
temporary disk file), and the pass that to the socket. But if there
is a way, I would like to avoid a disk file.

Maybe MemoryStream is NOT the right object, because I don't see how to
do the above. Is there some other stream object, or perhaps, a way to
do what I want with MemoryStream?

TIA - best regards, Lee Gillie

Nov 20 '05 #4
I was going to say that MemoryStream.Ge tArray() will return the bytes, but
Jay beat me to it.
An alternate option is to just pass a NetworkStream to the CryptoStream
constructor instead of a memory stream. That way, the crypto stream writes
directly to the socket. If you are using the TCPClient, you can get the
network stream from the GetStream method.

-Rob Teixeira [MVP]

"Lee Gillie" <AN************ *******@odp.com > wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
I am using Cryptography. You can encrypt or decrypt by providing an
output stream as a parameter to the CryptoStream constructor. But I
need byte arrays, as I am encrypting on the fly to a socket, and need
to manage all socket traffic.

My thought was a stateful call to my own Encrypt and Decrypt routines.
That I would pass a MemoryStream to the CryptoStream constructor. But
it appears that for each write CryptoStream does to my memory stream,
there is no way for me to get the bytes out of the MemoryStream and
and reset the MemoryStream content, so that its byte content does not
continue to grow. The files being encrypted, for example, might be as
large as 100MB. I could encrypt entirely to YATDF (yet another
temporary disk file), and the pass that to the socket. But if there
is a way, I would like to avoid a disk file.

Maybe MemoryStream is NOT the right object, because I don't see how to
do the above. Is there some other stream object, or perhaps, a way to
do what I want with MemoryStream?

TIA - best regards, Lee Gillie

Nov 20 '05 #5
Lee Gillie wrote:
Yes, that's what I did. I did not realize it was so trivial to derive
from Stream. The one awkward part was that my implementation of
Write() was called one additional time when the cryptography stream
was closed. But it is coded and tested, and works well.


That's because the crypto routines are block routines and hence they are
flushed after they are finished.

Richard
--
my email ev******@zicf.b et is encrypted with ROT13 (www.rot13.org)
sign up for my free .NET newsletter at
http://www.wd-mag.com/newsletters/
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1889
by: Stu | last post by:
Hi, Im reading a file in from disk as a byte array then passing it to a memory stream for decryption using crypto api functions. What I have found is that you need to reduce the array length by 2 from the original lenght in order to get it to work as there seems to be 2 extra 0 bytes at the end. Functions included Stu
2
3712
by: Nick | last post by:
Hi, Problem statement: Calling a java method from Dot Net code that returns a java byte array. Overview: I have a java server from which I need to establish connection. I hva ebeen able to send across data to the server in a serialised stream. However, when I need to geta response back to the client (.Net), I get a binary stream. What I wanna do is to convert this stream into an SByte array (The java byte is actually a signed byte,...
2
1415
by: Jeroen Ceuppens | last post by:
byte bytes=new byte; for (int i=0;i<50;i++) { for (int j=0;j<100;j++) bytes=i+j;
6
52266
by: Hardy Wang | last post by:
Hi all: The Stream object from WebRequest.GetResponseStream() is non-seekable. How can I convert this stream to a byte array? For ordinary Stream (seekable), I can use StreamObject.Read(myByteArray, 0, myLength) --
3
5094
by: Ivan Demkovitch | last post by:
Hi! I'm using output stream to output image to webpage. //Move array to response stream. Response.ContentType = "image/jpeg"; Response.OutputStream.Write(arrBt, 0, arrBt.Length); Response.End();
6
4928
by: Alec MacLean | last post by:
Hi, I've created a small application for our company extranet (staff bulletins) that outputs a list of links to PDF's that are stored in a SQL table. The user clicks a link and the PDF is loaded into a new browser window. This works as expected on the test PC (using forms authentication, but no SSL) using IE. It also works as expected on the production server when using FireFox. The production server environment is using forms...
3
5081
by: rlrcstr | last post by:
Is it possible to truncate a MemroyStream to free up the space up to the current position? I wantes to be able to just keep dumping bytes into a stream and then read the stream as needed. After the read, free up the portion of the stream I read, esentially making the first unread byte the first byte in the stream. Thanks. Jerry
1
2600
by: untitled | last post by:
i wrote an application in c++ that capture a motion from a bmp sequence and outputs a 3DS max secript file contains the motions keys. i have two points here need to be fixed: 1- it is an offline application, ie. you take a movie file, convert it to a sequence of 16color BMPs with adobe premiere for example. to imporve, we should let it read the video stream directly from a webcam for example and detect motion directly from it. the...
2
1479
by: Mike P2 | last post by:
I made a Stream-inheriting class that just removes the tabs (actually the 4 spaces VS prefers to use) from the beginning of lines and empty lines. At first I was having trouble with it adding a null character in the middle of the output. I think it was a null character, Firefox displays it as a '?' and the W3C validator says it's a non-sgml character '0'. I changed the code around a bit and now the only problem is that it (on some pages of...
0
9072
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9653
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9451
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9421
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9333
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8328
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6869
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.