RSACryptoServiceProvider.SignHash 方法

定義

計算指定的雜湊值簽章。

多載

SignHash(Byte[], String)

計算指定的雜湊值簽章。

SignHash(Byte[], HashAlgorithmName, RSASignaturePadding)

針對指定雜湊值的簽章,使用指定的填補來計算。

SignHash(Byte[], String)

來源:
RSACryptoServiceProvider.Unix.cs
來源:
RSACryptoServiceProvider.Unix.cs
來源:
RSACryptoServiceProvider.Unix.cs

計算指定的雜湊值簽章。

public:
 cli::array <System::Byte> ^ SignHash(cli::array <System::Byte> ^ rgbHash, System::String ^ str);
public byte[] SignHash (byte[] rgbHash, string? str);
public byte[] SignHash (byte[] rgbHash, string str);
override this.SignHash : byte[] * string -> byte[]
member this.SignHash : byte[] * string -> byte[]
Public Function SignHash (rgbHash As Byte(), str As String) As Byte()

參數

rgbHash
Byte[]

要簽署之資料的雜湊值。

str
String

用以建立該資料雜湊值的雜湊演算法識別項 (OID)。

傳回

Byte[]

指定雜湊值的 RSA 簽章。

例外狀況

rgbHash 參數為 null

無法取得密碼編譯服務提供者 (CSP)。

-或-

沒有私密金鑰。

範例

下列程式碼範例會加密某些資料、建立加密資料的雜湊,然後使用數位簽章簽署雜湊。

// This example uses the SHA1 algorithm.
// Due to collision problems with SHA1, Microsoft recommends SHA256 or better.
#using <System.dll>

using namespace System;
using namespace System::Text;
using namespace System::Security::Cryptography;
ref class Sender
{
private:
   RSAParameters rsaPubParams;
   RSAParameters rsaPrivateParams;

public:
   Sender()
   {
      RSACryptoServiceProvider^ rsaCSP = gcnew RSACryptoServiceProvider;
      
      //Generate public and private key data.
      rsaPrivateParams = rsaCSP->ExportParameters( true );
      rsaPubParams = rsaCSP->ExportParameters( false );
   }


   property RSAParameters PublicParameters 
   {
      RSAParameters get()
      {
         return rsaPubParams;
      }

   }

   //Manually performs hash and then signs hashed value.
   array<Byte>^ HashAndSign( array<Byte>^encrypted )
   {
      RSACryptoServiceProvider^ rsaCSP = gcnew RSACryptoServiceProvider;
      SHA1Managed^ hash = gcnew SHA1Managed;
      array<Byte>^hashedData;
      rsaCSP->ImportParameters( rsaPrivateParams );
      hashedData = hash->ComputeHash( encrypted );
      return rsaCSP->SignHash( hashedData, CryptoConfig::MapNameToOID( "SHA1" ) );
   }


   //Encrypts using only the public key data.
   array<Byte>^ EncryptData( RSAParameters rsaParams, array<Byte>^toEncrypt )
   {
      RSACryptoServiceProvider^ rsaCSP = gcnew RSACryptoServiceProvider;
      rsaCSP->ImportParameters( rsaParams );
      return rsaCSP->Encrypt( toEncrypt, false );
   }

};

ref class Receiver
{
private:
   RSAParameters rsaPubParams;
   RSAParameters rsaPrivateParams;

public:
   Receiver()
   {
      RSACryptoServiceProvider^ rsaCSP = gcnew RSACryptoServiceProvider;
      
      //Generate public and private key data.
      rsaPrivateParams = rsaCSP->ExportParameters( true );
      rsaPubParams = rsaCSP->ExportParameters( false );
   }


   property RSAParameters PublicParameters 
   {
      RSAParameters get()
      {
         return rsaPubParams;
      }

   }

   //Manually performs hash and then verifies hashed value.
   bool VerifyHash( RSAParameters rsaParams, array<Byte>^signedData, array<Byte>^signature )
   {
      RSACryptoServiceProvider^ rsaCSP = gcnew RSACryptoServiceProvider;
      SHA1Managed^ hash = gcnew SHA1Managed;
      array<Byte>^hashedData;
      rsaCSP->ImportParameters( rsaParams );
      bool dataOK = rsaCSP->VerifyData(signedData, CryptoConfig::MapNameToOID("SHA1"), signature);
      hashedData = hash->ComputeHash( signedData );
      return rsaCSP->VerifyHash( hashedData, CryptoConfig::MapNameToOID( "SHA1" ), signature );
   }


   //Decrypt using the private key data.
   void DecryptData( array<Byte>^encrypted )
   {
      array<Byte>^fromEncrypt;
      String^ roundTrip;
      ASCIIEncoding^ myAscii = gcnew ASCIIEncoding;
      RSACryptoServiceProvider^ rsaCSP = gcnew RSACryptoServiceProvider;
      rsaCSP->ImportParameters( rsaPrivateParams );
      fromEncrypt = rsaCSP->Decrypt( encrypted, false );
      roundTrip = myAscii->GetString( fromEncrypt );
      Console::WriteLine( "RoundTrip: {0}", roundTrip );
   }

};

