P
pseudonym
To keep this short, assume the function working_test() works perfectly
(because it does), while
failing_test() is not. My GetCodec function returns a Stream (CryptoStream)
object wrapped around the Stream it is passed.
private function working_test()
{
string f = "c:\\test.dat";
StreamWriter w;
StreamReader r;
w = new StreamWriter(GetCodec(File.Create(f), CryptoStreamMode.Write));
w.WriteLine("shhh.. it's a secret");
w.Close();
r = new StreamReader(GetCodec(File.OpenRead(f), CryptoStreamMode.Read));
MessageBox.Show(r.ReadLine());
r.Close();
}
private function failing_test()
{
string f = "c:\\test.dat";
Stream s;
StreamWriter w;
StreamReader r;
s = File.Create(f);
w = new StreamWriter(stream);
w.WriteLine("MyFileTag");
w.Flush(); // **** see extra note about this ****
this.dataset.WriteXml(GetCodec(s, CryptoStreamMode.Write),
XmlWriteMode.WriteSchema));
s.Close();
s = File.OpenRead(f);
r = new StreamReader(s);
if("MyFileTag" != r.ReadLine())
{
throw new System.IO.IOException("not my file!");
}
this.dataset.Clear();
this.dataset.ReadXml(GetCodec(s, CryptoStreamMode.Read),
XmlReadMode.Auto));
s.Close();
}
In the second test, writing the file succeeds. When reading, however, the
call to this.dataset.ReadXml
throws the following exception:
System.Security.Cryptography.CryptographicException: Length of the data to
decrypt is invalid.
at
System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[]
inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at System.Security.Cryptography.CryptoStream.Close()
at failing_test()
Opening c:\test.dat in WordPad shows the file as you'd expect:
MyFileTag
[ a large block of encrypted xml data]
Note: Before I added the explict call to w.Flush(), the file was written out
backwards like this:
[ a large block of encrypted xml data]
MyFileTag
Odd that. Anyway if anyone can tell me what I need to do to get my xml to
read properly, I'd appreciate it!
Nick
(because it does), while
failing_test() is not. My GetCodec function returns a Stream (CryptoStream)
object wrapped around the Stream it is passed.
private function working_test()
{
string f = "c:\\test.dat";
StreamWriter w;
StreamReader r;
w = new StreamWriter(GetCodec(File.Create(f), CryptoStreamMode.Write));
w.WriteLine("shhh.. it's a secret");
w.Close();
r = new StreamReader(GetCodec(File.OpenRead(f), CryptoStreamMode.Read));
MessageBox.Show(r.ReadLine());
r.Close();
}
private function failing_test()
{
string f = "c:\\test.dat";
Stream s;
StreamWriter w;
StreamReader r;
s = File.Create(f);
w = new StreamWriter(stream);
w.WriteLine("MyFileTag");
w.Flush(); // **** see extra note about this ****
this.dataset.WriteXml(GetCodec(s, CryptoStreamMode.Write),
XmlWriteMode.WriteSchema));
s.Close();
s = File.OpenRead(f);
r = new StreamReader(s);
if("MyFileTag" != r.ReadLine())
{
throw new System.IO.IOException("not my file!");
}
this.dataset.Clear();
this.dataset.ReadXml(GetCodec(s, CryptoStreamMode.Read),
XmlReadMode.Auto));
s.Close();
}
In the second test, writing the file succeeds. When reading, however, the
call to this.dataset.ReadXml
throws the following exception:
System.Security.Cryptography.CryptographicException: Length of the data to
decrypt is invalid.
at
System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[]
inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at System.Security.Cryptography.CryptoStream.Close()
at failing_test()
Opening c:\test.dat in WordPad shows the file as you'd expect:
MyFileTag
[ a large block of encrypted xml data]
Note: Before I added the explict call to w.Flush(), the file was written out
backwards like this:
[ a large block of encrypted xml data]
MyFileTag
Odd that. Anyway if anyone can tell me what I need to do to get my xml to
read properly, I'd appreciate it!
Nick