事件
作法:使用 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 項目。
這個範例適合多個應用程式需要共用加密資料或應用程式需要在它執行時間之間儲存加密資料的情況。
藉由從磁碟載入 XML 檔案,建立 XmlDocument 物件。 XmlDocument 物件會包含要解密的 XML 項目。
C#XmlDocument xmlDoc = new XmlDocument();
Dim xmlDoc As New XmlDocument()
建立新的 EncryptedXml 物件,並傳遞 XmlDocument 物件給建構函式。
C#EncryptedXml exml = new EncryptedXml(Doc);
Dim exml As New EncryptedXml(Doc)
使用 DecryptDocument 方法解密 XML 文件。
C#exml.DecryptDocument();
exml.DecryptDocument()
儲存 XmlDocument 物件。
C#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 Framework 為目標的專案中,包含
System.Security.dll
的參考。在以 .NET Core 或 .NET 5 為目標的專案中,安裝 NuGet 套件 System.Security.Cryptography.Xml。
包含下列命名空間:System.Xml、System.Security.Cryptography 和 System.Security.Cryptography.Xml。
此範例中使用的 X.509 憑證僅供測試使用。 應用程式應該使用受信任的憑證授權單位單位所產生的 X.509 憑證。
其他資源
訓練
認證
Microsoft Certified: Cybersecurity Architect Expert - Certifications
身為 Microsoft 網路安全性架構師,您會將網路安全性策略轉化為保護組織資產、業務和營運的能力。
文件
-
作法:使用 X.509 憑證加密 XML 元素 - .NET
深入了解:HOW TO:操作說明:使用 X.509 憑證加密 XML 項目
-
了解如何使用非對稱金鑰加密 XML 元素。
-
瞭解如何使用數位簽章簽署 XML 文件。 在 .NET 中使用 System.Security.Cryptography.Xml 命名空間中的類別。