int main()
{
   array<Byte>^toEncrypt;
   array<Byte>^encrypted;
   array<Byte>^signature;
   
   //Choose a small amount of data to encrypt.
   String^ original = "Hello";
   ASCIIEncoding^ myAscii = gcnew ASCIIEncoding;
   
   //Create a sender and receiver.
   Sender^ mySender = gcnew Sender;
   Receiver^ myReceiver = gcnew Receiver;
   
   //Convert the data string to a byte array.
   toEncrypt = myAscii->GetBytes( original );
   
   //Encrypt data using receiver's public key.
   encrypted = mySender->EncryptData( myReceiver->PublicParameters, toEncrypt );
   
   //Hash the encrypted data and generate a signature on the hash
   // using the sender's private key.
   signature = mySender->HashAndSign( encrypted );
   Console::WriteLine( "Original: {0}", original );
   
   //Verify the signature is authentic using the sender's public key.
   if ( myReceiver->VerifyHash( mySender->PublicParameters, encrypted, signature ) )
   {
      
      //Decrypt the data using the receiver's private key.
      myReceiver->DecryptData( encrypted );
   }
   else
   {
      Console::WriteLine( "Invalid signature" );
   }
}
// This example uses the SHA1 algorithm.
// Due to collision problems with SHA1, Microsoft recommends SHA256 or better.
using System;
using System.Text;
using System.Security.Cryptography;

namespace RSACryptoServiceProvider_Examples
{
    class MyMainClass
    {
        static void Main()
        {
            byte[] toEncrypt;
            byte[] encrypted;
            byte[] signature;
            //Choose a small amount of data to encrypt.
            string original = "Hello";
            ASCIIEncoding myAscii = new ASCIIEncoding();

            //Create a sender and receiver.
            Sender mySender = new Sender();
            Receiver myReceiver = new Receiver();

            //Convert the data string to a byte array.
            toEncrypt = myAscii.GetBytes(original);

            //Encrypt data using receiver's public key.
            encrypted = mySender.EncryptData(myReceiver.PublicParameters, toEncrypt);

            //Hash the encrypted data and generate a signature on the hash
            // using the sender's private key.
            signature = mySender.HashAndSign(encrypted);

            Console.WriteLine("Original: {0}", original);

            //Verify the signature is authentic using the sender's public key.
            if (myReceiver.VerifyHash(mySender.PublicParameters, encrypted, signature))
            {
                //Decrypt the data using the receiver's private key.
                myReceiver.DecryptData(encrypted);
            }
            else
            {
                Console.WriteLine("Invalid signature");
            }
        }
    }

    class Sender
    {
        RSAParameters rsaPubParams;
        RSAParameters rsaPrivateParams;

        public Sender()
        {
            RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();

            //Generate public and private key data.
            rsaPrivateParams = rsaCSP.ExportParameters(true);
            rsaPubParams = rsaCSP.ExportParameters(false);
        }

        public RSAParameters PublicParameters
        {
            get
            {
                return rsaPubParams;
            }
        }

        //Manually performs hash and then signs hashed value.
        public byte[] HashAndSign(byte[] encrypted)
        {
            RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();
            SHA1Managed hash = new SHA1Managed();
            byte[] hashedData;

            rsaCSP.ImportParameters(rsaPrivateParams);

            hashedData = hash.ComputeHash(encrypted);
            return rsaCSP.SignHash(hashedData, CryptoConfig.MapNameToOID("SHA1"));
        }

