次の方法で共有


RSACryptoServiceProvider.SignData メソッド

指定したデータのハッシュ値を計算し、算出された値に署名します。

オーバーロードの一覧

指定したハッシュ アルゴリズムを使用して、指定したバイト配列のハッシュ値を計算し、結果ハッシュ値に署名します。

[Visual Basic] Overloads Public Function SignData(Byte(), Object) As Byte()

[C#] public byte[] SignData(byte[], object);

[C++] public: unsigned char SignData(unsigned char __gc[], Object*) __gc[];

[JScript] public function SignData(Byte[], Object) : Byte[];

指定したハッシュ アルゴリズムを使用して、指定した入力ストリームのハッシュ値を計算し、結果ハッシュ値に署名します。

[Visual Basic] Overloads Public Function SignData(Stream, Object) As Byte()

[C#] public byte[] SignData(Stream, object);

[C++] public: unsigned char SignData(Stream*, Object*) __gc[];

[JScript] public function SignData(Stream, Object) : Byte[];

指定したハッシュ アルゴリズムを使用して、指定したバイト配列のサブセットのハッシュ値を計算し、結果のハッシュ値に署名します。

[Visual Basic] Overloads Public Function SignData(Byte(), Integer, Integer, Object) As Byte()

[C#] public byte[] SignData(byte[], int, int, object);

[C++] public: unsigned char SignData(unsigned char __gc[], int, int, Object*) __gc[];

[JScript] public function SignData(Byte[], int, int, Object) : Byte[];

使用例

[Visual Basic, C#] SignData メソッドを使用する方法を次の例に示します。

[Visual Basic, C#] メモ   ここでは、SignData のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Imports System.Security.Cryptography
Imports System.Text

Module RSACSPExample

    Sub Main()
        Try
            ' Create a UnicodeEncoder to convert between byte array and string.
            Dim ByteConverter As New ASCIIEncoding

            Dim dataString As String = "Data to Sign"

            ' Create byte arrays to hold original, encrypted, and decrypted data.
            Dim originalData As Byte() = ByteConverter.GetBytes(dataString)
            Dim signedData() As Byte
            Dim smallArray() As Byte

            ' Create a new instance of the RSACryptoServiceProvider class 
            ' and automatically create a new key-pair.
            Dim RSAalg As New RSACryptoServiceProvider

            ' Export the key information to an RSAParameters object.
            ' You must pass true to export the private key for signing.
            ' However, you do not need to export the private key
            ' for verification.
            Dim Key As RSAParameters = RSAalg.ExportParameters(True)

            ' Hash and sign the data.  Start at the fifth offset
            ' only use data from the next 7 bytes.
            signedData = HashAndSignBytes(originalData, Key, 5, 7)

            ' The previous function only signed one segment
            ' of the array.  Create a new array for verification
            ' that only holds the data that was actually signed.
            '
            ' Initialize the array.
            smallArray = New Byte(6) {}
            ' Copy 7 bytes starting at the 5th index to 
            ' the new array.
            Array.Copy(originalData, 5, smallArray, 0, 7)

            ' Verify the data and display the result to the 
            ' console.  
            If VerifySignedHash(smallArray, signedData, Key) Then
                Console.WriteLine("The data was verified.")
            Else
                Console.WriteLine("The data does not match the signature.")
            End If

        Catch e As ArgumentNullException
            Console.WriteLine("The data was not signed or verified")
        End Try
    End Sub

    Function HashAndSignBytes(ByVal DataToSign() As Byte, ByVal Key As RSAParameters, ByVal Index As Integer, ByVal Length As Integer) As Byte()
        Try
            ' Create a new instance of RSACryptoServiceProvider using the 
            ' key from RSAParameters.  
            Dim RSAalg As New RSACryptoServiceProvider

            RSAalg.ImportParameters(Key)

            ' Hash and sign the data. Pass a new instance of SHA1CryptoServiceProvider
            ' to specify the use of SHA1 for hashing.
            Return RSAalg.SignData(DataToSign, Index, Length, New SHA1CryptoServiceProvider)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)

            Return Nothing
        End Try
    End Function


    Function VerifySignedHash(ByVal DataToVerify() As Byte, ByVal SignedData() As Byte, ByVal Key As RSAParameters) As Boolean
        Try
            ' Create a new instance of RSACryptoServiceProvider using the 
            ' key from RSAParameters.
            Dim RSAalg As New RSACryptoServiceProvider

            RSAalg.ImportParameters(Key)

            ' Verify the data using the signature.  Pass a new instance of SHA1CryptoServiceProvider
            ' to specify the use of SHA1 for hashing.
            Return RSAalg.VerifyData(DataToVerify, New SHA1CryptoServiceProvider, SignedData)

        Catch e As CryptographicException
            Console.WriteLine(e.Message)

            Return False
        End Try
    End Function
End Module

[C#] 
using System;
using System.Security.Cryptography;
using System.Text;

class RSACSPSample
{
    static void Main()
    {
        try
        {
            // Create a UnicodeEncoder to convert between byte array and string.
            ASCIIEncoding ByteConverter = new ASCIIEncoding();

            string dataString = "Data to Sign";

            // Create byte arrays to hold original, encrypted, and decrypted data.
            byte[] originalData = ByteConverter.GetBytes(dataString);
            byte[] signedData;
            byte[] smallArray;

            // Create a new instance of the RSACryptoServiceProvider class 
            // and automatically create a new key-pair.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            // Export the key information to an RSAParameters object.
            // You must pass true to export the private key for signing.
            // However, you do not need to export the private key
            // for verification.
            RSAParameters Key = RSAalg.ExportParameters(true);

            // Hash and sign the data.  Start at the fifth offset
            // only use data from the next 7 bytes.
            signedData = HashAndSignBytes(originalData, Key, 5, 7 );

            // The previous method only signed one segment
            // of the array.  Create a new array for verification
            // that only holds the data that was actually signed.
            //
            // Initialize the array.
            smallArray = new byte[7];
            // Copy 7 bytes starting at the 5th index to 
            // the new array.
            Array.Copy(originalData, 5 , smallArray, 0, 7); 

            // Verify the data and display the result to the 
            // console.  
            if(VerifySignedHash(smallArray, signedData, Key))
            {
                Console.WriteLine("The data was verified.");
            }
            else
            {
                Console.WriteLine("The data does not match the signature.");
            }

        }
        catch(ArgumentNullException)
        {
            Console.WriteLine("The data was not signed or verified");

        }
    }
    public static byte[] HashAndSignBytes(byte[] DataToSign, RSAParameters Key, int Index, int Length)
    {
        try
        {   
            // Create a new instance of RSACryptoServiceProvider using the 
            // key from RSAParameters.  
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            RSAalg.ImportParameters(Key);

            // Hash and sign the data. Pass a new instance of SHA1CryptoServiceProvider
            // to specify the use of SHA1 for hashing.
            return RSAalg.SignData(DataToSign,Index,Length, new SHA1CryptoServiceProvider());
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return null;
        }
    }

    public static bool VerifySignedHash(byte[] DataToVerify, byte[] SignedData, RSAParameters Key)
    {
        try
        {
            // Create a new instance of RSACryptoServiceProvider using the 
            // key from RSAParameters.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            RSAalg.ImportParameters(Key);

            // Verify the data using the signature.  Pass a new instance of SHA1CryptoServiceProvider
            // to specify the use of SHA1 for hashing.
            return RSAalg.VerifyData(DataToVerify, new SHA1CryptoServiceProvider(), SignedData); 

        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return false;
        }
    }
}

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

参照

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