RSACryptoServiceProvider.Encrypt RSACryptoServiceProvider.Encrypt RSACryptoServiceProvider.Encrypt RSACryptoServiceProvider.Encrypt Method

Définition

Chiffre les données avec l’algorithme RSA.Encrypts data with the RSA algorithm.

Surcharges

Encrypt(Byte[], Boolean) Encrypt(Byte[], Boolean) Encrypt(Byte[], Boolean) Encrypt(Byte[], Boolean)

Chiffre les données avec l’algorithme RSA.Encrypts data with the RSA algorithm.

Encrypt(Byte[], RSAEncryptionPadding) Encrypt(Byte[], RSAEncryptionPadding) Encrypt(Byte[], RSAEncryptionPadding) Encrypt(Byte[], RSAEncryptionPadding)

Chiffre les données avec l’algorithme RSA à l’aide du remplissage spécifié.Encrypts data with the RSA algorithm using the specified padding.

Encrypt(Byte[], Boolean) Encrypt(Byte[], Boolean) Encrypt(Byte[], Boolean) Encrypt(Byte[], Boolean)

Chiffre les données avec l’algorithme RSA.Encrypts data with the RSA algorithm.

public:
 cli::array <System::Byte> ^ Encrypt(cli::array <System::Byte> ^ rgb, bool fOAEP);
public byte[] Encrypt (byte[] rgb, bool fOAEP);
member this.Encrypt : byte[] * bool -> byte[]
Public Function Encrypt (rgb As Byte(), fOAEP As Boolean) As Byte()

Paramètres

rgb
Byte[]

Données à chiffrer.The data to be encrypted.

fOAEP
Boolean Boolean Boolean Boolean

true pour effectuer directement le chiffrement RSA à l’aide du remplissage OAEP (uniquement disponible sur un ordinateur exécutant Windows XP ou ultérieur) ; sinon, false pour utiliser le remplissage PKCS #1 v1.5.true to perform direct RSA encryption using OAEP padding (only available on a computer running Windows XP or later); otherwise, false to use PKCS#1 v1.5 padding.

Retours

Byte[]

Données chiffrées.The encrypted data.

Exceptions

Le fournisseur de services de chiffrement ne peut pas être acquis.The cryptographic service provider (CSP) cannot be acquired.

- ou --or- La longueur du paramètre rgb est supérieure à la longueur maximale autorisée.The length of the rgb parameter is greater than the maximum allowed length.

- ou --or- Le paramètre fOAEP est true et le remplissage OAEP n’est pas pris en charge.The fOAEP parameter is true and OAEP padding is not supported.

Exemples

L’exemple de code suivant initialise un RSACryptoServiceProvider objet avec la valeur d’une clé publique (envoyée par un tiers), génère une clé de session à RijndaelManaged l’aide de l’algorithme, puis chiffre la clé RSACryptoServiceProvider de session à l’aide de l’objet.The following code example initializes an RSACryptoServiceProvider object to the value of a public key (sent by another party), generates a session key using the RijndaelManaged algorithm, and then encrypts the session key using the RSACryptoServiceProvider object. À l’aide de ce schéma, la clé de session peut être renvoyée au propriétaire de la clé RSA privée et les deux parties peuvent utiliser la clé de session pour échanger des données chiffrées.Using this scheme, the session key could be sent back to the owner of the private RSA key and the two parties could use the session key to exchange encrypted data.

#using <System.dll>

