KeyInfo コンストラクタ

KeyInfo クラスの新しいインスタンスを初期化します。

Public Sub New()
[C#]
public KeyInfo();
[C++]
public: KeyInfo();
[JScript]
public function KeyInfo();

使用例

 
' Sign an XML file and save the signature in a new file.
Public Shared Sub SignDetachedResource(URIString As String, XmlSigFileName As String, Key As RSA)
   ' Create a SignedXml object.
   Dim signedXml As New SignedXml()
   
   ' Assign the key to the SignedXml object.
   signedXml.SigningKey = Key
   
   ' Create a reference to be signed.
   Dim reference As New Reference()
   
   ' Add the passed URI to the reference object.
   reference.Uri = URIString
   
   ' Add a transformation if the URI is an XML file.
   If URIString.EndsWith("xml") Then
      reference.AddTransform(New XmlDsigC14NTransform())
   End If
   
   ' 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()
   
   ' Save the signed XML document to a file specified
   ' using the passed string.
   Dim xmltw As New XmlTextWriter(XmlSigFileName, New UTF8Encoding(False))
   xmlDigitalSignature.WriteTo(xmltw)
   xmltw.Close()
End Sub 

[C#] 
// Sign an XML file and save the signature in a new file.
public static void SignDetachedResource(string URIString, string XmlSigFileName, RSA Key)
{
    // Create a SignedXml object.
    SignedXml signedXml = new SignedXml();

    // Assign the key to the SignedXml object.
    signedXml.SigningKey = Key;

    // Create a reference to be signed.
    Reference reference = new Reference();

    // Add the passed URI to the reference object.
    reference.Uri = URIString;

    // Add a transformation if the URI is an XML file.
    if (URIString.EndsWith("xml")) 
    {
        reference.AddTransform(new XmlDsigC14NTransform());
    }
    
    // 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();

    // Save the signed XML document to a file specified
    // using the passed string.
    XmlTextWriter xmltw = new XmlTextWriter(XmlSigFileName, new UTF8Encoding(false));
    xmlDigitalSignature.WriteTo(xmltw);
    xmltw.Close();
}

[C++] 
// Sign an XML file and save the signature in a new file.
void SignDetachedResource(String* URIString, String* XmlSigFileName, RSA* Key) {
   // Create a SignedXml object.
   SignedXml* signedXml = new SignedXml();

   // Assign the key to the SignedXml object.
   signedXml->SigningKey = Key;

   // Create a reference to be signed.
   Reference* reference = new Reference();

   // Add the passed URI to the reference object.
   reference->Uri = URIString;

   // Add a transformation if the URI is an XML file.
   if (URIString->EndsWith(S"xml")) {
      reference->AddTransform(new XmlDsigC14NTransform());
   } 


   // 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(__try_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();

   // Save the signed XML document to a file specified
   // using the passed string.
   XmlTextWriter* xmltw = new XmlTextWriter(XmlSigFileName, new UTF8Encoding(false));
   xmlDigitalSignature->WriteTo(xmltw);
   xmltw->Close();
}

[Visual Basic] 
' Sign an XML file and save the signature in a new file.
Public Shared Sub SignXmlFile(FileName As String, SignedFileName As String, 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
   
   ' Create a reference to be signed.
   Dim reference As New Reference()
   reference.Uri = ""
   
   ' Add a transformation to the reference.
   Dim trns = New XmlDsigC14NTransform()
   reference.AddTransform(trns)
   
   ' 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 

[C#] 
// 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;

    // Create a reference to be signed.
    Reference reference = new Reference();
    reference.Uri = "";

    // Add a transformation to the reference.
    Transform trns = new XmlDsigC14NTransform();
    reference.AddTransform(trns);

    // 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();
}

[C++] 
// Sign an XML file and save the signature in a new file.
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 its 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;

   // Create a reference to be signed.
   Reference* reference = new Reference();
   reference->Uri = S"";

   // Add a transformation to the reference.
   Transform* trns = new XmlDsigC14NTransform();
   reference->AddTransform(trns);

   // 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(__try_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 ((doc->FirstChild)->GetType() == __typeof(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();
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

KeyInfo クラス | KeyInfo メンバ | System.Security.Cryptography.Xml 名前空間