RSACryptoServiceProvider Konstruktory

Definice

Inicializuje novou instanci RSACryptoServiceProvider třídy.

Přetížení

RSACryptoServiceProvider()

Inicializuje novou instanci RSACryptoServiceProvider třídy s náhodným párem klíčů.

RSACryptoServiceProvider(Int32)

Inicializuje novou instanci RSACryptoServiceProvider třídy pomocí náhodného klíče páru zadané velikosti klíče.

RSACryptoServiceProvider(CspParameters)

Inicializuje novou instanci RSACryptoServiceProvider třídy se zadanými parametry.

RSACryptoServiceProvider(Int32, CspParameters)

Inicializuje novou instanci RSACryptoServiceProvider třídy se zadanou velikostí klíče a parametry.

RSACryptoServiceProvider()

Zdroj:
RSACryptoServiceProvider.Unix.cs
Zdroj:
RSACryptoServiceProvider.Unix.cs
Zdroj:
RSACryptoServiceProvider.Unix.cs

Inicializuje novou instanci RSACryptoServiceProvider třídy s náhodným párem klíčů.

public:
 RSACryptoServiceProvider();
public RSACryptoServiceProvider ();
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public RSACryptoServiceProvider ();
Public Sub New ()
Atributy

Výjimky

Zprostředkovatele kryptografických služeb (CSP) nelze získat.

Příklady

Následující příklad kódu používá RSACryptoServiceProvider třídu k šifrování řetězce do pole bajtů a následné dešifrování bajtů zpět do řetězce.

using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Text;
array<Byte>^ RSAEncrypt( array<Byte>^DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding )
{
   try
   {
      
      //Create a new instance of RSACryptoServiceProvider.
      RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
      
      //Import the RSA Key information. This only needs
      //toinclude the public key information.
      RSA->ImportParameters( RSAKeyInfo );
      
      //Encrypt the passed byte array and specify OAEP padding.  
      //OAEP padding is only available on Microsoft Windows XP or
      //later.  

      array<Byte>^encryptedData = RSA->Encrypt( DataToEncrypt, DoOAEPPadding );
      delete RSA;
      return encryptedData;
   }
   //Catch and display a CryptographicException  
   //to the console.
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
      return nullptr;
   }

}

array<Byte>^ RSADecrypt( array<Byte>^DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding )
{
   try
   {
      
      //Create a new instance of RSACryptoServiceProvider.
      RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
      
      //Import the RSA Key information. This needs
      //to include the private key information.
      RSA->ImportParameters( RSAKeyInfo );
      
      //Decrypt the passed byte array and specify OAEP padding.  
      //OAEP padding is only available on Microsoft Windows XP or
      //later.  
      
      array<Byte>^decryptedData = RSA->Decrypt( DataToDecrypt, DoOAEPPadding );
      delete RSA;
      return decryptedData;
   }
   //Catch and display a CryptographicException  
   //to the console.
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e );
      return nullptr;
   }

}

int main()
{
   try
   {
      
      //Create a UnicodeEncoder to convert between byte array and string.
      UnicodeEncoding^ ByteConverter = gcnew UnicodeEncoding;
      
      //Create byte arrays to hold original, encrypted, and decrypted data.
      array<Byte>^dataToEncrypt = ByteConverter->GetBytes( "Data to Encrypt" );
      array<Byte>^encryptedData;
      array<Byte>^decryptedData;
      
      //Create a new instance of RSACryptoServiceProvider to generate
      //public and private key data.
      RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
      
      //Pass the data to ENCRYPT, the public key information 
      //(using RSACryptoServiceProvider.ExportParameters(false),
      //and a boolean flag specifying no OAEP padding.
      encryptedData = RSAEncrypt( dataToEncrypt, RSA->ExportParameters( false ), false );
      
      //Pass the data to DECRYPT, the private key information 
      //(using RSACryptoServiceProvider.ExportParameters(true),
      //and a boolean flag specifying no OAEP padding.
      decryptedData = RSADecrypt( encryptedData, RSA->ExportParameters( true ), false );
      
      //Display the decrypted plaintext to the console. 
      Console::WriteLine( "Decrypted plaintext: {0}", ByteConverter->GetString( decryptedData ) );
      delete RSA;
   }
   catch ( ArgumentNullException^ ) 
   {
      
      //Catch this exception in case the encryption did
      //not succeed.
      Console::WriteLine( "Encryption failed." );
   }

}
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.
            UnicodeEncoding ByteConverter = new UnicodeEncoding();

            //Create byte arrays to hold original, encrypted, and decrypted data.
            byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
            byte[] encryptedData;
            byte[] decryptedData;

            //Create a new instance of RSACryptoServiceProvider to generate
            //public and private key data.
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
            {

                //Pass the data to ENCRYPT, the public key information 
                //(using RSACryptoServiceProvider.ExportParameters(false),
                //and a boolean flag specifying no OAEP padding.
                encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);

                //Pass the data to DECRYPT, the private key information 
                //(using RSACryptoServiceProvider.ExportParameters(true),
                //and a boolean flag specifying no OAEP padding.
                decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);

                //Display the decrypted plaintext to the console. 
                Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
            }
        }
        catch (ArgumentNullException)
        {
            //Catch this exception in case the encryption did
            //not succeed.
            Console.WriteLine("Encryption failed.");
        }
    }

    public static byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
    {
        try
        {
            byte[] encryptedData;
            //Create a new instance of RSACryptoServiceProvider.
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
            {

                //Import the RSA Key information. This only needs
                //to include the public key information.
                RSA.ImportParameters(RSAKeyInfo);

                //Encrypt the passed byte array and specify OAEP padding.  
                //OAEP padding is only available on Microsoft Windows XP or
                //later.  
                encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
            }
            return encryptedData;
        }
        //Catch and display a CryptographicException  
        //to the console.
        catch (CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return null;
        }
    }

    public static byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
    {
        try
        {
            byte[] decryptedData;
            //Create a new instance of RSACryptoServiceProvider.
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
            {
                //Import the RSA Key information. This needs
                //to include the private key information.
                RSA.ImportParameters(RSAKeyInfo);

                //Decrypt the passed byte array and specify OAEP padding.  
                //OAEP padding is only available on Microsoft Windows XP or
                //later.  
                decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
            }
            return decryptedData;
        }
        //Catch and display a CryptographicException  
        //to the console.
        catch (CryptographicException e)
        {
            Console.WriteLine(e.ToString());

            return null;
        }
    }
}
Imports System.Security.Cryptography
Imports System.Text

 _