        //Encrypts using only the public key data.
        public byte[] EncryptData(RSAParameters rsaParams, byte[] toEncrypt)
        {
            RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();

            rsaCSP.ImportParameters(rsaParams);
            return rsaCSP.Encrypt(toEncrypt, false);
        }
    }

    class Receiver
    {
        RSAParameters rsaPubParams;
        RSAParameters rsaPrivateParams;

        public Receiver()
        {
            RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();

            //Generate public and private key data.
            rsaPrivateParams = rsaCSP.ExportParameters(true);
            rsaPubParams = rsaCSP.ExportParameters(false);
        }

        public RSAParameters PublicParameters
        {
            get
            {
                return rsaPubParams;
            }
        }

        //Manually performs hash and then verifies hashed value.
        public bool VerifyHash(RSAParameters rsaParams, byte[] signedData, byte[] signature)
        {
            RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();
            SHA1Managed hash = new SHA1Managed();
            byte[] hashedData;

            rsaCSP.ImportParameters(rsaParams);
            bool dataOK = rsaCSP.VerifyData(signedData, CryptoConfig.MapNameToOID("SHA1"), signature);
            hashedData = hash.ComputeHash(signedData);
            return rsaCSP.VerifyHash(hashedData, CryptoConfig.MapNameToOID("SHA1"), signature);
        }

        //Decrypt using the private key data.
        public void DecryptData(byte[] encrypted)
        {
            byte[] fromEncrypt;
            string roundTrip;
            ASCIIEncoding myAscii = new ASCIIEncoding();
            RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();

            rsaCSP.ImportParameters(rsaPrivateParams);
            fromEncrypt = rsaCSP.Decrypt(encrypted, false);
            roundTrip = myAscii.GetString(fromEncrypt);

            Console.WriteLine("RoundTrip: {0}", roundTrip);
        }
    }
}
' This example uses the SHA1 algorithm.
' Due to collision problems with SHA1, Microsoft recommends SHA256 or better.
Imports System.Text
Imports System.Security.Cryptography

