views:

254

answers:

1

I'm trying to decrypt an encrypted XML file and put it into a stream then load it into a dataset. I am able to do this if I decrypt the file, and write it back as a file. Then do the Dataset.ReadXML method. However, so I don't defeat the purpose of the encryption I'd like to leave it in memory. I see that .ReadXML does accept a system.io.stream as a parameter but I'm just not sure the best way to construct it from the decryption method.

Here is the code to load the xml file into the dataset

        'ds is the dataset
        ds.ReadXmlSchema(m_TheSchemaPath)
        ds.ReadXml(m_TheXMLDatasetPath, XmlReadMode.IgnoreSchema)

Here is the code to decrypt the file:

Sub DecryptFile(ByVal sInputFilename As String, _
    ByVal sOutputFilename As String, _
    ByVal sKey As String)

    Dim DES As New DESCryptoServiceProvider()
    'A 64-bit key and an IV are required for this provider.
    'Set the secret key for the DES algorithm.
    DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
    'Set the initialization vector.
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

    'Create the file stream to read the encrypted file back.
    Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
    'Create the DES decryptor from the DES instance.
    Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
    'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
    Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
    'Print out the contents of the decrypted file.
    Dim fsDecrypted As New StreamWriter(sOutputFilename)
    fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
    fsDecrypted.Flush()
    fsDecrypted.Close()
End Sub
+1  A: 

Just use the CryptoStream to load the DataSet.

John Saunders
Whoa, that was far too easy. ;-)
Cj Anderson
There's no such thing as "too easy" with programming, as long as it works. "Too easy, and that's why it doesn't work" is something else entirely.
John Saunders