Class RSACSPSample


    Shared Sub Main()
        Try
            'Create a UnicodeEncoder to convert between byte array and string.
            Dim ByteConverter As New UnicodeEncoding()

            'Create byte arrays to hold original, encrypted, and decrypted data.
            Dim dataToEncrypt As Byte() = ByteConverter.GetBytes("Data to Encrypt")
            Dim encryptedData() As Byte
            Dim decryptedData() As Byte

            'Create a new instance of RSACryptoServiceProvider to generate
            'public and private key data.
            Using RSA As New RSACryptoServiceProvider

                'Pass the data to ENCRYPT, the public key information 
                '(using RSACryptoServiceProvider.ExportParameters(false),
                'and a boolean flag specifying no OAEP padding.
                encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(False), False)

                'Pass the data to DECRYPT, the private key information 
                '(using RSACryptoServiceProvider.ExportParameters(true),
                'and a boolean flag specifying no OAEP padding.
                decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(True), False)

                'Display the decrypted plaintext to the console. 
                Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData))
            End Using
        Catch e As ArgumentNullException
            'Catch this exception in case the encryption did
            'not succeed.
            Console.WriteLine("Encryption failed.")
        End Try
    End Sub


    Public Shared Function RSAEncrypt(ByVal DataToEncrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            Dim encryptedData() As Byte
            'Create a new instance of RSACryptoServiceProvider.
            Using RSA As New RSACryptoServiceProvider

                'Import the RSA Key information. This only needs
                'toinclude the public key information.
                RSA.ImportParameters(RSAKeyInfo)

                'Encrypt the passed byte array and specify OAEP padding.  
                'OAEP padding is only available on Microsoft Windows XP or
                'later.  
                encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding)
            End Using
            Return encryptedData
            'Catch and display a CryptographicException  
            'to the console.
        Catch e As CryptographicException
            Console.WriteLine(e.Message)

            Return Nothing
        End Try
    End Function


    Public Shared Function RSADecrypt(ByVal DataToDecrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            Dim decryptedData() As Byte
            'Create a new instance of RSACryptoServiceProvider.
            Using RSA As New RSACryptoServiceProvider
                'Import the RSA Key information. This needs
                'to include the private key information.
                RSA.ImportParameters(RSAKeyInfo)

                'Decrypt the passed byte array and specify OAEP padding.  
                'OAEP padding is only available on Microsoft Windows XP or
                'later.  
                decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding)
                'Catch and display a CryptographicException  
                'to the console.
            End Using
            Return decryptedData
        Catch e As CryptographicException
            Console.WriteLine(e.ToString())

            Return Nothing
        End Try
    End Function
End Class

Poznámky

Tento konstruktor vytvoří Exchange pár klíčů vhodný k šifrování klíčů relace, aby je bylo možné bezpečně ukládat a vyměňovat s jinými uživateli. Vygenerovaný klíč odpovídá klíči vygenerovanému AT_KEYEXCHANGE pomocí hodnoty použité v nespravovaném kryptografickém rozhraní API microsoftu (CAPI).

Tento konstruktor negeneruje nový veřejný/privátní klíčpair okamžitě. Pokud metodou nebo jinou metodou importu ImportParameters klíče není načten žádný klíč, vytvoří se na vyžádání 1024bitový dočasný klíč.

Viz také

Platí pro

RSACryptoServiceProvider(Int32)

Zdroj:
RSACryptoServiceProvider.Unix.cs
Zdroj:
RSACryptoServiceProvider.Unix.cs
Zdroj:
RSACryptoServiceProvider.Unix.cs

Inicializuje novou instanci RSACryptoServiceProvider třídy pomocí náhodného klíče páru zadané velikosti klíče.

public:
 RSACryptoServiceProvider(int dwKeySize);
public RSACryptoServiceProvider (int dwKeySize);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public RSACryptoServiceProvider (int dwKeySize);
new System.Security.Cryptography.RSACryptoServiceProvider : int -> System.Security.Cryptography.RSACryptoServiceProvider
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
new System.Security.Cryptography.RSACryptoServiceProvider : int -> System.Security.Cryptography.RSACryptoServiceProvider
Public Sub New (dwKeySize As Integer)

Parametry

dwKeySize
Int32

Velikost klíče, který se má použít v bitech.

Atributy

Výjimky

Zprostředkovatele kryptografických služeb (CSP) nelze získat.

Příklady

Následující příklad kódu vytvoří RSACryptoServiceProvidera vygeneruje nový klíč a uloží ho do kontejneru klíčů.

using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Text;
array<Byte>^ RSAEncrypt( array<Byte>^DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding )
{
   try
   {
      
      //Create a new instance of RSACryptoServiceProvider.
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider;
      
      //Import the RSA Key information. This only needs
      //toinclude the public key information.
      RSAalg->ImportParameters( RSAKeyInfo );
      
      //Encrypt the passed byte array and specify OAEP padding.  
      //OAEP padding is only available on Microsoft Windows XP or
      //later.  
      return RSAalg->Encrypt( DataToEncrypt, DoOAEPPadding );
   }
   //Catch and display a CryptographicException  
   //to the console.
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
      return nullptr;
   }

}

