如何:使用 X.509 证书解密 XML 元素
可以使用 System.Security.Cryptography.Xml 命名空间中的类对 XML 文档内的元素进行加密和解密。 XML 加密是交换或存储加密的 XML 数据的一种标准方式,使用后就无需担心数据被轻易读取。 有关 XML 加密标准的详细信息,请参阅万维网联合会 (W3C) 对于 XML 加密的规范,该规范位于 https://www.w3.org/TR/xmldsig-core/。
此示例对使用如何:用 X.509 证书加密 XML 元素中描述的方法进行加密的 XML 元素进行解密。 它找到一个 <EncryptedData
> 元素,解密该元素,然后将其替换为原始纯文本 XML 元素。
此过程中的代码示例将使用当前用户帐户的本地证书存储中的 X.509 证书来解密 XML 元素。 该示例使用 DecryptDocument 方法自动检索 X.509 证书,然后对 <EncryptedData
> 元素的 <EncryptedKey
> 元素中存储的会话密钥进行解密。 然后,DecryptDocument 方法将自动使用会话密钥对 XML 元素进行解密。
此示例适用于以下情况:多个应用程序需要共享加密数据,或应用程序需要保存它各次运行之间的加密数据。
用 X.509 证书解密 XML 元素
通过从磁盘加载 XML 文件来创建 XmlDocument 对象。 XmlDocument 对象包含要解密的 XML 元素。
XmlDocument xmlDoc = new XmlDocument();
Dim xmlDoc As New XmlDocument()
通过将 XmlDocument 对象传递给构造函数,创建一个新的 EncryptedXml 对象。
EncryptedXml exml = new EncryptedXml(Doc);
Dim exml As New EncryptedXml(Doc)
使用 DecryptDocument 方法解密 XML 文档。
exml.DecryptDocument();
exml.DecryptDocument()
保存 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 Framework 的项目中,包含对
System.Security.dll
的引用。在面向 .NET Core 或 .NET 5 的项目中,安装 NuGet 包 System.Security.Cryptography.Xml。
包括以下命名空间:System.Xml、System.Security.Cryptography 和 System.Security.Cryptography.Xml。
.NET 安全性
此示例中使用的 X.509 证书仅用于测试目的。 应用程序应使用受信任的证书授权生成的 X.509 证书。
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