Condividi tramite


Procedura: accedere ai dispositivi di crittografia hardware

È possibile utilizzare la classe CspParameters per accedere ai dispositivi di crittografia hardware. Tale classe può essere impiegata, ad esempio, per integrare l'applicazione in uso con una smart card, un generatore di numeri casuali hardware o per l'implementazione hardware di un determinato algoritmo di crittografia.

La classe CspParameters crea un provider del servizio di crittografia (CSP, Cryptographic Service Provider) che accede a un dispositivo di crittografia hardware installato correttamente. È possibile verificare la disponibilità di un CSP esaminando la chiave del Registro di sistema HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider mediante l'Editor del Registro di sistema (Regedit.exe).

Per firmare i dati mediante una scheda di chiavi

  1. Creare una nuova istanza della classe CspParameters, passando il tipo di provider integer e il nome del provider al costruttore.

  2. Passare i flag appropriati alla proprietà Flags dell'oggetto CspParameters appena creato. Passare, ad esempio, il flag UseDefaultKeyContainer.

  3. Creare una nuova istanza di una classe AsymmetricAlgorithm (ad esempio, la classe RSACryptoServiceProvider), passando l'oggetto CspParameters al costruttore.

  4. Firmare e verificare i dati utilizzando rispettivamente i metodi Sign e Verify.

Per generare un numero casuale utilizzando un generatore di numeri casuali hardware

  1. Creare una nuova istanza della classe CspParameters, passando il tipo di provider integer e il nome del provider al costruttore.

  2. Creare una nuova istanza di RNGCryptoServiceProvider, passando l'oggetto CspParameters al costruttore.

  3. Creare un valore casuale utilizzando il metodo GetBytes o GetNonZeroBytes.

Esempio

Nell'esempio di codice riportato di seguito viene illustrato come firmare i dati mediante una smart card. Nell'esempio di codice viene creato un oggetto CspParameters che espone una smart card, quindi viene inizializzato un oggetto RSACryptoServiceProvider mediante il CSP. Dopodichè vengono firmati e verificati alcuni dati.

Imports System
Imports System.Security.Cryptography



Module SCSign

    Sub Main(ByVal args() As String)
        ' To idendify the Smart Card CryptoGraphic Providers on your
        ' computer, use the Microsoft Registry Editor (Regedit.exe).
        ' The available Smart Card CryptoGraphic Providers are listed
        ' in HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider.

        ' Create a new CspParameters object that identifies a 
        ' Smart Card CryptoGraphic Provider.
        ' The 1st parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider Types.
        ' The 2nd parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider.
        Dim csp As New CspParameters(1, "Schlumberger Cryptographic Service Provider")
        csp.Flags = CspProviderFlags.UseDefaultKeyContainer

        ' Initialize an RSACryptoServiceProvider object using
        ' the CspParameters object.
        Dim rsa As New RSACryptoServiceProvider(csp)

        ' Create some data to sign.
        Dim data() As Byte = {0, 1, 2, 3, 4, 5, 6, 7}


        Console.WriteLine("Data   : " + BitConverter.ToString(data))

        ' Sign the data using the Smart Card CryptoGraphic Provider.
        Dim sig As Byte() = rsa.SignData(data, "SHA1")

        Console.WriteLine("Signature : " + BitConverter.ToString(sig))

        ' Verify the data using the Smart Card CryptoGraphic Provider.
        Dim verified As Boolean = rsa.VerifyData(data, "SHA1", sig)

        Console.WriteLine("Verified")

    End Sub

End Module
using System;
using System.Security.Cryptography;

namespace SmartCardSign
{
    class SCSign
    {
        static void Main(string[] args)
        {
            // To idendify the Smart Card CryptoGraphic Providers on your
            // computer, use the Microsoft Registry Editor (Regedit.exe).
            // The available Smart Card CryptoGraphic Providers are listed
            // in HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider.


            // Create a new CspParameters object that identifies a 
            // Smart Card CryptoGraphic Provider.
            // The 1st parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider Types.
            // The 2nd parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider.
            CspParameters csp = new CspParameters(1, "Schlumberger Cryptographic Service Provider");
            csp.Flags = CspProviderFlags.UseDefaultKeyContainer;

            // Initialize an RSACryptoServiceProvider object using
            // the CspParameters object.
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);

            // Create some data to sign.
            byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };

            Console.WriteLine("Data         : " + BitConverter.ToString(data));

            // Sign the data using the Smart Card CryptoGraphic Provider.
            byte[] sig = rsa.SignData(data, "SHA1");

            Console.WriteLine("Signature    : " + BitConverter.ToString(sig));

            // Verify the data using the Smart Card CryptoGraphic Provider.
            bool verified = rsa.VerifyData(data, "SHA1", sig);

            Console.WriteLine("Verified     : " + verified);

        }
    }
}
using namespace System;
using namespace System::Security::Cryptography;
int main()
{

   // To idendify the Smart Card CryptoGraphic Providers on your
   // computer, use the Microsoft Registry Editor (Regedit.exe).
   // The available Smart Card CryptoGraphic Providers are listed
   // in HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider.
   // Create a new CspParameters object that identifies a 
   // Smart Card CryptoGraphic Provider.
   // The 1st parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider Types.
   // The 2nd parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider.
   CspParameters^ csp = gcnew CspParameters( 1,L"Schlumberger Cryptographic Service Provider" );
   csp->Flags = CspProviderFlags::UseDefaultKeyContainer;

   // Initialize an RSACryptoServiceProvider object using
   // the CspParameters object.
   RSACryptoServiceProvider^ rsa = gcnew RSACryptoServiceProvider( csp );

   // Create some data to sign.
   array<Byte>^data = gcnew array<Byte>{
      0,1,2,3,4,5,6,7
   };
   Console::WriteLine( L"Data           : {0}", BitConverter::ToString( data ) );

   // Sign the data using the Smart Card CryptoGraphic Provider.
   array<Byte>^sig = rsa->SignData( data, L"SHA1" );
   Console::WriteLine( L"Signature  : {0}", BitConverter::ToString( sig ) );

   // Verify the data using the Smart Card CryptoGraphic Provider.
   bool verified = rsa->VerifyData( data, L"SHA1", sig );
   Console::WriteLine( L"Verified       : {0}", verified );
}

Compilazione del codice

  • Includere gli spazi dei nomi System e System.Security.Cryptography.

  • È necessario disporre di un lettore di smart card e di driver installati nel computer.

  • È necessario inizializzare l'oggetto CspParameters utilizzando informazioni specifiche del lettore di smart card. Per ulteriori informazioni, vedere la documentazione relativa al lettore.

Vedere anche

Altre risorse

Attività di crittografia

Cronologia delle modifiche

Data

Cronologia

Motivo

Luglio 2010

Correzione del nome del file eseguibile dell'editor del Registro di sistema.

Commenti e suggerimenti dei clienti.