473,385 Members | 1,396 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,385 software developers and data experts.

decrypting from memory stream

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

Similar topics

0
by: luca minelli via .NET 247 | last post by:
Hi, reading the documentation I wrote few lines in order to try to decrypt something. Unfortunately I always get back an exception : "System.Security.Cryptography.CryptographicException' occurred...
16
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
5
by: Tomaz Koritnik | last post by:
Hi I have many short HTML files stored in a binary stream storage to display descriptions for various items in application. HTML would be display inside application using some .NET control or...
5
by: Joe | last post by:
I'm trying to figure out how to remove the characters padded to the end of my string without setting the Padding = PaddingMode.None. My original string passed in is 'passwordTest' and the...
2
by: Steve Long | last post by:
Hello, I'm wondering if somebody can help me figure out how to decrypt data. I seem to be able to encrypt data but have not been able to decrypt the very data that I've encrypted. I know it's...
2
by: PaulN | last post by:
I'm working on an inherited app that decrypts an XML file ftp'd from a web site and then fills a dataset with it and then loads it into a database. The app as written decrypts the xml into a...
4
by: Fritjolf | last post by:
Hi. I've got a strange problem... I've made a simple program to test encryption/decryption. I use Rijndael encryption and here are the most important properties. RijndaelManaged cipher =...
0
by: phreak008 | last post by:
I'm using SendMessage(...) to send a message to all other process that might run. It works well. My problem is when I try to pass data using shared memory. Here is my code. In the 1st process, I...
3
by: CSharper | last post by:
I have a resource file that i open to write.After writing almost 300MB I call the close method and It failed with out of memory. One thing I learned that, in C#, resource writers doesn't write data...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.