Lire en anglais

Partager via


RijndaelManaged.CreateDecryptor Méthode

Définition

Crée un objet déchiffreur symétrique.

Surcharges

CreateDecryptor()

Crée un objet déchiffreur symétrique avec la propriété Key et le vecteur d'initialisation (IV) actuels.

CreateDecryptor(Byte[], Byte[])

Crée un objet déchiffreur Rijndael symétrique avec le Key spécifié et le vecteur d'initialisation (IV).

CreateDecryptor()

Source:
RijndaelManaged.cs
Source:
RijndaelManaged.cs
Source:
RijndaelManaged.cs

Crée un objet déchiffreur symétrique avec la propriété Key et le vecteur d'initialisation (IV) actuels.

C#
public override System.Security.Cryptography.ICryptoTransform CreateDecryptor();

Retours

Objet déchiffreur symétrique.

Remarques

Cette méthode déchiffre un message chiffré créé à l’aide de la CreateEncryptor surcharge avec la même signature.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

CreateDecryptor(Byte[], Byte[])

Source:
RijndaelManaged.cs
Source:
RijndaelManaged.cs
Source:
RijndaelManaged.cs

Crée un objet déchiffreur Rijndael symétrique avec le Key spécifié et le vecteur d'initialisation (IV).

C#
public override System.Security.Cryptography.ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV);
C#
public override System.Security.Cryptography.ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV);

Paramètres

rgbKey
Byte[]

Clé secrète à utiliser pour l'algorithme symétrique. La taille de la clé doit être de 128, 192 ou 256 bits.

rgbIV
Byte[]

Vecteur d'initialisation (IV) à utiliser pour l'algorithme symétrique.

Retours

Objet déchiffreur Rijndael symétrique.

Exceptions

Le paramètre rgbKey a la valeur null.

- ou -

Le paramètre rgbIV a la valeur null.

La valeur de la propriété Mode n'est ni ECB, CBC ni CFB.

Exemples

L’exemple de code suivant montre comment utiliser la CreateDecryptor méthode pour déchiffrer les messages encodés avec le même paramètre de signature.

C#
using System;
using System.IO;
using System.Security.Cryptography;

namespace RijndaelManaged_Example
{
    class RijndaelExample
    {
        public static void Main()
        {
            try
            {

                string original = "Here is some data to encrypt!";

                // Create a new instance of the RijndaelManaged
                // class.  This generates a new key and initialization
                // vector (IV).
                using (RijndaelManaged myRijndael = new RijndaelManaged())
                {

                    myRijndael.GenerateKey();
                    myRijndael.GenerateIV();
                    // Encrypt the string to an array of bytes.
                    byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);

                    // Decrypt the bytes to a string.
                    string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

                    //Display the original data and the decrypted data.
                    Console.WriteLine("Original:   {0}", original);
                    Console.WriteLine("Round Trip: {0}", roundtrip);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }
        static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");
            byte[] encrypted;
            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;

                // Create an encryptor to perform the stream transform.
                ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {

                            //Write all data to the stream.
                            swEncrypt.Write(plainText);
                        }
                    }

                    encrypted = msEncrypt.ToArray();
                }
            }

            // Return the encrypted bytes from the memory stream.
            return encrypted;
        }

        static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;

                // Create a decryptor to perform the stream transform.
                ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }

            return plaintext;
        }
    }
}

Remarques

Cette méthode déchiffre un message chiffré créé à l’aide de la CreateEncryptor surcharge avec la même signature.

La rgbkey taille doit être de 128, 192 ou 256 bits.

Voir aussi

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1