作法:使用 X.509 憑證解密 XML 元素

您可以使用 System.Security.Cryptography.Xml 命名空間中的類別來加密和解密 XML 文件內的項目。 XML 加密是交換或儲存加密 XML 資料的標準方法,不必擔心資料被輕易讀取。 如需 XML 加密標準的詳細資訊,請參閱 XML 加密的全球資訊網協會 (W3C) 規格,位於 https://www.w3.org/TR/xmldsig-core/

此範例會解密使用 HOW TO:使用 X.509 憑證加密 XML 項目中所述方法加密的 XML 項目。 其會找出 <EncryptedData> 項目、解密該項目,然後再以原始純文字 XML 項目取代該項目。

此程序的程式碼範例會使用來自目前使用者帳戶本機憑證存放區的 X.509 憑證解密 XML 項目。 這個範例會使用 DecryptDocument 方法自動擷取 X.509 憑證,並解密儲存在 <EncryptedData> 項目之 <EncryptedKey> 項目中的工作階段金鑰。 DecryptDocument 方法接著會自動使用工作階段金鑰解密 XML 項目。

這個範例適合多個應用程式需要共用加密資料或應用程式需要在它執行時間之間儲存加密資料的情況。

使用 X.509 憑證解密 XML 項目

  1. 藉由從磁碟載入 XML 檔案,建立 XmlDocument 物件。 XmlDocument 物件會包含要解密的 XML 項目。

    XmlDocument xmlDoc = new XmlDocument();
    
    Dim xmlDoc As New XmlDocument()
    
  2. 建立新的 EncryptedXml 物件,並傳遞 XmlDocument 物件給建構函式。

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

    exml.DecryptDocument();
    
    exml.DecryptDocument()
    
  4. 儲存 XmlDocument 物件。

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

範例

這個範例假設名為 "test.xml" 的檔案已存在於和編譯程式相同的目錄中。 它同時也假設 "test.xml" 包含 "creditcard" 元素。 您可以將下列 XML 放入稱為 test.xml 的檔案,並使用它搭配此範例。

<root>  
    <creditcard>  
        <number>19834209</number>  
        <expiry>02/02/2002</expiry>  
    </creditcard>  
</root>  
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();
    }
}
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.  
        ArgumentNullException.ThrowIfNull(Doc)

        ' Create a new EncryptedXml object.
        Dim exml As New EncryptedXml(Doc)
        ' Decrypt the XML document.
        exml.DecryptDocument()
    End Sub
End Module

編譯程式碼

.NET 安全性

此範例中使用的 X.509 憑證僅供測試使用。 應用程式應該使用受信任的憑證授權單位單位所產生的 X.509 憑證。

另請參閱