array<Byte>^ RSADecrypt( array<Byte>^DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding )
{
   try
   {
      
      //Create a new instance of RSACryptoServiceProvider.
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider;
      
      //Import the RSA Key information. This needs
      //to include the private key information.
      RSAalg->ImportParameters( RSAKeyInfo );
      
      //Decrypt the passed byte array and specify OAEP padding.  
      //OAEP padding is only available on Microsoft Windows XP or
      //later.  
      return RSAalg->Decrypt( DataToDecrypt, DoOAEPPadding );
   }
   //Catch and display a CryptographicException  
   //to the console.
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e );
      return nullptr;
   }

}

int main()
{
   try
   {
      
      //Create a UnicodeEncoder to convert between byte array and string.
      UnicodeEncoding^ ByteConverter = gcnew UnicodeEncoding;
      
      //Create byte arrays to hold original, encrypted, and decrypted data.
      array<Byte>^dataToEncrypt = ByteConverter->GetBytes( "Data to Encrypt" );
      array<Byte>^encryptedData;
      array<Byte>^decryptedData;
      
      //Create a new instance of RSACryptoServiceProvider to generate
      //public and private key data.  Pass an integer specifying a key-
      //length of 2048.
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider( 2048 );
      
      //Display the key-legth to the console.
      Console::WriteLine( "A new key pair of legth {0} was created", RSAalg->KeySize );
      
      //Pass the data to ENCRYPT, the public key information 
      //(using RSACryptoServiceProvider.ExportParameters(false),
      //and a boolean flag specifying no OAEP padding.
      encryptedData = RSAEncrypt( dataToEncrypt, RSAalg->ExportParameters( false ), false );
      
      //Pass the data to DECRYPT, the private key information 
      //(using RSACryptoServiceProvider.ExportParameters(true),
      //and a boolean flag specifying no OAEP padding.
      decryptedData = RSADecrypt( encryptedData, RSAalg->ExportParameters( true ), false );
      
      //Display the decrypted plaintext to the console. 
      Console::WriteLine( "Decrypted plaintext: {0}", ByteConverter->GetString( decryptedData ) );
   }
   catch ( ArgumentNullException^ ) 
   {
      
      //Catch this exception in case the encryption did
      //not succeed.
      Console::WriteLine( "Encryption failed." );
   }

}
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.
            UnicodeEncoding ByteConverter = new UnicodeEncoding();

            //Create byte arrays to hold original, encrypted, and decrypted data.
            byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
            byte[] encryptedData;
            byte[] decryptedData;
            
            //Create a new instance of RSACryptoServiceProvider to generate
            //public and private key data.  Pass an integer specifying a key-
            //length of 2048.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(2048);

            //Display the key-legth to the console.
            Console.WriteLine("A new key pair of legth {0} was created", RSAalg.KeySize);

            //Pass the data to ENCRYPT, the public key information
            //(using RSACryptoServiceProvider.ExportParameters(false),
            //and a boolean flag specifying no OAEP padding.
            encryptedData = RSAEncrypt(dataToEncrypt,RSAalg.ExportParameters(false), false);

            //Pass the data to DECRYPT, the private key information
            //(using RSACryptoServiceProvider.ExportParameters(true),
            //and a boolean flag specifying no OAEP padding.
            decryptedData = RSADecrypt(encryptedData,RSAalg.ExportParameters(true), false);

            //Display the decrypted plaintext to the console.
            Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
        }
        catch(ArgumentNullException)
        {
            //Catch this exception in case the encryption did
            //not succeed.
            Console.WriteLine("Encryption failed.");
        }
    }

    static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
    {
        try
        {	
            //Create a new instance of RSACryptoServiceProvider.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            //Import the RSA Key information. This only needs
            //toinclude the public key information.
            RSAalg.ImportParameters(RSAKeyInfo);

            //Encrypt the passed byte array and specify OAEP padding.
            //OAEP padding is only available on Microsoft Windows XP or
            //later.
            return RSAalg.Encrypt(DataToEncrypt, DoOAEPPadding);
        }
            //Catch and display a CryptographicException
            //to the console.
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return null;
        }
    }

    static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo,bool DoOAEPPadding)
    {
        try
        {
            //Create a new instance of RSACryptoServiceProvider.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            //Import the RSA Key information. This needs
            //to include the private key information.
            RSAalg.ImportParameters(RSAKeyInfo);

            //Decrypt the passed byte array and specify OAEP padding.
            //OAEP padding is only available on Microsoft Windows XP or
            //later.
            return RSAalg.Decrypt(DataToDecrypt, DoOAEPPadding);
        }
            //Catch and display a CryptographicException
            //to the console.
        catch(CryptographicException e)
        {
            Console.WriteLine(e.ToString());

            return null;
        }
    }
}
Imports System.Security.Cryptography
Imports System.Text

