次の方法で共有


署名の生成

更新 : 2007 年 11 月

通常、デジタル署名は大きなデータを表現するハッシュ値に適用されます。ハッシュ値にデジタル署名を適用する例を次に示します。まず、公開キーと秘密キーのペアを生成するために RSACryptoServiceProvider クラスの新しいインスタンスを作成します。次に、RSACryptoServiceProviderRSAPKCS1SignatureFormatter クラスの新しいインスタンスに渡します。これにより、実際にデジタル署名を実行する RSAPKCS1SignatureFormatter に秘密キーが渡されます。ハッシュ コードに署名するためには、使用するハッシュ アルゴリズムを指定する必要があります。この例では SHA1 アルゴリズムを使用します。最後に、RSAPKCS1SignatureFormatter.CreateSignature メソッドを呼び出して署名を実行します。

Imports System
Imports System.Security.Cryptography

Module Module1
    Sub Main()
        'The hash value to sign.
        Dim HashValue As Byte() = {59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135}

        'The value to hold the signed value.
        Dim SignedHashValue() As Byte

        'Generate a public/private key pair.
        Dim RSA As New RSACryptoServiceProvider()

        'Create an RSAPKCS1SignatureFormatter object and pass it 
        'the RSACryptoServiceProvider to transfer the private key.
        Dim RSAFormatter As New RSAPKCS1SignatureFormatter(RSA)

        'Set the hash algorithm to SHA1.
        RSAFormatter.SetHashAlgorithm("SHA1")

        'Create a signature for HashValue and assign it to 
        'SignedHashValue.
        SignedHashValue = RSAFormatter.CreateSignature(HashValue)
    End Sub
End Module

using System;
using System.Security.Cryptography;
class Class1
{
   static void Main()
   {
      //The hash value to sign.
      byte[] HashValue = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135};

      //The value to hold the signed value.
      byte[] SignedHashValue;

      //Generate a public/private key pair.
      RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

      //Create an RSAPKCS1SignatureFormatter object and pass it the 
      //RSACryptoServiceProvider to transfer the private key.
      RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);

      //Set the hash algorithm to SHA1.
      RSAFormatter.SetHashAlgorithm("SHA1");

      //Create a signature for HashValue and assign it to 
      //SignedHashValue.
      SignedHashValue = RSAFormatter.CreateSignature(HashValue);
   }
}

XML ファイルへの署名

.NET Framework が提供する System.Security.Cryptography.XML 名前空間を使用すると、XML に署名できるようになります。XML が特定のソースから送信されたことを検査する必要があるときは、XML に署名を付けることが重要です。たとえば、XML を使用した株価サービスを利用している場合、その XML に署名が付いていれば、XML のソースを検査できます。

この名前空間に含まれるクラスは、World Wide Web Consortium による勧告『XML-Signature Syntax and Processing』に準拠しています。この勧告は www.w3.org で公開されています。

参照

概念

署名の検査

暗号署名

その他の技術情報

暗号タスク

暗号サービス