using namespace System;
using namespace System::Security::Cryptography;
int main()
{
   try
   {
      
      //initialze the Byte arrays to the public key information.
      array<Byte>^PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222,207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175,108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194,240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139,168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171,38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93,106,99,179,68,175,211,164,116,64,148,226,254,172,147};
      array<Byte>^Exponent = {1,0,1};
      
      //Values to store encrypted symmetric keys.
      array<Byte>^EncryptedSymmetricKey;
      array<Byte>^EncryptedSymmetricIV;
      
      //Create a new instance of RSACryptoServiceProvider.
      RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
      
      //Create a new instance of RSAParameters.
      RSAParameters RSAKeyInfo;
      
      //Set RSAKeyInfo to the public key values. 
      RSAKeyInfo.Modulus = PublicKey;
      RSAKeyInfo.Exponent = Exponent;
      
      //Import key parameters into RSA.
      RSA->ImportParameters( RSAKeyInfo );
      
      //Create a new instance of the RijndaelManaged class.
      RijndaelManaged^ RM = gcnew RijndaelManaged;
      
      //Encrypt the symmetric key and IV.
      EncryptedSymmetricKey = RSA->Encrypt( RM->Key, false );
      EncryptedSymmetricIV = RSA->Encrypt( RM->IV, false );
      Console::WriteLine( "RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider." );
   }
   catch ( CryptographicException^ e ) 
   {
      
      //Catch and display a CryptographicException  
      //to the console.
      Console::WriteLine( e->Message );
   }

}

using System;
using System.Security.Cryptography;

class RSACSPSample
{

	static void Main()
	{
		try
		{		//initialze the byte arrays to the public key information.
			byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,
								   74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222,
								   207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175,
								   108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194,
								   240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139,
								   168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171,
								   38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93,
								   106,99,179,68,175,211,164,116,64,148,226,254,172,147};

			byte[] Exponent = {1,0,1};
      
			//Values to store encrypted symmetric keys.
			byte[] EncryptedSymmetricKey;
			byte[] EncryptedSymmetricIV;

			//Create a new instance of RSACryptoServiceProvider.
			RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

			//Create a new instance of RSAParameters.
			RSAParameters RSAKeyInfo = new RSAParameters();

			//Set RSAKeyInfo to the public key values. 
			RSAKeyInfo.Modulus = PublicKey;
			RSAKeyInfo.Exponent = Exponent;

			//Import key parameters into RSA.
			RSA.ImportParameters(RSAKeyInfo);

			//Create a new instance of the RijndaelManaged class.
			RijndaelManaged RM = new RijndaelManaged();

			//Encrypt the symmetric key and IV.
			EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false);
			EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false);

			Console.WriteLine("RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider."); 
		
		}
		//Catch and display a CryptographicException  
		//to the console.
		catch(CryptographicException e)
		{
			Console.WriteLine(e.Message);
		}
	}
}
Imports System.Security.Cryptography

Class RSACSPSample

    Shared Sub Main()
        Try
            'initialze the byte arrays to the public key information.
            Dim PublicKey As Byte() = {214, 46, 220, 83, 160, 73, 40, 39, 201, 155, 19, 202, 3, 11, 191, 178, 56, 74, 90, 36, 248, 103, 18, 144, 170, 163, 145, 87, 54, 61, 34, 220, 222, 207, 137, 149, 173, 14, 92, 120, 206, 222, 158, 28, 40, 24, 30, 16, 175, 108, 128, 35, 230, 118, 40, 121, 113, 125, 216, 130, 11, 24, 90, 48, 194, 240, 105, 44, 76, 34, 57, 249, 228, 125, 80, 38, 9, 136, 29, 117, 207, 139, 168, 181, 85, 137, 126, 10, 126, 242, 120, 247, 121, 8, 100, 12, 201, 171, 38, 226, 193, 180, 190, 117, 177, 87, 143, 242, 213, 11, 44, 180, 113, 93, 106, 99, 179, 68, 175, 211, 164, 116, 64, 148, 226, 254, 172, 147}

            Dim Exponent As Byte() = {1, 0, 1}

            'Values to store encrypted symmetric keys.
            Dim EncryptedSymmetricKey() As Byte
            Dim EncryptedSymmetricIV() As Byte

            'Create a new instance of RSACryptoServiceProvider.
            Dim RSA As New RSACryptoServiceProvider()

            'Create a new instance of RSAParameters.
            Dim RSAKeyInfo As New RSAParameters()

            'Set RSAKeyInfo to the public key values. 
            RSAKeyInfo.Modulus = PublicKey
            RSAKeyInfo.Exponent = Exponent

            'Import key parameters into RSA.
            RSA.ImportParameters(RSAKeyInfo)

            'Create a new instance of the RijndaelManaged class.
            Dim RM As New RijndaelManaged()

            'Encrypt the symmetric key and IV.
            EncryptedSymmetricKey = RSA.Encrypt(RM.Key, False)
            EncryptedSymmetricIV = RSA.Encrypt(RM.IV, False)

            Console.WriteLine("RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider.")

            'Catch and display a CryptographicException  
            'to the console.
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub
End Class

Remarques

Le tableau suivant décrit le remplissage pris en charge par les différentes versions de Microsoft Windows et la longueur rgb maximale autorisée par les différentes combinaisons de systèmes d’exploitation et de remplissage.The following table describes the padding supported by different versions of Microsoft Windows and the maximum length of rgb allowed by the different combinations of operating systems and padding.

PaddingPadding Système d’exploitation pris en chargeOperating System Supported Longueur maximale du paramètre RGBMaximum Length of rgb Parameter
Remplissage OAEP (PKCS # 1 V2)OAEP padding (PKCS#1 v2) Windows XP ou version ultérieure.Windows XP or later. Modulo Size-2-2 * hLen, où hLen est la taille du hachage.Modulus size -2 -2*hLen, where hLen is the size of the hash.
Chiffrement direct (PKCS # 1 v 1.5)Direct Encryption (PKCS#1 v1.5) Microsoft Windows 2000 ou version ultérieure avec High Encryption Pack installé.Microsoft Windows 2000 or later with the high encryption pack installed. Taille du modulo-11.Modulus size - 11. (11 octets est le remplissage minimal possible.)(11 bytes is the minimum padding possible.)
Chiffrement direct et remplissage OAEP non pris en chargeDirect Encryption and OAEP padding not supported Microsoft Windows 98, Windows Millennium Edition ou Windows 2000 ou version ultérieure sans le Pack de chiffrement élevé installé.Microsoft Windows 98, Windows Millennium Edition, or Windows 2000 or later without the high encryption pack installed. Taille maximale autorisée pour une clé symétrique.The maximum size allowed for a symmetric key.

Utilisez Decrypt pour déchiffrer les résultats de cette méthode.Use Decrypt to decrypt the results of this method.

Voir aussi

Encrypt(Byte[], RSAEncryptionPadding) Encrypt(Byte[], RSAEncryptionPadding) Encrypt(Byte[], RSAEncryptionPadding) Encrypt(Byte[], RSAEncryptionPadding)

Chiffre les données avec l’algorithme RSA à l’aide du remplissage spécifié.Encrypts data with the RSA algorithm using the specified padding.

public:
 override cli::array <System::Byte> ^ Encrypt(cli::array <System::Byte> ^ data, System::Security::Cryptography::RSAEncryptionPadding ^ padding);
public override byte[] Encrypt (byte[] data, System.Security.Cryptography.RSAEncryptionPadding padding);
override this.Encrypt : byte[] * System.Security.Cryptography.RSAEncryptionPadding -> byte[]
Public Overrides Function Encrypt (data As Byte(), padding As RSAEncryptionPadding) As Byte()

Paramètres

data
Byte[]

Données à chiffrer.The data to encrypt.

Retours

Byte[]

Données chiffrées.The encrypted data.

Exceptions

data a la valeur null.data is null.

- ou --or- padding a la valeur null.padding is null.

Le mode de remplissage n’est pas pris en charge.The padding mode is not supported.

Remarques

paddingdoit avoir la RSAEncryptionPadding.Pkcs1 valeur RSAEncryptionPadding.OaepSHA1ou.padding must be either RSAEncryptionPadding.Pkcs1 or RSAEncryptionPadding.OaepSHA1.

S’applique à