Export (0) Print
Expand All

Cryptographic Signatures

Cryptographic digital signatures use public key algorithms to provide data integrity. When you sign data with a digital signature, someone else can verify the signature, and can prove that the data originated from you and was not altered after you signed it. For more information about digital signatures, see Cryptographic Services.

This topic explains how to generate and verify digital signatures using classes in the System.Security.Cryptography namespace.

Digital signatures are usually applied to hash values that represent larger data. The following example applies a digital signature to a hash value. First, a new instance of the RSACryptoServiceProvider class is created to generate a public/private key pair. Next, the RSACryptoServiceProvider is passed to a new instance of the RSAPKCS1SignatureFormatter class. This transfers the private key to the RSAPKCS1SignatureFormatter, which actually performs the digital signing. Before you can sign the hash code, you must specify a hash algorithm to use. This example uses the SHA1 algorithm. Finally, the RSAPKCS1SignatureFormatter.CreateSignature method is called to perform the signing.

class Class1
   static void Main()
      //The hash value to sign.
      byte[] HashValue = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135};

      //The value to hold the signed value.
      byte[] SignedHashValue;

      //Generate a public/private key pair.
      RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

      //Create an RSAPKCS1SignatureFormatter object and pass it the 
      //RSACryptoServiceProvider to transfer the private key.
      RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);

      //Set the hash algorithm to SHA1.

      //Create a signature for HashValue and assign it to 
      SignedHashValue = RSAFormatter.CreateSignature(HashValue);

Signing XML Files

The .NET Framework provides the System.Security.Cryptography.XML namespace, which enables you sign XML. Signing XML is important when you want to verify that the XML originates from a certain source. For example, if you are using a stock quote service that uses XML, you can verify the source of the XML if it is signed.

The classes in this namespace follow the XML-Signature Syntax and Processing recommendation from the World Wide Web Consortium.

Back to top

To verify that data was signed by a particular party, you must have the following information:

  • The public key of the party that signed the data.

  • The digital signature.

  • The data that was signed.

  • The hash algorithm used by the signer.

To verify a signature signed by the RSAPKCS1SignatureFormatter class, use the RSAPKCS1SignatureDeformatter class. The RSAPKCS1SignatureDeformatter class must be supplied the public key of the signer. You will need the values of the modulus and the exponent to specify the public key. (The party that generated the public/private key pair should provide these values.) First create an RSACryptoServiceProvider object to hold the public key that will verify the signature, and then initialize an RSAParameters structure to the modulus and exponent values that specify the public key.

The following code shows the creation of an RSAParameters structure. The Modulus property is set to the value of a byte array called ModulusData and the Exponent property is set to the value of a byte array called ExponentData.

RSAParameters RSAKeyInfo;
RSAKeyInfo.Modulus = ModulusData;
RSAKeyInfo.Exponent = ExponentData;

After you have created the RSAParameters object, you can initialize a new instance of the RSACryptoServiceProvider class to the values specified in RSAParameters. The RSACryptoServiceProvider is, in turn, passed to the constructor of an RSAPKCS1SignatureDeformatter to transfer the key.

The following example illustrates this process. In this example, HashValue and SignedHashValue are arrays of bytes provided by a remote party. The remote party has signed the HashValue using the SHA1 algorithm, producing the digital signature SignedHashValue. The

RSAPKCS1SignatureDeformatter.VerifySignature method verifies that the digital signature is valid and was used to sign the HashValue.

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
if(RSADeformatter.VerifySignature(HashValue, SignedHashValue))
   Console.WriteLine("The signature is valid.");
   Console.WriteLine("The signature is not valid.");

This code fragment will display "The signature is valid" if the signature is valid and "The signature is not valid" if it is not.

Back to top

Community Additions

© 2014 Microsoft