将加密的XML文件加载到数据集中

时间:2009-07-20 20:21:32

标签: .net encryption dataset stream

我正在尝试解密加密的XML文件并将其放入流中,然后将其加载到数据集中。如果我解密文件并将其作为文件写回来,我就能做到这一点。然后执行Dataset.ReadXML方法。但是,所以我没有打破加密的目的,我想把它留在内存中。我看到.ReadXML接受一个system.io.stream作为参数,但我不确定从解密方法构造它的最佳方法。

以下是将xml文件加载到数据集中的代码

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

以下是解密文件的代码:

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 个答案:

答案 0 :(得分:1)

只需使用CryptoStream加载DataSet。