XML en-/Decryption problem

E

EMW

Hi,

I use the following code:

Function CryptoWriter(ByVal file As String) As CryptoStream
Dim FileWriter As FileStream = New FileStream(file, FileMode.Create)
Dim CryptoProvider As RijndaelManaged = New RijndaelManaged
Dim key As Byte() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
Dim iv As Byte() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
Return New CryptoStream(FileWriter, CryptoProvider.CreateEncryptor(key, iv),
CryptoStreamMode.Write)
End Function

Function CryptoReader(ByVal file As String) As CryptoStream
Dim FileReader As FileStream = New FileStream(file, FileMode.Open)
Dim CryptoProvider As RijndaelManaged = New RijndaelManaged
Dim key As Byte() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
Dim iv As Byte() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
Return New CryptoStream(FileReader, CryptoProvider.CreateDecryptor(key, iv),
CryptoStreamMode.Read)
End Function

In the sub where I call these function I use:
ds.writexml(Cryptowriter(filename))
to write the tables from the dataset to disk.
I don't get any errors and a nicely unreadable file.

When I use:
ds.readxml(cryptoreader(filename))
to read the xml back and in the dataset I get the following error:

System.Security.Cryptography.CryptographicException: PKCS7....

Can someone help me with this?
All I want to do is to read the encrypted xml file in to a dataset, do
something with it, then encrypt it again and write it to disk.

thanks,
Eric
 
J

Jon Skeet [C# MVP]

In the sub where I call these function I use:
ds.writexml(Cryptowriter(filename))
to write the tables from the dataset to disk.
I don't get any errors and a nicely unreadable file.

When I use:
ds.readxml(cryptoreader(filename))
to read the xml back and in the dataset I get the following error:

System.Security.Cryptography.CryptographicException: PKCS7....

Can someone help me with this?
All I want to do is to read the encrypted xml file in to a dataset, do
something with it, then encrypt it again and write it to disk.

You haven't shown the streams being closed - and unless they're closed,
the final block might not be getting flushed, which would lead to an
invalid decryption stream.
 
J

Jon Skeet [C# MVP]

EMW said:
How dso I close them, since I pass the stream on from a function?

The answer is not to pass the stream immediately. Instead, do:

Stream stream = CryptoWriter(filename);
ds.WriteXml(stream);
stream.Close();

(You should put it in a suitable try/finally block so that the stream
gets closed even if an exception is thrown.)
 
E

EMW

ok, thanks!


Jon Skeet said:
The answer is not to pass the stream immediately. Instead, do:

Stream stream = CryptoWriter(filename);
ds.WriteXml(stream);
stream.Close();

(You should put it in a suitable try/finally block so that the stream
gets closed even if an exception is thrown.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top