Module RSACSPExample

    Sub Main()
        Try
            'Create a UnicodeEncoder to convert between byte array and string.
            Dim ByteConverter As New UnicodeEncoding

            'Create byte arrays to hold original, encrypted, and decrypted data.
            Dim dataToEncrypt As Byte() = ByteConverter.GetBytes("Data to Encrypt")
            Dim encryptedData() As Byte
            Dim decryptedData() As Byte

            'Create a new instance of RSACryptoServiceProvider to generate
            'public and private key data.  Pass an integer specifying a key-
            'length of 2048.
            Dim RSAalg As New RSACryptoServiceProvider(2048)

            'Display the key-legth to the console.
            Console.WriteLine("A new key pair of legth {0} was created", RSAalg.KeySize)

            'Pass the data to ENCRYPT, the public key information 
            '(using RSACryptoServiceProvider.ExportParameters(false),
            'and a boolean flag specifying no OAEP padding.
            encryptedData = RSAEncrypt(dataToEncrypt, RSAalg.ExportParameters(False), False)

            'Pass the data to DECRYPT, the private key information 
            '(using RSACryptoServiceProvider.ExportParameters(true),
            'and a boolean flag specifying no OAEP padding.
            decryptedData = RSADecrypt(encryptedData, RSAalg.ExportParameters(True), False)

            'Display the decrypted plaintext to the console. 
            Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData))
        Catch e As ArgumentNullException
            'Catch this exception in case the encryption did
            'not succeed.
            Console.WriteLine("Encryption failed.")
        End Try
    End Sub


    Function RSAEncrypt(ByVal DataToEncrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            'Create a new instance of RSACryptoServiceProvider.
            Dim RSAalg As New RSACryptoServiceProvider

            'Import the RSA Key information. This only needs
            'toinclude the public key information.
            RSAalg.ImportParameters(RSAKeyInfo)

            'Encrypt the passed byte array and specify OAEP padding.  
            'OAEP padding is only available on Microsoft Windows XP or
            'later.  
            Return RSAalg.Encrypt(DataToEncrypt, DoOAEPPadding)
            'Catch and display a CryptographicException  
            'to the console.
        Catch e As CryptographicException
            Console.WriteLine(e.Message)

            Return Nothing
        End Try
    End Function 


    Function RSADecrypt(ByVal DataToDecrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            'Create a new instance of RSACryptoServiceProvider.
            Dim RSAalg As New RSACryptoServiceProvider

            'Import the RSA Key information. This needs
            'to include the private key information.
            RSAalg.ImportParameters(RSAKeyInfo)

            'Decrypt the passed byte array and specify OAEP padding.  
            'OAEP padding is only available on Microsoft Windows XP or
            'later.  
            Return RSAalg.Decrypt(DataToDecrypt, DoOAEPPadding)
            'Catch and display a CryptographicException  
            'to the console.
        Catch e As CryptographicException
            Console.WriteLine(e.ToString())

            Return Nothing
        End Try
    End Function  

End Module

Poznámky

Tento konstruktor vytvoří Exchange pár klíčů vhodný k šifrování klíčů relace, aby je bylo možné bezpečně ukládat a vyměňovat s jinými uživateli. Vygenerovaný klíč odpovídá klíči vygenerovanému AT_KEYEXCHANGE pomocí hodnoty použité v nespravovaném kryptografickém rozhraní API microsoftu (CAPI).

Tento konstruktor negeneruje nový veřejný/privátní klíčpair okamžitě. Pokud metodou nebo jinou metodou importu ImportParameters klíče není načten žádný klíč, před potřebou dwKeySizeklíče se na vyžádání vytvoří dočasný klíč .bit.

Viz také

Platí pro

RSACryptoServiceProvider(CspParameters)

Zdroj:
RSACryptoServiceProvider.Unix.cs
Zdroj:
RSACryptoServiceProvider.Unix.cs
Zdroj:
RSACryptoServiceProvider.Unix.cs

Inicializuje novou instanci RSACryptoServiceProvider třídy se zadanými parametry.

public:
 RSACryptoServiceProvider(System::Security::Cryptography::CspParameters ^ parameters);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public RSACryptoServiceProvider (System.Security.Cryptography.CspParameters? parameters);
public RSACryptoServiceProvider (System.Security.Cryptography.CspParameters parameters);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
new System.Security.Cryptography.RSACryptoServiceProvider : System.Security.Cryptography.CspParameters -> System.Security.Cryptography.RSACryptoServiceProvider
new System.Security.Cryptography.RSACryptoServiceProvider : System.Security.Cryptography.CspParameters -> System.Security.Cryptography.RSACryptoServiceProvider
Public Sub New (parameters As CspParameters)

Parametry

parameters
CspParameters

Parametry, které se mají předat poskytovateli kryptografických služeb (CSP).

Atributy

Výjimky

Poskytovatele CSP nelze získat.

Příklady

Následující příklad kódu vytvoří RSACryptoServiceProvider objekt, vygeneruje nový klíč a uloží ho do kontejneru klíčů.

using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Text;
void RSAPersistKeyInCSP( String^ ContainerName )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters;
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of RSACryptoServiceProvider to generate
      //a new key pair.  Pass the CspParameters class to persist the 
      //key in the container.
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider( cspParams );
      
      //Indicate that the key was persisted.
      Console::WriteLine( "The RSA key was persisted in the container, \"{0}\".", ContainerName );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

void RSADeleteKeyInCSP( String^ ContainerName )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters;
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of RSACryptoServiceProvider. 
      //Pass the CspParameters class to use the 
      //key in the container.
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider( cspParams );
      
      //Delete the key entry in the container.
      RSAalg->PersistKeyInCsp = false;
      
      //Call Clear to release resources and delete the key from the container.
      RSAalg->Clear();
      
      //Indicate that the key was persisted.
      Console::WriteLine( "The RSA key was deleted from the container, \"{0}\".", ContainerName );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

array<Byte>^ RSAEncrypt( array<Byte>^DataToEncrypt, String^ ContainerName, bool DoOAEPPadding )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters;
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of RSACryptoServiceProvider.
      //Pass the CspParameters class to use the key 
      //from the key in the container.
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider( cspParams );
      
      //Encrypt the passed byte array and specify OAEP padding.  
      //OAEP padding is only available on Microsoft Windows XP or
      //later.  
      return RSAalg->Encrypt( DataToEncrypt, DoOAEPPadding );
   }
   //Catch and display a CryptographicException  
   //to the console.
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
      return nullptr;
   }

}

array<Byte>^ RSADecrypt( array<Byte>^DataToDecrypt, String^ ContainerName, bool DoOAEPPadding )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters;
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of RSACryptoServiceProvider.
      //Pass the CspParameters class to use the key 
      //from the key in the container.
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider( cspParams );
      
      //Decrypt the passed byte array and specify OAEP padding.  
      //OAEP padding is only available on Microsoft Windows XP or
      //later.  
      return RSAalg->Decrypt( DataToDecrypt, DoOAEPPadding );
   }
   //Catch and display a CryptographicException  
   //to the console.
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e );
      return nullptr;
   }

}

