Partager via


Génération de signatures

Les signatures numériques s'appliquent habituellement aux valeurs de hachage qui représentent des données plus grandes. L'exemple suivant applique une signature numérique à une valeur de hachage. D'abord, une nouvelle instance de la classe RSACryptoServiceProvider est créée pour générer une paire de clés publique/privée. Puis, RSACryptoServiceProvider est passé à une nouvelle instance de la classe RSAPKCS1SignatureFormatter. Cela transfère la clé privée à RSAPKCS1SignatureFormatter qui effectue la signature numérique. Avant de pouvoir signer le code de hachage, vous devez spécifier un algorithme de hachage à utiliser. Cet exemple utilise l'algorithme SHA1. Enfin, la méthode RSAPKCS1SignatureFormatter.CreateSignature est appelée pour effectuer la signature.

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

Signature de fichiers XML

Le .NET Framework fournit l'espace de noms System.Security.Cryptography.Xml qui vous permet de signer XML. La signature XML est importante lorsque vous voulez vérifier que le code XML provient d'une certaine source. Par exemple, si vous utilisez un service de cotation en bourse qui utilise XML, vous pouvez vérifier la source du code XML si celle-ci est signée.

Les classes dans cet espace de noms suivent les recommandations du World Wide Web Consortium, « XML-Signature Syntax and Processing », décrites à l'adresse suivante : www.w3.org.

Voir aussi

Concepts

Vérification de signatures
Signatures de chiffrement

Autres ressources

Tâches de chiffrement
Services de chiffrement