如何:用 X.509 证书对 XML 元素进行解密

更新:2007 年 11 月

可以使用 System.Security.Cryptography.Xml 命名空间中的类对 XML 文档中的元素进行加密和解密。 XML 加密是交换或存储加密的 XML 数据的标准方法,无需担心数据被轻易读取。 有关 XML 加密标准的更多信息,请参见位于 http://www.w3.org/TR/xmldsig-core/ 上的关于 XML 加密的 WWW 联合会 (W3C) 规范。

此示例对用 如何:用 X.509 证书对 XML 元素进行加密 中描述的方法加密的 XML 元素进行解密。 它找到一个 <EncryptedData> 元素,对该元素进行解密,然后用原始纯文本 XML 元素替换该元素。

此过程中的代码示例使用当前用户帐户的本地证书存储区中的 X.509 证书对 XML 元素进行解密。 该示例使用 DecryptDocument 方法自动检索出 X.509 证书,然后对存储在 <EncryptedData> 元素的 <EncryptedKey> 元素中的会话密钥进行解密。 然后 DecryptDocument 方法自动使用该会话密钥对 XML 元素进行解密。

此示例适合于以下情形:多个应用程序需要共享加密数据,或者一个应用程序需要在多次运行之间保存加密数据。

用 X.509 证书对 XML 元素进行解密

  1. 通过从磁盘加载 XML 文件创建 XmlDocument 对象。 XmlDocument 对象包含要解密的 XML 元素。

    Dim xmlDoc As New XmlDocument()
    
    XmlDocument xmlDoc = new XmlDocument();
    
  2. 通过将 XmlDocument 对象传递给构造函数,创建一个新的 EncryptedXml 对象。

    Dim exml As New EncryptedXml(Doc)
    
    EncryptedXml exml = new EncryptedXml(Doc);
    
  3. 使用 DecryptDocument 方法对 XML 文档进行解密。

    exml.DecryptDocument()
    
    exml.DecryptDocument();
    
  4. 保存 XmlDocument 对象。

    xmlDoc.Save("test.xml")
    
    xmlDoc.Save("test.xml");
    

示例

Imports System
Imports System.Xml
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Imports System.Security.Cryptography.X509Certificates



Module Program

    Sub Main(ByVal args() As String)
        Try
            ' Create an XmlDocument object.
            Dim xmlDoc As New XmlDocument()
            ' Load an XML file into the XmlDocument object.
            xmlDoc.PreserveWhitespace = True
            xmlDoc.Load("test.xml")

            ' Decrypt the document.
            Decrypt(xmlDoc)

            ' Save the XML document.
            xmlDoc.Save("test.xml")
            ' Display the decrypted XML to the console.
            Console.WriteLine("Decrypted XML:")
            Console.WriteLine()
            Console.WriteLine(xmlDoc.OuterXml)

        Catch e As Exception
            Console.WriteLine(e.Message)
        End Try

    End Sub


    Sub Decrypt(ByVal Doc As XmlDocument)
        ' Check the arguments.  
        If Doc Is Nothing Then
            Throw New ArgumentNullException("Doc")
        End If

        ' Create a new EncryptedXml object.
        Dim exml As New EncryptedXml(Doc)
        ' Decrypt the XML document.
        exml.DecryptDocument()
    End Sub
End Module
using System;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Security.Cryptography.X509Certificates;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            // Create an XmlDocument object.
            XmlDocument xmlDoc = new XmlDocument();

            // Load an XML file into the XmlDocument object.
            xmlDoc.PreserveWhitespace = true;
            xmlDoc.Load("test.xml");

            // Decrypt the document.
            Decrypt(xmlDoc);

            // Save the XML document.
            xmlDoc.Save("test.xml");

            // Display the decrypted XML to the console.
            Console.WriteLine("Decrypted XML:");
            Console.WriteLine();
            Console.WriteLine(xmlDoc.OuterXml);

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

    }

    public static void Decrypt(XmlDocument Doc)
    {
        // Check the arguments.  
        if (Doc == null)
            throw new ArgumentNullException("Doc");


        // Create a new EncryptedXml object.
        EncryptedXml exml = new EncryptedXml(Doc);

        // Decrypt the XML document.
        exml.DecryptDocument();

    }
}

此示例假定一个名为 "test.xml" 的文件与编译的程序在同一目录中。 它还假定 "test.xml" 包含一个 "creditcard" 元素。 您可以将以下 XML 放入名为 test.xml 的文件中,并将它和此示例一起使用。

<root>
    <creditcard>
        <number>19834209</number>
        <expiry>02/02/2002</expiry>
    </creditcard>
</root>

编译代码

安全性

此示例中使用的 X.509 证书仅用于测试目的。 应用程序应当使用由受信任的证书颁发机构生成的 X.509 证书,或者使用 Microsoft Windows Certificate Server 生成的证书。

请参见

任务

如何:用 X.509 证书对 XML 元素进行加密

参考

System.Security.Cryptography.Xml

其他资源

XML 加密和数字签名