int main()
{
   try
   {
      String^ KeyContainerName = "MyKeyContainer";
      
      //Create a new key and persist it in 
      //the key container.
      RSAPersistKeyInCSP( KeyContainerName );
      
      //Create a UnicodeEncoder to convert between byte array and string.
      UnicodeEncoding^ ByteConverter = gcnew UnicodeEncoding;
      
      //Create byte arrays to hold original, encrypted, and decrypted data.
      array<Byte>^dataToEncrypt = ByteConverter->GetBytes( "Data to Encrypt" );
      array<Byte>^encryptedData;
      array<Byte>^decryptedData;
      
      //Pass the data to ENCRYPT, the name of the key container,
      //and a boolean flag specifying no OAEP padding.
      encryptedData = RSAEncrypt( dataToEncrypt, KeyContainerName, false );
      
      //Pass the data to DECRYPT, the name of the key container,
      //and a boolean flag specifying no OAEP padding.
      decryptedData = RSADecrypt( encryptedData, KeyContainerName, false );
      
      //Display the decrypted plaintext to the console. 
      Console::WriteLine( "Decrypted plaintext: {0}", ByteConverter->GetString( decryptedData ) );
      RSADeleteKeyInCSP( KeyContainerName );
   }
   catch ( ArgumentNullException^ ) 
   {
      
      //Catch this exception in case the encryption did
      //not succeed.
      Console::WriteLine( "Encryption failed." );
   }

}
using System;
using System.Security.Cryptography;
using System.Text;

class RSACSPSample
{

    static void Main()
    {
        try
        {
            string KeyContainerName = "MyKeyContainer";

            //Create a new key and persist it in
            //the key container.
            RSAPersistKeyInCSP(KeyContainerName);

            //Create a UnicodeEncoder to convert between byte array and string.
            UnicodeEncoding ByteConverter = new UnicodeEncoding();

            //Create byte arrays to hold original, encrypted, and decrypted data.
            byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
            byte[] encryptedData;
            byte[] decryptedData;

            //Pass the data to ENCRYPT, the name of the key container,
            //and a boolean flag specifying no OAEP padding.
            encryptedData = RSAEncrypt(dataToEncrypt,KeyContainerName, false);

            //Pass the data to DECRYPT, the name of the key container,
            //and a boolean flag specifying no OAEP padding.
            decryptedData = RSADecrypt(encryptedData,KeyContainerName, false);

            //Display the decrypted plaintext to the console.
            Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));

            RSADeleteKeyInCSP(KeyContainerName);
        }
        catch(ArgumentNullException)
        {
            //Catch this exception in case the encryption did
            //not succeed.
            Console.WriteLine("Encryption failed.");
        }
    }

    public static void RSAPersistKeyInCSP(string ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of RSACryptoServiceProvider to generate
            //a new key pair.  Pass the CspParameters class to persist the
            //key in the container.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams);

            //Indicate that the key was persisted.
            Console.WriteLine("The RSA key was persisted in the container, \"{0}\".", ContainerName);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    public static void RSADeleteKeyInCSP(string ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of RSACryptoServiceProvider.
            //Pass the CspParameters class to use the
            //key in the container.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams);

            //Delete the key entry in the container.
            RSAalg.PersistKeyInCsp = false;

            //Call Clear to release resources and delete the key from the container.
            RSAalg.Clear();

            //Indicate that the key was persisted.
            Console.WriteLine("The RSA key was deleted from the container, \"{0}\".", ContainerName);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    static public byte[] RSAEncrypt(byte[] DataToEncrypt, string ContainerName, bool DoOAEPPadding)
    {
        try
        {	
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of RSACryptoServiceProvider.
            //Pass the CspParameters class to use the key
            //from the key in the container.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams);

            //Encrypt the passed byte array and specify OAEP padding.
            //OAEP padding is only available on Microsoft Windows XP or
            //later.
            return RSAalg.Encrypt(DataToEncrypt, DoOAEPPadding);
        }
            //Catch and display a CryptographicException
            //to the console.
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return null;
        }
    }

    static public byte[] RSADecrypt(byte[] DataToDecrypt, string ContainerName, bool DoOAEPPadding)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of RSACryptoServiceProvider.
            //Pass the CspParameters class to use the key
            //from the key in the container.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams);

            //Decrypt the passed byte array and specify OAEP padding.
            //OAEP padding is only available on Microsoft Windows XP or
            //later.
            return RSAalg.Decrypt(DataToDecrypt, DoOAEPPadding);
        }
            //Catch and display a CryptographicException
            //to the console.
        catch(CryptographicException e)
        {
            Console.WriteLine(e.ToString());

            return null;
        }
    }
}
Imports System.Security.Cryptography
Imports System.Text

