Share via


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

您可以使用 System.Security.Cryptography.Xml 命名空間中的類別,加密及解密 XML 文件中的項目。 XML 加密是交換或儲存加密 XML 資料的標準方法,無資料遭竊取之虞。 如需 XML 加密標準的詳細資訊,請參閱全球資訊網協會 (W3C) 的 XML 加密規格,網址為 http://www.w3.org/TR/xmldsig-core/。

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

這個程序中的程式碼範例會使用 X.509 憑證來解密 XML 項目,這個 X.509 憑證位於目前使用者帳戶的本機憑證存放區。 此範例會使用 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. 建立新的 EncryptedXml 物件,並將 XmlDocument 物件傳遞給建構函式。

    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 產生的憑證。

請參閱

工作

HOW TO:使用 X.509 憑證加密 XML 項目

參考

System.Security.Cryptography.Xml

其他資源

XML 加密和數位簽章