M
Matt
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
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