Module RSACSPExample

    Sub Main()
        Try
            Dim KeyContainerName As String = "MyKeyContainer"

            'Create a new key and persist it in 
            'the key container.
            RSAPersistKeyInCSP(KeyContainerName)

            'Create a UnicodeEncoder to convert between byte array and string.
            Dim ByteConverter As New UnicodeEncoding

            'Create byte arrays to hold original, encrypted, and decrypted data.
            Dim dataToEncrypt As Byte() = ByteConverter.GetBytes("Data to Encrypt")
            Dim encryptedData() As Byte
            Dim decryptedData() As Byte

            'Pass the data to ENCRYPT, the name of the key container, 
            'and a boolean flag specifying no OAEP padding.
            encryptedData = RSAEncrypt(dataToEncrypt, KeyContainerName, False)

            'Pass the data to DECRYPT, the name of the key container, 
            'and a boolean flag specifying no OAEP padding.
            decryptedData = RSADecrypt(encryptedData, KeyContainerName, False)

            'Display the decrypted plaintext to the console. 
            Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData))

            RSADeleteKeyInCSP(KeyContainerName)
        Catch e As ArgumentNullException
            'Catch this exception in case the encryption did
            'not succeed.
            Console.WriteLine("Encryption failed.")
        End Try
    End Sub


    Sub RSAPersistKeyInCSP(ByVal ContainerName As String)
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New CspParameters

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of RSACryptoServiceProvider to generate
            'a new key pair.  Pass the CspParameters class to persist the 
            'key in the container.
            Dim RSAalg As New RSACryptoServiceProvider(cspParams)

            'Indicate that the key was persisted.
            Console.WriteLine("The RSA key was persisted in the container, ""{0}"".", ContainerName)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub


    Sub RSADeleteKeyInCSP(ByVal ContainerName As String)
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New CspParameters

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of RSACryptoServiceProvider. 
            'Pass the CspParameters class to use the 
            'key in the container.
            Dim RSAalg As New RSACryptoServiceProvider(cspParams)

            'Delete the key entry in the container.
            RSAalg.PersistKeyInCsp = False

            'Call Clear to release resources and delete the key from the container.
            RSAalg.Clear()

            'Indicate that the key was persisted.
            Console.WriteLine("The RSA key was deleted from the container, ""{0}"".", ContainerName)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub


    Function RSAEncrypt(ByVal DataToEncrypt() As Byte, ByVal ContainerName As String, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New CspParameters

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of RSACryptoServiceProvider.
            'Pass the CspParameters class to use the key 
            'from the key in the container.
            Dim RSAalg As New RSACryptoServiceProvider(cspParams)

            'Encrypt the passed byte array and specify OAEP padding.  
            'OAEP padding is only available on Microsoft Windows XP or
            'later.  
            Return RSAalg.Encrypt(DataToEncrypt, DoOAEPPadding)
            'Catch and display a CryptographicException  
            'to the console.
        Catch e As CryptographicException
            Console.WriteLine(e.Message)

            Return Nothing
        End Try
    End Function


    Function RSADecrypt(ByVal DataToDecrypt() As Byte, ByVal ContainerName As String, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New CspParameters

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of RSACryptoServiceProvider.
            'Pass the CspParameters class to use the key 
            'from the key in the container.
            Dim RSAalg As New RSACryptoServiceProvider(cspParams)

            'Decrypt the passed byte array and specify OAEP padding.  
            'OAEP padding is only available on Microsoft Windows XP or
            'later.  
            Return RSAalg.Decrypt(DataToDecrypt, DoOAEPPadding)
            'Catch and display a CryptographicException  
            'to the console.
        Catch e As CryptographicException
            Console.WriteLine(e.ToString())

            Return Nothing
        End Try
    End Function

End Module

Poznámky

Tento konstruktor vytvoří nebo znovu použije kontejner klíčů zadaný pomocí KeyContainerName pole parametru parameters .

Ve výchozím nastavení tento konstruktor vytvoří Exchange pár klíčů vhodný k šifrování klíčů relace, aby je bylo možné bezpečně ukládat a vyměňovat s jinými uživateli. Vygenerovaný klíč odpovídá klíči vygenerovanému AT_KEYEXCHANGE pomocí hodnoty použité v nespravovaném kryptografickém rozhraní API microsoftu (CAPI).

Můžete vytvořit Signature pár klíčů vhodný pro ověřování (digitálně podepsaných) zpráv nebo souborů nastavením KeyNumber pole parametru parametersSignature na hodnotu. Tento typ klíče odpovídá hodnotě AT_SIGNATURE použité v capi.

Pokud vytvoříte objekt se zadanou RSACryptoServiceProvider hodnotou a pak vytvoříte další RSACryptoServiceProvider objekt se zadanou Signature hodnotou, oba klíče se umístí do jednoho kontejneru, pokud oba objekty určují stejný název kontejneru klíčů.Exchange

Chcete-li vytvořit klíč, který je kompatibilní s podepisováním silného RSACryptoServiceProvider názvu pomocí třídy, musíte vytvořit Signature pár klíčů.

Viz také

Platí pro

RSACryptoServiceProvider(Int32, CspParameters)

Zdroj:
RSACryptoServiceProvider.Unix.cs
Zdroj:
RSACryptoServiceProvider.Unix.cs
Zdroj:
RSACryptoServiceProvider.Unix.cs

Inicializuje novou instanci RSACryptoServiceProvider třídy se zadanou velikostí klíče a parametry.

public:
 RSACryptoServiceProvider(int dwKeySize, System::Security::Cryptography::CspParameters ^ parameters);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public RSACryptoServiceProvider (int dwKeySize, System.Security.Cryptography.CspParameters? parameters);
public RSACryptoServiceProvider (int dwKeySize, System.Security.Cryptography.CspParameters parameters);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
new System.Security.Cryptography.RSACryptoServiceProvider : int * System.Security.Cryptography.CspParameters -> System.Security.Cryptography.RSACryptoServiceProvider
new System.Security.Cryptography.RSACryptoServiceProvider : int * System.Security.Cryptography.CspParameters -> System.Security.Cryptography.RSACryptoServiceProvider
Public Sub New (dwKeySize As Integer, parameters As CspParameters)

Parametry

dwKeySize
Int32

Velikost klíče, který se má použít v bitech.

parameters
CspParameters

Parametry, které se mají předat poskytovateli kryptografických služeb (CSP).

Atributy

Výjimky

Poskytovatele CSP nelze získat.

-nebo-

Klíč nelze vytvořit.

Příklady

Následující příklad kódu vytvoří RSACryptoServiceProvidera vygeneruje nový klíč a uloží ho do kontejneru klíčů.

using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Text;
void RSAPersistKeyInCSP( String^ ContainerName )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters;
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of RSACryptoServiceProvider to generate
      //a new key pair.  Pass the CspParameters class to persist the 
      //key in the container.  Pass an intger of 2048 to specify the 
      //key-size.
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider( 2048,cspParams );
      
      //Indicate that the key was persisted.
      Console::WriteLine( "The RSA key with a key-size of {0} was persisted in the container, \"{1}\".", RSAalg->KeySize, ContainerName );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

void RSADeleteKeyInCSP( String^ ContainerName )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters;
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of DSACryptoServiceProvider. 
      //Pass the CspParameters class to use the 
      //key in the container.
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider( cspParams );
      
      //Delete the key entry in the container.
      RSAalg->PersistKeyInCsp = false;
      
      //Call Clear to release resources and delete the key from the container.
      RSAalg->Clear();
      
      //Indicate that the key was persisted.
      Console::WriteLine( "The RSA key was deleted from the container, \"{0}\".", ContainerName );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

array<Byte>^ RSAEncrypt( array<Byte>^DataToEncrypt, String^ ContainerName, bool DoOAEPPadding )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters;
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of DSACryptoServiceProvider.
      //Pass the CspParameters class to use the key 
      //from the key in the container.
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider( cspParams );
      
      //Encrypt the passed byte array and specify OAEP padding.  
      //OAEP padding is only available on Microsoft Windows XP or
      //later.  
      return RSAalg->Encrypt( DataToEncrypt, DoOAEPPadding );
   }
   //Catch and display a CryptographicException  
   //to the console.
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
      return nullptr;
   }

}

