PC Review


Reply
Thread Tools Rate Thread

Cryptostream truncation

 
 
Matt
Guest
Posts: n/a
 
      5th Aug 2003
Has anyone else out there noticed that cryptostreams get truncated if
you follow the msdn method for encrypting and decrypting files/memory
streams? If I replace the crypto stream with a file stream in my code
everything works great, as soon as I put the crypto stream back in, it
gets truncated at some point. Does anyone know why or how to fix it?

Here is the code I'm using....

DataRow dr = dsBurgers.Tables["tblBurgers"].NewRow();
dr["burgerType"] = "cheeseBurgerffdfd";
dr["burgerPrice"] = 1.25;
dsBurgers.Tables["tblBurgers"].Rows.Add(dr);

//create key and initialization vector
byte[] rijnKey = {bla bla};
byte[] rijnIV = {bla bla};

dsBurgers.WriteXml("XmlDirect.txt");

//encode to file
FileStream fin = new
FileStream("XmlDirect.txt",FileMode.Open,FileAccess.Read);
FileStream fout = new
FileStream("XmlEnc.txt",FileMode.OpenOrCreate,FileAccess.Write);
fout.SetLength(0);
TripleDESCryptoServiceProvider rijn = new
TripleDESCryptoServiceProvider();
CryptoStream encStream = new
CryptoStream(fout,rijn.CreateEncryptor(rijnKey,rijnIV),CryptoStreamMode.Write);
long fileLength = fin.Length;
byte[] buffer = new Byte[1];
int amountRead = 0;
long amountDone = 0;
while(amountDone<fileLength)
{
amountRead = fin.Read(buffer,0,1);
encStream.Write(buffer,0,amountRead);
amountDone = amountDone + amountRead;
}
//fin.Read(buffer,0,Convert.ToInt32(fileLength));
//encStream.Write(buffer,0,Convert.ToInt32(fileLength));
fout.Close();
fin.Close();


//decrypt the file and make sure everything is still there
FileStream fin2 = new
FileStream("XmlEnc.txt",FileMode.Open,FileAccess.Read);
FileStream fout2 = new
FileStream("XmlOutEncDec.txt",FileMode.OpenOrCreate,FileAccess.Write);
fout2.SetLength(0);
CryptoStream decStream = new
CryptoStream(fout2,rijn.CreateDecryptor(rijnKey,rijnIV),CryptoStreamMode.Write);
long fileLen = fin2.Length;
amountRead = 0;
amountDone = 0;
while(amountDone<fileLen)
{
amountRead = fin2.Read(buffer,0,1);
decStream.Write(buffer,0,amountRead);
amountDone += amountRead;
}

//fin2.Read(buffer,0,Convert.ToInt32(fileLen));
//decStream.Write(buffer,0,Convert.ToInt32(fileLen));

fin2.Close();
fout2.Close();

___________________________________
changing encStream or decStream to fout and fout2 fixes everything,
which makes me think it could be a problem with the cryptostreams
themselves or there is something I don't know about their behavior
that would cause the truncation.

If anyone knows the answer please help.
Thanks and code on!
Matt
 
Reply With Quote
 
 
 
 
Bill
Guest
Posts: n/a
 
      5th Aug 2003
I think you may need to add a encStream.Flush() before you close your
underlying stream. This will clear any internal buffers the Crypto object
may be using.

Bill.


"Matt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Has anyone else out there noticed that cryptostreams get truncated if
> you follow the msdn method for encrypting and decrypting files/memory
> streams? If I replace the crypto stream with a file stream in my code
> everything works great, as soon as I put the crypto stream back in, it
> gets truncated at some point. Does anyone know why or how to fix it?
>
> Here is the code I'm using....
>
> DataRow dr = dsBurgers.Tables["tblBurgers"].NewRow();
> dr["burgerType"] = "cheeseBurgerffdfd";
> dr["burgerPrice"] = 1.25;
> dsBurgers.Tables["tblBurgers"].Rows.Add(dr);
>
> //create key and initialization vector
> byte[] rijnKey = {bla bla};
> byte[] rijnIV = {bla bla};
>
> dsBurgers.WriteXml("XmlDirect.txt");
>
> //encode to file
> FileStream fin = new
> FileStream("XmlDirect.txt",FileMode.Open,FileAccess.Read);
> FileStream fout = new
> FileStream("XmlEnc.txt",FileMode.OpenOrCreate,FileAccess.Write);
> fout.SetLength(0);
> TripleDESCryptoServiceProvider rijn = new
> TripleDESCryptoServiceProvider();
> CryptoStream encStream = new
>

CryptoStream(fout,rijn.CreateEncryptor(rijnKey,rijnIV),CryptoStreamMode.Writ
e);
> long fileLength = fin.Length;
> byte[] buffer = new Byte[1];
> int amountRead = 0;
> long amountDone = 0;
> while(amountDone<fileLength)
> {
> amountRead = fin.Read(buffer,0,1);
> encStream.Write(buffer,0,amountRead);
> amountDone = amountDone + amountRead;
> }
> //fin.Read(buffer,0,Convert.ToInt32(fileLength));
> //encStream.Write(buffer,0,Convert.ToInt32(fileLength));
> fout.Close();
> fin.Close();
>
>
> //decrypt the file and make sure everything is still there
> FileStream fin2 = new
> FileStream("XmlEnc.txt",FileMode.Open,FileAccess.Read);
> FileStream fout2 = new
> FileStream("XmlOutEncDec.txt",FileMode.OpenOrCreate,FileAccess.Write);
> fout2.SetLength(0);
> CryptoStream decStream = new
>

CryptoStream(fout2,rijn.CreateDecryptor(rijnKey,rijnIV),CryptoStreamMode.Wri
te);
> long fileLen = fin2.Length;
> amountRead = 0;
> amountDone = 0;
> while(amountDone<fileLen)
> {
> amountRead = fin2.Read(buffer,0,1);
> decStream.Write(buffer,0,amountRead);
> amountDone += amountRead;
> }
>
> //fin2.Read(buffer,0,Convert.ToInt32(fileLen));
> //decStream.Write(buffer,0,Convert.ToInt32(fileLen));
>
> fin2.Close();
> fout2.Close();
>
> ___________________________________
> changing encStream or decStream to fout and fout2 fixes everything,
> which makes me think it could be a problem with the cryptostreams
> themselves or there is something I don't know about their behavior
> that would cause the truncation.
>
> If anyone knows the answer please help.
> Thanks and code on!
> Matt



 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      5th Aug 2003
Bill,

If you are out there, thankyou so much for your response. I was
banging my head on the wall for 48 hours going over and over that.
That seems to have fixed the problem.

Code on,
Matt
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using CryptoStream jp2msft Microsoft C# .NET 14 7th Dec 2008 01:43 AM
Problems with CryptoStream karthy84@gmail.com Microsoft C# .NET 2 15th Nov 2007 06:52 PM
CryptoStream =?Utf-8?B?RnJlZCBIZXJyaW5n?= Microsoft Dot NET Framework Forms 1 6th Jan 2005 08:11 PM
CryptoStream question Fede Microsoft C# .NET 2 8th Feb 2004 05:19 AM
CryptoStream? weixiang Microsoft C# .NET 5 25th Jul 2003 12:26 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:36 AM.