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

decrypting from memory stream

P: n/a
JW

I can encrypt the contents of a memory stream with no problems, however when
decrypting( I'm using the same key an IV ) I get an exception stating bad
data.

I'm at a lost. I'm actually creating a new memory stream, filling it with
the encrypted bytes then trying to read from the crypto stream object. I
have used the same approach encrypting to a file handle and then decrypting
from the same file and it works.

sample: (this same code works with a file stream but not memory stream) what
am I missing?

MemoryStream memStrEnc2 = new MemoryStream(newByte, 0, newByte.Length);

CryptoStream decStream = new CryptoStream(memStrEnc2,
tdes.CreateDecryptor(random, random), CryptoStreamMode.Read);

byte[] memString2 = new Byte[memStrEnc2.Length];

decStream.Read(memString2, 0, 16);

decStream.Close();

Thank you for your time

Josiah

--
Josiah Woodhouse
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi JW,

The common cause to your problem is that you probably don't call
FlushFinalBlock() method before closing CryptoStream when writting data.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

"JW" <jo*************@hotmail.com> wrote in message
news:Ol****************@TK2MSFTNGP09.phx.gbl...

I can encrypt the contents of a memory stream with no problems, however when decrypting( I'm using the same key an IV ) I get an exception stating bad
data.

I'm at a lost. I'm actually creating a new memory stream, filling it with
the encrypted bytes then trying to read from the crypto stream object. I
have used the same approach encrypting to a file handle and then decrypting from the same file and it works.

sample: (this same code works with a file stream but not memory stream) what am I missing?

MemoryStream memStrEnc2 = new MemoryStream(newByte, 0, newByte.Length);

CryptoStream decStream = new CryptoStream(memStrEnc2,
tdes.CreateDecryptor(random, random), CryptoStreamMode.Read);

byte[] memString2 = new Byte[memStrEnc2.Length];

decStream.Read(memString2, 0, 16);

decStream.Close();

Thank you for your time

Josiah

--
Josiah Woodhouse

Nov 15 '05 #2

P: n/a
<"Miha Markic" <miha at rthand com>> wrote:
The common cause to your problem is that you probably don't call
FlushFinalBlock() method before closing CryptoStream when writting data.


That shouldn't make any difference, as I believe CryptoStream.Close
flushes the final block anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

P: n/a
JW <jo*************@hotmail.com> wrote:
sample: (this same code works with a file stream but not memory stream) what
am I missing?

MemoryStream memStrEnc2 = new MemoryStream(newByte, 0, newByte.Length);

CryptoStream decStream = new CryptoStream(memStrEnc2,
tdes.CreateDecryptor(random, random), CryptoStreamMode.Read);

byte[] memString2 = new Byte[memStrEnc2.Length];

decStream.Read(memString2, 0, 16);

decStream.Close();


You're still using Stream.Read the wrong way, as you were before. See
http://www.pobox.com/~skeet/csharp/readbinary.html

That shouldn't give that kind of exception though. Again, could you
come up with a short but complete program which demonstrates the
problem?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.