array<Byte>^ RSADecrypt( array<Byte>^DataToDecrypt, String^ ContainerName, bool DoOAEPPadding )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters;
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of DSACryptoServiceProvider.
      //Pass the CspParameters class to use the key 
      //from the key in the container.
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider( cspParams );
      
      //Decrypt the passed byte array and specify OAEP padding.  
      //OAEP padding is only available on Microsoft Windows XP or
      //later.  
      return RSAalg->Decrypt( DataToDecrypt, DoOAEPPadding );
   }
   //Catch and display a CryptographicException  
   //to the console.
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e );
      return nullptr;
   }

}

int main()
{
   try
   {
      String^ KeyContainerName = "MyKeyContainer";
      
      //Create a new key and persist it in 
      //the key container.
      RSAPersistKeyInCSP( KeyContainerName );
      
      //Create a UnicodeEncoder to convert between byte array and string.
      UnicodeEncoding^ ByteConverter = gcnew UnicodeEncoding;
      
      //Create byte arrays to hold original, encrypted, and decrypted data.
      array<Byte>^dataToEncrypt = ByteConverter->GetBytes( "Data to Encrypt" );
      array<Byte>^encryptedData;
      array<Byte>^decryptedData;
      
      //Pass the data to ENCRYPT, the name of the key container,
      //and a boolean flag specifying no OAEP padding.
      encryptedData = RSAEncrypt( dataToEncrypt, KeyContainerName, false );
      
      //Pass the data to DECRYPT, the name of the key container,
      //and a boolean flag specifying no OAEP padding.
      decryptedData = RSADecrypt( encryptedData, KeyContainerName, false );
      
      //Display the decrypted plaintext to the console. 
      Console::WriteLine( "Decrypted plaintext: {0}", ByteConverter->GetString( decryptedData ) );
      RSADeleteKeyInCSP( KeyContainerName );
   }
   catch ( ArgumentNullException^ ) 
   {
      
      //Catch this exception in case the encryption did
      //not succeed.
      Console::WriteLine( "Encryption failed." );
   }

}
using System;
using System.Security.Cryptography;
using System.Text;

class RSACSPSample
{

    static void Main()
    {
        try
        {
            string KeyContainerName = "MyKeyContainer";

            //Create a new key and persist it in
            //the key container.
            RSAPersistKeyInCSP(KeyContainerName);

            //Create a UnicodeEncoder to convert between byte array and string.
            UnicodeEncoding ByteConverter = new UnicodeEncoding();

            //Create byte arrays to hold original, encrypted, and decrypted data.
            byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
            byte[] encryptedData;
            byte[] decryptedData;

            //Pass the data to ENCRYPT, the name of the key container,
            //and a boolean flag specifying no OAEP padding.
            encryptedData = RSAEncrypt(dataToEncrypt,KeyContainerName, false);

            //Pass the data to DECRYPT, the name of the key container,
            //and a boolean flag specifying no OAEP padding.
            decryptedData = RSADecrypt(encryptedData,KeyContainerName, false);

            //Display the decrypted plaintext to the console.
            Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));

            RSADeleteKeyInCSP(KeyContainerName);
        }
        catch(ArgumentNullException)
        {
            //Catch this exception in case the encryption did
            //not succeed.
            Console.WriteLine("Encryption failed.");
        }
    }

    public static void RSAPersistKeyInCSP(string ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of RSACryptoServiceProvider to generate
            //a new key pair.  Pass the CspParameters class to persist the
            //key in the container.  Pass an intger of 2048 to specify the
            //key-size.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider( 2048, cspParams);

            //Indicate that the key was persisted.
            Console.WriteLine("The RSA key with a key-size of {0} was persisted in the container, \"{1}\".",
                              RSAalg.KeySize , ContainerName);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    public static void RSADeleteKeyInCSP(string ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider.
            //Pass the CspParameters class to use the
            //key in the container.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams);

            //Delete the key entry in the container.
            RSAalg.PersistKeyInCsp = false;

            //Call Clear to release resources and delete the key from the container.
            RSAalg.Clear();

            //Indicate that the key was persisted.
            Console.WriteLine("The RSA key was deleted from the container, \"{0}\".", ContainerName);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    static public byte[] RSAEncrypt(byte[] DataToEncrypt, string ContainerName, bool DoOAEPPadding)
    {
        try
        {	
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider.
            //Pass the CspParameters class to use the key
            //from the key in the container.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams);

            //Encrypt the passed byte array and specify OAEP padding.
            //OAEP padding is only available on Microsoft Windows XP or
            //later.
            return RSAalg.Encrypt(DataToEncrypt, DoOAEPPadding);
        }
            //Catch and display a CryptographicException
            //to the console.
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return null;
        }
    }

    static public byte[] RSADecrypt(byte[] DataToDecrypt, string ContainerName, bool DoOAEPPadding)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider.
            //Pass the CspParameters class to use the key
            //from the key in the container.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams);

            //Decrypt the passed byte array and specify OAEP padding.
            //OAEP padding is only available on Microsoft Windows XP or
            //later.
            return RSAalg.Decrypt(DataToDecrypt, DoOAEPPadding);
        }
            //Catch and display a CryptographicException
            //to the console.
        catch(CryptographicException e)
        {
            Console.WriteLine(e.ToString());

            return null;
        }
    }
}
Imports System.Security.Cryptography
Imports System.Text