Namespace RSACryptoServiceProvider_Examples
    Class MyMainClass
        Shared Sub Main()
            Dim toEncrypt() As Byte
            Dim encrypted() As Byte
            Dim signature() As Byte
            'Choose a small amount of data to encrypt.
            Dim original As String = "Hello"
            Dim myAscii As New ASCIIEncoding()

            'Create a sender and receiver.
            Dim mySender As New Sender()
            Dim myReceiver As New Receiver()

            'Convert the data string to a byte array.
            toEncrypt = myAscii.GetBytes(original)

            'Encrypt data using receiver's public key.
            encrypted = mySender.EncryptData(myReceiver.PublicParameters, toEncrypt)

            'Hash the encrypted data and generate a signature on the hash
            ' using the sender's private key.
            signature = mySender.HashAndSign(encrypted)

            Console.WriteLine("Original: {0}", original)

            'Verify the signature is authentic using the sender's public key.
            If myReceiver.VerifyHash(mySender.PublicParameters, encrypted, signature) Then
                'Decrypt the data using the receiver's private key.
                myReceiver.DecryptData(encrypted)
            Else
                Console.WriteLine("Invalid signature")
            End If
        End Sub
    End Class

    Class Sender
        Private rsaPubParams As RSAParameters
        Private rsaPrivateParams As RSAParameters

        Public Sub New()
            Dim rsaCSP As New RSACryptoServiceProvider()

            'Generate public and private key data.
            rsaPrivateParams = rsaCSP.ExportParameters(True)
            rsaPubParams = rsaCSP.ExportParameters(False)
        End Sub

        Public ReadOnly Property PublicParameters() As RSAParameters
            Get
                Return rsaPubParams
            End Get
        End Property

        'Manually performs hash and then signs hashed value.
        Public Function HashAndSign(ByVal encrypted() As Byte) As Byte()
            Dim rsaCSP As New RSACryptoServiceProvider()
            Dim hash As New SHA1Managed()
            Dim hashedData() As Byte

            rsaCSP.ImportParameters(rsaPrivateParams)

            hashedData = hash.ComputeHash(encrypted)
            Return rsaCSP.SignHash(hashedData, CryptoConfig.MapNameToOID("SHA1"))
        End Function 'HashAndSign

        'Encrypts using only the public key data.
        Public Function EncryptData(ByVal rsaParams As RSAParameters, ByVal toEncrypt() As Byte) As Byte()
            Dim rsaCSP As New RSACryptoServiceProvider()

            rsaCSP.ImportParameters(rsaParams)
            Return rsaCSP.Encrypt(toEncrypt, False)
        End Function 'EncryptData
    End Class

    Class Receiver
        Private rsaPubParams As RSAParameters
        Private rsaPrivateParams As RSAParameters

        Public Sub New()
            Dim rsaCSP As New RSACryptoServiceProvider()

            'Generate public and private key data.
            rsaPrivateParams = rsaCSP.ExportParameters(True)
            rsaPubParams = rsaCSP.ExportParameters(False)
        End Sub

        Public ReadOnly Property PublicParameters() As RSAParameters
            Get
                Return rsaPubParams
            End Get
        End Property

        'Manually performs hash and then verifies hashed value.
        Public Function VerifyHash(ByVal rsaParams As RSAParameters, ByVal signedData() As Byte, ByVal signature() As Byte) As Boolean
            Dim rsaCSP As New RSACryptoServiceProvider()
            Dim hash As New SHA1Managed()
            Dim hashedData() As Byte
            Dim dataOK As Boolean

            rsaCSP.ImportParameters(rsaParams)
            dataOK = rsaCSP.VerifyData(signedData, CryptoConfig.MapNameToOID("SHA1"), signature)
            hashedData = hash.ComputeHash(signedData)
            Return rsaCSP.VerifyHash(hashedData, CryptoConfig.MapNameToOID("SHA1"), signature)
        End Function 'VerifyHash

        'Decrypt using the private key data.
        Public Sub DecryptData(ByVal encrypted() As Byte)
            Dim fromEncrypt() As Byte
            Dim roundTrip As String
            Dim myAscii As New ASCIIEncoding()
            Dim rsaCSP As New RSACryptoServiceProvider()

            rsaCSP.ImportParameters(rsaPrivateParams)
            fromEncrypt = rsaCSP.Decrypt(encrypted, False)
            roundTrip = myAscii.GetString(fromEncrypt)

            Console.WriteLine("RoundTrip: {0}", roundTrip)
        End Sub
    End Class
End Namespace 'RSACryptoServiceProvider_Examples

備註

這個方法會建立使用 VerifyHash 方法驗證的數位簽章。

有效的雜湊演算法為 SHA1MD5 。 演算法識別碼可以使用 方法來衍生自雜湊名稱 MapNameToOID

由於 SHA1 和 MD5 發生衝突問題,Microsoft 建議根據 SHA256 或更好的安全性模型。

另請參閱

適用於

SignHash(Byte[], HashAlgorithmName, RSASignaturePadding)

來源:
RSACryptoServiceProvider.Unix.cs
來源:
RSACryptoServiceProvider.Unix.cs
來源:
RSACryptoServiceProvider.Unix.cs

針對指定雜湊值的簽章,使用指定的填補來計算。

public:
 override cli::array <System::Byte> ^ SignHash(cli::array <System::Byte> ^ hash, System::Security::Cryptography::HashAlgorithmName hashAlgorithm, System::Security::Cryptography::RSASignaturePadding ^ padding);
public override byte[] SignHash (byte[] hash, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, System.Security.Cryptography.RSASignaturePadding padding);
override this.SignHash : byte[] * System.Security.Cryptography.HashAlgorithmName * System.Security.Cryptography.RSASignaturePadding -> byte[]
Public Overrides Function SignHash (hash As Byte(), hashAlgorithm As HashAlgorithmName, padding As RSASignaturePadding) As Byte()

參數

hash
Byte[]

要簽署之資料的雜湊值。

hashAlgorithm
HashAlgorithmName

用來建立該資料雜湊值的雜湊演算法名稱。

padding
RSASignaturePadding

填補。

傳回

Byte[]

指定雜湊值的 RSA 簽章。

例外狀況

hashAlgorithmnullEmpty

hashnull

-或-

paddingnull

padding 不等於 Pkcs1

適用於