Export (0) Print
Expand All

RSACryptoServiceProvider.SignData Method (Byte[], Int32, Int32, Object)

Computes the hash value of a subset of the specified byte array using the specified hash algorithm, and signs the resulting hash value.

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)

public byte[] SignData (
	byte[] buffer,
	int offset,
	int count,
	Object halg
)
public byte[] SignData (
	byte[] buffer, 
	int offset, 
	int count, 
	Object halg
)
public function SignData (
	buffer : byte[], 
	offset : int, 
	count : int, 
	halg : Object
) : byte[]

Parameters

buffer

The input data for which to compute the hash.

offset

The offset into the array from which to begin using data.

count

The number of bytes in the array to use as data.

halg

The hash algorithm to use to create the hash value.

Return Value

The RSA signature for the specified data.

Exception typeCondition

ArgumentNullException

The halg parameter is a null reference (Nothing in Visual Basic).

ArgumentException

The halg parameter is not a valid type.

This method creates a digital signature that is verified using the VerifyData method.

The halg parameter can accept a String, a HashAlgorithm, or a Type. The string value can be one of the following:

  • The object identifier (OID) friendly name of the hash algorithm to use, either a name registered in the crypto config file or one in the Crypto API OID table.

  • The OID value. The OID must be one recognized by the Crypto API.

For example, you could use SignData(new byte[5], "1.3.14.3.2.26") or SignData(new byte[5], "sha1"), or SignData(new byte[5], "SHA1").

The following code example signs and verifies data.

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;
        }
    }
}

import System.*;
import System.Security.Cryptography.*;
import System.Text.*;

class RSACSPSample
{
    public static void main(String[] args)
    {
        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.
            ubyte originalData[] = byteConverter.GetBytes(dataString);
            ubyte signedData[];
            ubyte 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 ubyte[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 exp) {
            Console.WriteLine("The data was not signed or verified");
        }
    } //main

    public static ubyte[] HashAndSignBytes(ubyte 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.get_Message());
            return null;
        }
    } //HashAndSignBytes

    public static boolean VerifySignedHash(ubyte dataToVerify[], 
        ubyte 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.get_Message());
            return false;
        }
    } //VerifySignedHash
} //RSACSPSample

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft