次の方法で共有


KeyInfoX509Data コンストラクタ (X509Certificate)

指定した X.509v3 証明書から KeyInfoX509Data クラスの新しいインスタンスを初期化します。

名前空間: System.Security.Cryptography.Xml
アセンブリ: System.Security (system.security.dll 内)

構文

'宣言
Public Sub New ( _
    cert As X509Certificate _
)
'使用
Dim cert As X509Certificate

Dim instance As New KeyInfoX509Data(cert)
public KeyInfoX509Data (
    X509Certificate cert
)
public:
KeyInfoX509Data (
    X509Certificate^ cert
)
public KeyInfoX509Data (
    X509Certificate cert
)
public function KeyInfoX509Data (
    cert : X509Certificate
)
適用できません。

パラメータ

例外

例外の種類 条件

ArgumentNullException

cert パラメータが null 参照 (Visual Basic では Nothing) です。

使用例

このセクションには、2 つのコード例が含まれています。最初の例では、デタッチ シグネチャを使用して XML ファイルに署名する方法を示します。2 番目の例では、エンベロープ シグネチャを使用して XML ファイルに署名する方法を示します。

例 1

   ' 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, Certificate As String)
      ' 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 the reference to the SignedXml object.
      signedXml.AddReference(reference)
      
      ' Create a new KeyInfo object.
      Dim keyInfo As New KeyInfo()
      
      ' Load the X509 certificate.
      Dim MSCert As X509Certificate = X509Certificate.CreateFromCertFile(Certificate)
      
      ' Load the certificate into a KeyInfoX509Data object
      ' and add it to the KeyInfo object.
      keyInfo.AddClause(New KeyInfoX509Data(MSCert))
      
      ' Add the KeyInfo object to the SignedXml object.
      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  
End Class 
// Sign an XML file and save the signature in a new file.
public static void SignDetachedResource(string URIString, string XmlSigFileName, RSA Key, string Certificate)
{
    // 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 the reference to the SignedXml object.
    signedXml.AddReference(reference);

    // Create a new KeyInfo object.
    KeyInfo keyInfo = new KeyInfo();

    // Load the X509 certificate.
    X509Certificate MSCert = X509Certificate.CreateFromCertFile(Certificate);
 
    // Load the certificate into a KeyInfoX509Data object
    // and add it to the KeyInfo object.
    keyInfo.AddClause(new KeyInfoX509Data(MSCert));
  
    // Add the KeyInfo object to the SignedXml object.
    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();

}
// Sign an XML file and save the signature in a new file.
void SignDetachedResource( String^ URIString, String^ XmlSigFileName, RSA^ Key, String^ Certificate )
{
   
   // Create a SignedXml object.
   SignedXml^ signedXml = gcnew SignedXml;
   
   // Assign the key to the SignedXml object.
   signedXml->SigningKey = Key;
   
   // Create a reference to be signed.
   Reference^ reference = gcnew Reference;
   
   // Add the passed URI to the reference object.
   reference->Uri = URIString;
   
   // Add the reference to the SignedXml object.
   signedXml->AddReference( reference );
   
   // Create a new KeyInfo object.
   KeyInfo^ keyInfo = gcnew KeyInfo;
   
   // Load the X509 certificate.
   X509Certificate^ MSCert = X509Certificate::CreateFromCertFile( Certificate );
   
   // Load the certificate into a KeyInfoX509Data object
   // and add it to the KeyInfo object.
   keyInfo->AddClause( gcnew KeyInfoX509Data( MSCert ) );
   
   // Add the KeyInfo object to the SignedXml object.
   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 = gcnew XmlTextWriter( XmlSigFileName,gcnew UTF8Encoding( false ) );
   xmlDigitalSignature->WriteTo( xmltw );
   xmltw->Close();
}

// Sign an XML file and save the signature in a new file.
public static void SignDetachedResource(String uriString, 
    String xmlSigFileName, RSA key, String certificate)
{
    // Create a SignedXml object.
    SignedXml signedXml = new SignedXml();

    // Assign the key to the SignedXml object.
    signedXml.set_SigningKey(key);

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

    // Add the passed URI to the reference object.
    reference.set_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);

    // Create a new KeyInfo object.
    KeyInfo keyInfo = new KeyInfo();

    // Load the X509 certificate.
    X509Certificate msCert = 
        X509Certificate.CreateFromCertFile(certificate);

    // Load the certificate into a KeyInfoX509Data object
    // and add it to the KeyInfo object.
    keyInfo.AddClause(new KeyInfoX509Data(msCert));

    // Add the KeyInfo object to the SignedXml object.
    signedXml.set_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();
} //SignDetachedResource 

例 2

' 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, Certificate As String)
   ' 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 an enveloped transformation to the reference.
   Dim env As New XmlDsigEnvelopedSignatureTransform()
   reference.AddTransform(env)
   
   ' Add the reference to the SignedXml object.
   signedXml.AddReference(reference)
   
   ' Create a new KeyInfo object.
   Dim keyInfo As New KeyInfo()
   
   ' Load the X509 certificate.
   Dim MSCert As X509Certificate = X509Certificate.CreateFromCertFile(Certificate)
   
   ' Load the certificate into a KeyInfoX509Data object
   ' and add it to the KeyInfo object.
   keyInfo.AddClause(New KeyInfoX509Data(MSCert))
   
   ' Add the KeyInfo object to the SignedXml object.
   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 
// Sign an XML file and save the signature in a new file.
public static void SignXmlFile(string FileName, string SignedFileName, RSA Key, string Certificate)
{
    // 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 an enveloped transformation to the reference.
    XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
    reference.AddTransform(env);

    // Add the reference to the SignedXml object.
    signedXml.AddReference(reference);

    // Create a new KeyInfo object.
    KeyInfo keyInfo = new KeyInfo();

    // Load the X509 certificate.
    X509Certificate MSCert = X509Certificate.CreateFromCertFile(Certificate);
 
    // Load the certificate into a KeyInfoX509Data object
    // and add it to the KeyInfo object.
    keyInfo.AddClause(new KeyInfoX509Data(MSCert));
  
    // Add the KeyInfo object to the SignedXml object.
    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();
}
// Sign an XML file and save the signature in a new file.
void SignXmlFile( String^ FileName, String^ SignedFileName, RSA^ Key, String^ Certificate )
{
   
   // 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 its 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;
   
   // Create a reference to be signed.
   Reference^ reference = gcnew Reference;
   reference->Uri = "";
   
   // Add an enveloped transformation to the reference.
   XmlDsigEnvelopedSignatureTransform^ env = gcnew XmlDsigEnvelopedSignatureTransform;
   reference->AddTransform( env );
   
   // Add the reference to the SignedXml object.
   signedXml->AddReference( reference );
   
   // Create a new KeyInfo object.
   KeyInfo^ keyInfo = gcnew KeyInfo;
   
   // Load the X509 certificate.
   X509Certificate^ MSCert = X509Certificate::CreateFromCertFile( Certificate );
   
   // Load the certificate into a KeyInfoX509Data object
   // and add it to the KeyInfo object.
   keyInfo->AddClause( gcnew KeyInfoX509Data( MSCert ) );
   
   // Add the KeyInfo object to the SignedXml object.
   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() == XmlDeclaration::typeid )
   {
      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();
}

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

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