RSAPKCS1SignatureFormatter.CreateSignature(Byte[]) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立所指定資料的 RSA PKCS #1 簽章。
public:
override cli::array <System::Byte> ^ CreateSignature(cli::array <System::Byte> ^ rgbHash);
public override byte[] CreateSignature (byte[] rgbHash);
override this.CreateSignature : byte[] -> byte[]
Public Overrides Function CreateSignature (rgbHash As Byte()) As Byte()
參數
- rgbHash
- Byte[]
要簽署的資料。
傳回
Byte[]
rgbHash
的數位簽章。
例外狀況
rgbHash
參數為 null
。
範例
下列範例示範如何建立 PKCS #1 1.5 版簽章。
using System;
using System.Security.Cryptography;
class RSASample
{
static void Main()
{
try
{
//Create a new instance of RSA.
using (RSA rsa = RSA.Create())
{
//The hash to sign.
byte[] hash;
using (SHA256 sha256 = SHA256.Create())
{
byte[] data = new byte[] { 59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135 };
hash = sha256.ComputeHash(data);
}
//Create an RSASignatureFormatter object and pass it the
//RSA instance to transfer the key information.
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(rsa);
//Set the hash algorithm to SHA256.
RSAFormatter.SetHashAlgorithm("SHA256");
//Create a signature for HashValue and return it.
byte[] SignedHash = RSAFormatter.CreateSignature(hash);
}
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
}
}
Imports System.Security.Cryptography
Friend Class RSASample
Shared Sub Main()
Try
'Create a new instance of RSA.
Using rsa As RSA = RSA.Create()
'The hash to sign.
Dim hash() As Byte
Using sha256 As SHA256 = SHA256.Create()
Dim data() As Byte = { 59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135 }
hash = sha256.ComputeHash(data)
End Using
'Create an RSASignatureFormatter object and pass it the
'RSA to transfer the key information.
Dim RSAFormatter As New RSAPKCS1SignatureFormatter(rsa)
'Set the hash algorithm to SHA256.
RSAFormatter.SetHashAlgorithm("SHA256")
'Create a signature for HashValue and return it.
Dim SignedHash() As Byte = RSAFormatter.CreateSignature(hash)
End Using
Catch e As CryptographicException
Console.WriteLine(e.Message)
End Try
End Sub
End Class
備註
您必須先指定索引鍵和哈希演算法,才能呼叫這個方法。