RSACryptoServiceProvider.SignData メソッド (Byte , Int32, Int32, Object)
指定したハッシュ アルゴリズムを使用して、指定したバイト配列のサブセットのハッシュ値を計算し、結果のハッシュ値に署名します。
Overloads Public Function SignData( _
ByVal buffer() As Byte, _ ByVal offset As Integer, _ ByVal count As Integer, _ ByVal halg As Object _) As Byte()
[C#]
public byte[] SignData(byte[] buffer,intoffset,intcount,objecthalg);
[C++]
public: unsigned char SignData(unsigned charbuffer __gc[],intoffset,intcount,Object* halg) __gc[];
[JScript]
public function SignData(
buffer : Byte[],offset : int,count : int,halg : Object) : Byte[];
パラメータ
- buffer
ハッシュを計算する対象の入力データ。 - offset
使用するデータの先頭位置を示す、配列内のオフセット。 - count
配列内でデータとして使用されるバイトの数。 - halg
ハッシュ値の作成に使用するハッシュ アルゴリズム。
戻り値
指定したデータの RSA 署名。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | halg パラメータが null 参照 (Visual Basic では Nothing) です。 |
ArgumentException | halg パラメータの型が有効ではありません。 |
解説
このメソッドは、デジタル署名を作成します。作成した署名は VerifyData メソッドを使用して検証されます。
halg パラメータには、 String 、 HashAlgorithm 、または Type を使用できます。
使用例
[Visual Basic, C#] SignData メソッドを使用する方法を次の例に示します。
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# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
RSACryptoServiceProvider クラス | RSACryptoServiceProvider メンバ | System.Security.Cryptography 名前空間 | RSACryptoServiceProvider.SignData オーバーロードの一覧 | 暗号サービス