XmlDsigExcC14NTransform 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表由萬維網聯合會 (W3C) 所定義之數位簽名的獨佔 C14N XML 標準轉換,而不加上批注。
public ref class XmlDsigExcC14NTransform : System::Security::Cryptography::Xml::Transform
public class XmlDsigExcC14NTransform : System.Security.Cryptography.Xml.Transform
type XmlDsigExcC14NTransform = class
inherit Transform
Public Class XmlDsigExcC14NTransform
Inherits Transform
- 繼承
- 衍生
範例
下列程式代碼範例示範如何使用信封簽章,使用 XmlDsigExcC14NTransform 類別簽署 XML 檔。
//
// This example signs an XML file using an
// envelope signature. It then verifies the
// signed XML.
//
#using <System.Xml.dll>
#using <System.Security.dll>
#using <System.dll>
using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Security::Cryptography::X509Certificates;
using namespace System::Security::Cryptography::Xml;
using namespace System::Text;
using namespace System::Xml;
// Sign an XML file and save the signature in a new file.
static void SignXmlFile( String^ FileName, String^ SignedFileName, RSA^ Key )
{
// Create a new XML document.
XmlDocument^ doc = gcnew XmlDocument;
// Format the document to ignore white spaces.
doc->PreserveWhitespace = false;
// Load the passed XML file using it's name.
doc->Load( gcnew XmlTextReader( FileName ) );
// Create a SignedXml object.
SignedXml^ signedXml = gcnew SignedXml( doc );
// Add the key to the SignedXml document.
signedXml->SigningKey = Key;
// Specify a canonicalization method.
signedXml->SignedInfo->CanonicalizationMethod = SignedXml::XmlDsigExcC14NTransformUrl;
// Set the InclusiveNamespacesPrefixList property.
XmlDsigExcC14NTransform^ canMethod = dynamic_cast<XmlDsigExcC14NTransform^>(signedXml->SignedInfo->CanonicalizationMethodObject);
canMethod->InclusiveNamespacesPrefixList = L"Sign";
// Create a reference to be signed.
Reference^ reference = gcnew Reference;
reference->Uri = L"";
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform^ env = gcnew XmlDsigEnvelopedSignatureTransform;
reference->AddTransform( env );
// Add the reference to the SignedXml object.
signedXml->AddReference( reference );
// Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate).
KeyInfo^ keyInfo = gcnew KeyInfo;
keyInfo->AddClause( gcnew RSAKeyValue( dynamic_cast<RSA^>(Key) ) );
signedXml->KeyInfo = keyInfo;
// Compute the signature.
signedXml->ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
XmlElement^ xmlDigitalSignature = signedXml->GetXml();
// Append the element to the XML document.
doc->DocumentElement->AppendChild( doc->ImportNode( xmlDigitalSignature, true ) );
if ( dynamic_cast<XmlDeclaration^>(doc->FirstChild) )
{
doc->RemoveChild( doc->FirstChild );
}
// Save the signed XML document to a file specified
// using the passed string.
XmlTextWriter^ xmltw = gcnew XmlTextWriter( SignedFileName,gcnew UTF8Encoding( false ) );
doc->WriteTo( xmltw );
xmltw->Close();
}
// Verify the signature of an XML file and return the result.
static Boolean VerifyXmlFile( String^ Name )
{
// Create a new XML document.
XmlDocument^ xmlDocument = gcnew XmlDocument;
// Format using white spaces.
xmlDocument->PreserveWhitespace = true;
// Load the passed XML file into the document.
xmlDocument->Load( Name );
// Create a new SignedXml object and pass it
// the XML document class.
SignedXml^ signedXml = gcnew SignedXml( xmlDocument );
// Find the "Signature" node and create a new
// XmlNodeList object.
XmlNodeList^ nodeList = xmlDocument->GetElementsByTagName( L"Signature" );
// Load the signature node.
signedXml->LoadXml( dynamic_cast<XmlElement^>(nodeList->Item( 0 )) );
// Check the signature and return the result.
return signedXml->CheckSignature();
}
// Create example data to sign.
static void CreateSomeXml( String^ FileName )
{
// Create a new XmlDocument object.
XmlDocument^ document = gcnew XmlDocument;
// Create a new XmlNode object.
XmlNode^ node = document->CreateNode( XmlNodeType::Element, L"", L"MyXML", L"Don't_Sign" );
// Append the node to the document.
document->AppendChild( node );
// Create a new XmlNode object.
XmlNode^ subnode = document->CreateNode( XmlNodeType::Element, L"", L"TempElement", L"Sign" );
// Add some text to the node.
subnode->InnerText = L"Here is some data to sign.";
// Append the node to the document.
document->DocumentElement->AppendChild( subnode );
// Save the XML document to the file name specified.
XmlTextWriter^ xmltw = gcnew XmlTextWriter( FileName,gcnew UTF8Encoding( false ) );
document->WriteTo( xmltw );
xmltw->Close();
}
int main()
{
try
{
// Generate a signing key.
RSA^ Key = RSA::Create();
// Create an XML file to sign.
CreateSomeXml( L"Example.xml" );
Console::WriteLine( L"New XML file created." );
// Sign the XML that was just created and save it in a
// new file.
SignXmlFile( L"Example.xml", L"SignedExample.xml", Key );
Console::WriteLine( L"XML file signed." );
// Verify the signature of the signed XML.
Console::WriteLine( L"Verifying signature..." );
bool result = VerifyXmlFile( L"SignedExample.xml" );
// Display the results of the signature verification to \
// the console.
if ( result )
{
Console::WriteLine( L"The XML signature is valid." );
}
else
{
Console::WriteLine( L"The XML signature is not valid." );
}
}
catch ( CryptographicException^ e )
{
Console::WriteLine( e->Message );
}
return 1;
}
//
// This example signs an XML file using an
// envelope signature. It then verifies the
// signed XML.
//
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Xml;
public class SignVerifyEnvelope
{
public static void Main(String[] args)
{
try
{
// Generate a signing key.
RSA Key = RSA.Create();
// Create an XML file to sign.
CreateSomeXml("Example.xml");
Console.WriteLine("New XML file created.");
// Sign the XML that was just created and save it in a
// new file.
SignXmlFile("Example.xml", "SignedExample.xml", Key);
Console.WriteLine("XML file signed.");
// Verify the signature of the signed XML.
Console.WriteLine("Verifying signature...");
bool result = VerifyXmlFile("SignedExample.xml");
// Display the results of the signature verification to \
// the console.
if (result)
{
Console.WriteLine("The XML signature is valid.");
}
else
{
Console.WriteLine("The XML signature is not valid.");
}
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
}
// Sign an XML file and save the signature in a new file.
public static void SignXmlFile(string FileName, string SignedFileName, RSA Key)
{
// Create a new XML document.
XmlDocument doc = new XmlDocument();
// Format the document to ignore white spaces.
doc.PreserveWhitespace = false;
// Load the passed XML file using it's name.
doc.Load(new XmlTextReader(FileName));
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(doc);
// Add the key to the SignedXml document.
signedXml.SigningKey = Key;
// Specify a canonicalization method.
signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;
// Set the InclusiveNamespacesPrefixList property.
XmlDsigExcC14NTransform canMethod = (XmlDsigExcC14NTransform)signedXml.SignedInfo.CanonicalizationMethodObject;
canMethod.InclusiveNamespacesPrefixList = "Sign";
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate).
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new RSAKeyValue((RSA)Key));
signedXml.KeyInfo = keyInfo;
// Compute the signature.
signedXml.ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document.
doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));
if (doc.FirstChild is XmlDeclaration)
{
doc.RemoveChild(doc.FirstChild);
}
// Save the signed XML document to a file specified
// using the passed string.
XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new UTF8Encoding(false));
doc.WriteTo(xmltw);
xmltw.Close();
}
// Verify the signature of an XML file and return the result.
public static Boolean VerifyXmlFile(String Name)
{
// Create a new XML document.
XmlDocument xmlDocument = new XmlDocument();
// Format using white spaces.
xmlDocument.PreserveWhitespace = true;
// Load the passed XML file into the document.
xmlDocument.Load(Name);
// Create a new SignedXml object and pass it
// the XML document class.
SignedXml signedXml = new SignedXml(xmlDocument);
// Find the "Signature" node and create a new
// XmlNodeList object.
XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature");
// Load the signature node.
signedXml.LoadXml((XmlElement)nodeList[0]);
// Check the signature and return the result.
return signedXml.CheckSignature();
}
// Create example data to sign.
public static void CreateSomeXml(string FileName)
{
// Create a new XmlDocument object.
XmlDocument document = new XmlDocument();
// Create a new XmlNode object.
XmlNode node = document.CreateNode(XmlNodeType.Element, "", "MyXML", "Don't_Sign");
// Append the node to the document.
document.AppendChild(node);
// Create a new XmlNode object.
XmlNode subnode = document.CreateNode(XmlNodeType.Element, "", "TempElement", "Sign");
// Add some text to the node.
subnode.InnerText = "Here is some data to sign.";
// Append the node to the document.
document.DocumentElement.AppendChild(subnode);
// Save the XML document to the file name specified.
XmlTextWriter xmltw = new XmlTextWriter(FileName, new UTF8Encoding(false));
document.WriteTo(xmltw);
xmltw.Close();
}
}
'
' This example signs an XML file using an
' envelope signature. It then verifies the
' signed XML.
'
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.Security.Cryptography.Xml
Imports System.Text
Imports System.Xml
Module SignVerifyEnvelope
Sub Main(ByVal args() As String)
Try
' Generate a signing key.
Dim Key As RSA = RSA.Create()
' Create an XML file to sign.
CreateSomeXml("Example.xml")
Console.WriteLine("New XML file created.")
' Sign the XML that was just created and save it in a
' new file.
SignXmlFile("Example.xml", "SignedExample.xml", Key)
Console.WriteLine("XML file signed.")
' Verify the signature of the signed XML.
Console.WriteLine("Verifying signature...")
Dim result As Boolean = VerifyXmlFile("SignedExample.xml")
' Display the results of the signature verification to
' the console.
If result Then
Console.WriteLine("The XML signature is valid.")
Else
Console.WriteLine("The XML signature is not valid.")
End If
Catch e As CryptographicException
Console.WriteLine(e.Message)
End Try
End Sub
' Sign an XML file and save the signature in a new file.
Sub SignXmlFile(ByVal FileName As String, ByVal SignedFileName As String, ByVal Key As RSA)
' Create a new XML document.
Dim doc As New XmlDocument()
' Format the document to ignore white spaces.
doc.PreserveWhitespace = False
' Load the passed XML file using it's name.
doc.Load(New XmlTextReader(FileName))
' Create a SignedXml object.
Dim signedXml As New SignedXml(doc)
' Add the key to the SignedXml document.
signedXml.SigningKey = Key
' Specify a canonicalization method.
signedXml.SignedInfo.CanonicalizationMethod = signedXml.XmlDsigExcC14NTransformUrl
' Set the InclusiveNamespacesPrefixList property.
Dim canMethod As XmlDsigExcC14NTransform = CType(signedXml.SignedInfo.CanonicalizationMethodObject, XmlDsigExcC14NTransform)
canMethod.InclusiveNamespacesPrefixList = "Sign"
' Create a reference to be signed.
Dim reference As New Reference()
reference.Uri = ""
' Add an enveloped transformation to the reference.
Dim env As New XmlDsigEnvelopedSignatureTransform()
reference.AddTransform(env)
' Add the reference to the SignedXml object.
signedXml.AddReference(reference)
' Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate).
Dim keyInfo As New KeyInfo()
keyInfo.AddClause(New RSAKeyValue(CType(Key, RSA)))
signedXml.KeyInfo = keyInfo
' Compute the signature.
signedXml.ComputeSignature()
' Get the XML representation of the signature and save
' it to an XmlElement object.
Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()
' Append the element to the XML document.
doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, True))
If TypeOf doc.FirstChild Is XmlDeclaration Then
doc.RemoveChild(doc.FirstChild)
End If
' Save the signed XML document to a file specified
' using the passed string.
Dim xmltw As New XmlTextWriter(SignedFileName, New UTF8Encoding(False))
doc.WriteTo(xmltw)
xmltw.Close()
End Sub
' Verify the signature of an XML file and return the result.
Function VerifyXmlFile(ByVal Name As String) As [Boolean]
' Create a new XML document.
Dim xmlDocument As New XmlDocument()
' Format using white spaces.
xmlDocument.PreserveWhitespace = True
' Load the passed XML file into the document.
xmlDocument.Load(Name)
' Create a new SignedXml object and pass it
' the XML document class.
Dim signedXml As New SignedXml(xmlDocument)
' Find the "Signature" node and create a new
' XmlNodeList object.
Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("Signature")
' Load the signature node.
signedXml.LoadXml(CType(nodeList(0), XmlElement))
' Check the signature and return the result.
Return signedXml.CheckSignature()
End Function
' Create example data to sign.
Sub CreateSomeXml(ByVal FileName As String)
' Create a new XmlDocument object.
Dim document As New XmlDocument()
' Create a new XmlNode object.
Dim node As XmlNode = document.CreateNode(XmlNodeType.Element, "", "MyXML", "Don't_Sign")
' Append the node to the document.
document.AppendChild(node)
' Create a new XmlNode object.
Dim subnode As XmlNode = document.CreateNode(XmlNodeType.Element, "", "TempElement", "Sign")
' Add some text to the node.
subnode.InnerText = "Here is some data to sign."
' Append the node to the document.
document.DocumentElement.AppendChild(subnode)
' Save the XML document to the file name specified.
Dim xmltw As New XmlTextWriter(FileName, New UTF8Encoding(False))
document.WriteTo(xmltw)
xmltw.Close()
End Sub
End Module
備註
XmlDsigExcC14NTransform 類別代表不含批注的獨佔 C14N XML 標準化轉換。 這個類別類似於 XmlDsigC14NTransform 類別,可讓簽署者使用 XML 檔的正式形式建立摘要。 不過,XmlDsigExcC14NTransform 類別會從標準化子檔排除上階內容。
當您需要規範 XML 子檔,使其與 XML 內容無關時,請使用 XmlDsigC14NTransform 類別。 例如,在複雜通訊協定中使用已簽署 XML 的 Web 服務之類的應用程式,通常需要以這種方式規範 XML。 這類應用程式通常會將 XML 包圍在各種動態建構的元素內,這可以大幅變更檔,並導致 XML 簽章驗證失敗。 XmlDsigExcC14NTransform 類別會藉由排除標準子檔中的這類上階內容來解決此問題。
一般而言,您不會建立標準化轉換類別的新實例。 若要指定標準化轉換,請將描述轉換的統一資源識別元 (URI) 傳遞至可從 SignedInfo 屬性存取的 CanonicalizationMethod 屬性。 若要取得標準化轉換的參考,請使用可從 SignedInfo 屬性存取的 CanonicalizationMethodObject 屬性。
只有在您想要手動哈希 XML 檔或使用自己的標準化演算法時,才需要建立標準化轉換類別的新實例。
描述 XmlDsigExcC14NWithCommentsTransform 類別的 URI 是由 [XmlDsigExcC14NWithCommentsTransformUrl] 字段所定義。
描述 XmlDsigExcC14NTransform 類別的 URI 是由 [XmlDsigExcC14NTransformUrl] 字段所定義。
如需獨佔 C14N 轉換的詳細資訊,請參閱 W3C XMLDSIG 規格。 標準化演算法定義於 W3C 標準 XML 規格中。
建構函式
XmlDsigExcC14NTransform() |
初始化 XmlDsigExcC14NTransform 類別的新實例。 |
XmlDsigExcC14NTransform(Boolean, String) |
初始化 XmlDsigExcC14NTransform 類別的新實例,指定是否要包含批注,以及指定命名空間前置詞的清單。 |
XmlDsigExcC14NTransform(Boolean) |
初始化 XmlDsigExcC14NTransform 類別的新實例,指定值,以判斷是否要包含批注。 |
XmlDsigExcC14NTransform(String) |
初始化 XmlDsigExcC14NTransform 類別的新實例,這個類別會指定使用標準標準標準化演算法規範的命名空間前置詞清單。 |
屬性
Algorithm |
取得或設定統一資源識別元 (URI),識別目前轉換所執行的演算法。 (繼承來源 Transform) |
Context |
取得或設定 XmlElement 物件,表示目前 Transform 物件執行所在的文件內容。 (繼承來源 Transform) |
InclusiveNamespacesPrefixList |
取得或設定字串,其中包含使用標準標準標準化演算法進行標準化之命名空間前置詞的字串。 |
InputTypes |
取得類型陣列,這些型別是目前 XmlDsigExcC14NTransform 物件之 LoadInput(Object) 方法的有效輸入。 |
OutputTypes |
取得型別的陣列,這些型別可能是目前 XmlDsigExcC14NTransform 物件之 GetOutput() 方法的輸出。 |
PropagatedNamespaces |
取得或設定 Hashtable 物件,其中包含傳播至簽章的命名空間。 (繼承來源 Transform) |
Resolver |
設定目前的 XmlResolver 物件。 (繼承來源 Transform) |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetDigestedOutput(HashAlgorithm) |
傳回與 XmlDsigExcC14NTransform 對象相關聯的摘要。 |
GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
GetInnerXml() |
傳回適用於 XMLDSIG |
GetOutput() |
傳回目前 XmlDsigExcC14NTransform 對象的輸出。 |
GetOutput(Type) |
傳回目前 XmlDsigExcC14NTransform 對象的輸出做為指定型別的物件。 |
GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
GetXml() |
傳回目前 Transform 物件的 XML 表示。 (繼承來源 Transform) |
LoadInnerXml(XmlNodeList) |
將指定的 XmlNodeList 物件剖析為 |
LoadInput(Object) |
在衍生類別中覆寫時,將指定的輸入載入目前 XmlDsigExcC14NTransform 物件。 |
MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |