英語で読む

次の方法で共有


RSACryptoServiceProvider.SignData メソッド

定義

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

オーバーロード

SignData(Byte[], Object)

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

SignData(Stream, Object)

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

SignData(Byte[], Int32, Int32, Object)

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

SignData(Byte[], Object)

ソース:
RSACryptoServiceProvider.Unix.cs
ソース:
RSACryptoServiceProvider.Unix.cs
ソース:
RSACryptoServiceProvider.Unix.cs

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

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

パラメーター

buffer
Byte[]

ハッシュおよび署名する入力データ。

halg
Object

ハッシュ値を作成するために使用するハッシュ アルゴリズム。

戻り値

Byte[]

指定されたデータの RSA 署名。

例外

halg パラメーターが null です。

halg パラメーターが有効な型ではありません。

次のコード例では、データに署名して検証します。

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;

            // 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.
            signedData = HashAndSignBytes(originalData, Key);

            // Verify the data and display the result to the
            // console.
            if(VerifySignedHash(originalData, 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)
    {
        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 SHA256
            // to specify the hashing algorithm.
            return RSAalg.SignData(DataToSign, SHA256.Create());
        }
        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 SHA256
            // to specify the hashing algorithm.
            return RSAalg.VerifyData(DataToVerify, SHA256.Create(), SignedData);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return false;
        }
    }
}

注釈

このメソッドは、 メソッドを使用して検証されるデジタル署名を VerifyData 作成します。

パラメーターはhalg、または をStringHashAlgorithmType受け取ることができます。

こちらもご覧ください

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

SignData(Stream, Object)

ソース:
RSACryptoServiceProvider.Unix.cs
ソース:
RSACryptoServiceProvider.Unix.cs
ソース:
RSACryptoServiceProvider.Unix.cs

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

C#
public byte[] SignData(System.IO.Stream inputStream, object halg);

パラメーター

inputStream
Stream

ハッシュおよび署名する入力ストリーム。

halg
Object

ハッシュ値を作成するために使用するハッシュ アルゴリズム。

戻り値

Byte[]

指定されたデータの RSA 署名。

例外

halg パラメーターが null です。

halg パラメーターが有効な型ではありません。

次のコード例では、データに署名して検証します。

C#
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

class RSACSPSample
{
    static void Main()
    {
        try
        {
            ASCIIEncoding ByteConverter = new ASCIIEncoding();

            // Create some bytes to be signed.
            byte[] dataBytes = ByteConverter.GetBytes("Here is some data to sign!");

            // Create a buffer for the memory stream.
            byte[] buffer = new byte[dataBytes.Length];

            // Create a MemoryStream.
            MemoryStream mStream = new MemoryStream(buffer);

            // Write the bytes to the stream and flush it.
            mStream.Write(dataBytes, 0, dataBytes.Length);

            mStream.Flush();

            // 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.
            byte[] signedData = HashAndSignBytes(mStream, Key);

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

            // Close the MemoryStream.
            mStream.Close();
        }
        catch(ArgumentNullException)
        {
            Console.WriteLine("The data was not signed or verified");
        }
    }
    public static byte[] HashAndSignBytes(Stream DataStream, RSAParameters Key)
    {
        try
        {
            // Reset the current position in the stream to
            // the beginning of the stream (0). RSACryptoServiceProvider
            // can't verify the data unless the stream position
            // is set to the starting position of the data.
            DataStream.Position = 0;

            // 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 SHA256
            // to specify the hashing algorithm.
            return RSAalg.SignData(DataStream, SHA256.Create());
        }
        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 SHA256
            // to specify the hashing algorithm.
            return RSAalg.VerifyData(DataToVerify, SHA256.Create(), SignedData);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return false;
        }
    }
}

注釈

パラメーターはhalg、または をStringHashAlgorithmType受け取ることができます。

こちらもご覧ください

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

SignData(Byte[], Int32, Int32, Object)

ソース:
RSACryptoServiceProvider.Unix.cs
ソース:
RSACryptoServiceProvider.Unix.cs
ソース:
RSACryptoServiceProvider.Unix.cs

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

C#
public byte[] SignData(byte[] buffer, int offset, int count, object halg);

パラメーター

buffer
Byte[]

ハッシュおよび署名する入力データ。

offset
Int32

配列内のデータの使用開始位置を示すオフセット。

count
Int32

配列内でデータとして使用されるバイトの数。

halg
Object

ハッシュ値を作成するために使用するハッシュ アルゴリズム。

戻り値

Byte[]

指定されたデータの RSA 署名。

例外

halg パラメーターが null です。

halg パラメーターが有効な型ではありません。

次のコード例では、データに署名して検証します。

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 SHA256
            // to specify the hashing algorithm.
            return RSAalg.SignData(DataToSign,Index,Length, SHA256.Create());
        }
        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 SHA256
            // to specify the hashing algorithm.
            return RSAalg.VerifyData(DataToVerify, SHA256.Create(), SignedData);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return false;
        }
    }
}

注釈

このメソッドは、 メソッドを使用して検証されるデジタル署名を VerifyData 作成します。

パラメーターはhalg、または をStringHashAlgorithmType受け取ることができます。 文字列値には、次のいずれかを指定できます。

  • 使用するハッシュ アルゴリズムのオブジェクト識別子 (OID) フレンドリ名。暗号化構成ファイルに登録されている名前、または Crypto API OID テーブルに登録されている名前のいずれかです。

  • OID の値。 OID は、Crypto API によって認識される OID である必要があります。

たとえば、SignData(new byte[5], "1.3.14.3.2.26") または SignData(new byte[5], "sha1") または SignData(new byte[5], "SHA1") を使用できます。

こちらもご覧ください

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1