Groups | Blog | Home
all groups > dotnet general > june 2004 >

dotnet general : XML en-/Decryption problem


Jon Skeet [C# MVP]
6/17/2004 10:21:50 PM
[quoted text, click to view]

<snip>

[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
EMW
6/17/2004 10:57:23 PM
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


EMW
6/17/2004 11:24:17 PM
How dso I close them, since I pass the stream on from a function?

rg,
Eric

"Jon Skeet [C# MVP]" <skeet@pobox.com> schreef in bericht
news:MPG.1b3c1bc29169584998acd5@msnews.microsoft.com...
[quoted text, click to view]

Jon Skeet [C# MVP]
6/18/2004 7:48:53 AM
[quoted text, click to view]

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.)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
EMW
6/18/2004 7:01:15 PM
ok, thanks!


"Jon Skeet [C# MVP]" <skeet@pobox.com> schreef in bericht
news:MPG.1b3ca0b39913359598acd7@msnews.microsoft.com...
[quoted text, click to view]

AddThis Social Bookmark Button