Module RSACSPExample

    Sub Main()
        Try
            Dim KeyContainerName As String = "MyKeyContainer"

            'Create a new key and persist it in 
            'the key container.
            RSAPersistKeyInCSP(KeyContainerName)

            'Create a UnicodeEncoder to convert between byte array and string.
            Dim ByteConverter As New UnicodeEncoding

            'Create byte arrays to hold original, encrypted, and decrypted data.
            Dim dataToEncrypt As Byte() = ByteConverter.GetBytes("Data to Encrypt")
            Dim encryptedData() As Byte
            Dim decryptedData() As Byte

            'Pass the data to ENCRYPT, the name of the key container, 
            'and a boolean flag specifying no OAEP padding.
            encryptedData = RSAEncrypt(dataToEncrypt, KeyContainerName, False)

            'Pass the data to DECRYPT, the name of the key container, 
            'and a boolean flag specifying no OAEP padding.
            decryptedData = RSADecrypt(encryptedData, KeyContainerName, False)

            'Display the decrypted plaintext to the console. 
            Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData))

            RSADeleteKeyInCSP(KeyContainerName)
        Catch e As ArgumentNullException
            'Catch this exception in case the encryption did
            'not succeed.
            Console.WriteLine("Encryption failed.")
        End Try
    End Sub


    Sub RSAPersistKeyInCSP(ByVal ContainerName As String)
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New CspParameters

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of RSACryptoServiceProvider to generate
            'a new key pair.  Pass the CspParameters class to persist the 
            'key in the container.
            Dim RSAalg As New RSACryptoServiceProvider(2048, cspParams)

            'Indicate that the key was persisted.
            Console.WriteLine("The RSA key with a key-size of {0} was persisted in the container, ""{1}"".", _
                              RSAalg.KeySize, ContainerName)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub


    Sub RSADeleteKeyInCSP(ByVal ContainerName As String)
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New CspParameters

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of RSACryptoServiceProvider. 
            'Pass the CspParameters class to use the 
            'key in the container.
            Dim RSAalg As New RSACryptoServiceProvider(cspParams)

            'Delete the key entry in the container.
            RSAalg.PersistKeyInCsp = False

            'Call Clear to release resources and delete the key from the container.
            RSAalg.Clear()

            'Indicate that the key was persisted.
            Console.WriteLine("The RSA key was deleted from the container, ""{0}"".", ContainerName)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub


    Function RSAEncrypt(ByVal DataToEncrypt() As Byte, ByVal ContainerName As String, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New CspParameters

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of RSACryptoServiceProvider.
            'Pass the CspParameters class to use the key 
            'from the key in the container.
            Dim RSAalg As New RSACryptoServiceProvider(cspParams)

            'Encrypt the passed byte array and specify OAEP padding.  
            'OAEP padding is only available on Microsoft Windows XP or
            'later.  
            Return RSAalg.Encrypt(DataToEncrypt, DoOAEPPadding)
            'Catch and display a CryptographicException  
            'to the console.
        Catch e As CryptographicException
            Console.WriteLine(e.Message)

            Return Nothing
        End Try
    End Function


    Function RSADecrypt(ByVal DataToDecrypt() As Byte, ByVal ContainerName As String, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New CspParameters

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of RSACryptoServiceProvider.
            'Pass the CspParameters class to use the key 
            'from the key in the container.
            Dim RSAalg As New RSACryptoServiceProvider(cspParams)

            'Decrypt the passed byte array and specify OAEP padding.  
            'OAEP padding is only available on Microsoft Windows XP or
            'later.  
            Return RSAalg.Decrypt(DataToDecrypt, DoOAEPPadding)
            'Catch and display a CryptographicException  
            'to the console.
        Catch e As CryptographicException
            Console.WriteLine(e.ToString())

            Return Nothing
        End Try
    End Function

End Module

Poznámky

Tento konstruktor vytvoří nebo znovu použije kontejner klíčů zadaný pomocí KeyContainerName pole parametru parameters .

Ve výchozím nastavení tento konstruktor vytvoří Exchange pár klíčů vhodný k šifrování klíčů relace, aby je bylo možné bezpečně ukládat a vyměňovat s jinými uživateli. Vygenerovaný klíč odpovídá klíči vygenerovanému AT_KEYEXCHANGE pomocí hodnoty použité v nespravovaném kryptografickém rozhraní API microsoftu (CAPI).

Můžete vytvořit Signature pár klíčů vhodný pro ověřování (digitálně podepsaných) zpráv nebo souborů nastavením KeyNumber pole parametru parametersSignature na hodnotu. Tento typ klíče odpovídá hodnotě AT_SIGNATURE použité v capi.

Pokud vytvoříte objekt se zadanou RSACryptoServiceProvider hodnotou a pak vytvoříte další RSACryptoServiceProvider objekt se zadanou Signature hodnotou, oba klíče se umístí do jednoho kontejneru, pokud oba objekty určují stejný název kontejneru klíčů.Exchange

Chcete-li vytvořit klíč, který je kompatibilní s podepisováním silného RSACryptoServiceProvider názvu pomocí třídy, musíte vytvořit Signature pár klíčů.

Viz také

Platí pro