Compartir a través de


Generar firmas

Las firmas digitales se suelen aplicar a valores hash que representan datos mayores. En el ejemplo siguiente se aplica una firma digital a un valor hash. En primer lugar, se crea una nueva instancia de la clase RSACryptoServiceProvider para generar un par de claves pública y privada. A continuación, RSACryptoServiceProvider se pasa a una nueva instancia de la clase RSAPKCS1SignatureFormatter. De esta forma se transfiere la clave privada a RSAPKCS1SignatureFormatter, que realmente realiza la firma digital. Para firmar el código hash, debe especificar el algoritmo hash que se utilizará. En este ejemplo se utiliza el algoritmo SHA1. Por último, se llama al método RSAPKCS1SignatureFormatter.CreateSignature para realizar la firma.

Imports System
Imports System.Security.Cryptography

Module Module1
    Sub Main()
        'The hash value to sign.
        Dim HashValue As Byte() = {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.
        Dim SignedHashValue() As Byte

        'Generate a public/private key pair.
        Dim RSA As New RSACryptoServiceProvider()

        'Create an RSAPKCS1SignatureFormatter object and pass it 
        'the RSACryptoServiceProvider to transfer the private key.
        Dim RSAFormatter As New RSAPKCS1SignatureFormatter(RSA)

        'Set the hash algorithm to SHA1.
        RSAFormatter.SetHashAlgorithm("SHA1")

        'Create a signature for HashValue and assign it to 
        'SignedHashValue.
        SignedHashValue = RSAFormatter.CreateSignature(HashValue)
    End Sub
End Module

using System;
using System.Security.Cryptography;
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.
      RSAFormatter.SetHashAlgorithm("SHA1");

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

Firmar archivos XML

.NET Framework proporciona el espacio de nombres System.Security.Cryptography.XML, que permite firmar XML. La firma de XML es importante cuando se desea comprobar que el XML se origina en cierta fuente. Por ejemplo, si utiliza un servicio de cotización en bolsa que utiliza XML, puede comprobar el origen del XML si está firmado.

Las clases de este espacio de nombres siguen la recomendación del W3C (World Wide Web Consortium) "XML-Signature Syntax and Processing", descrito en www.w3.org.

Vea también

Conceptos

Comprobar firmas
Firmas criptográficas

Otros recursos

Tareas criptográficas
Servicios criptográficos