Lire en anglais

Partager via


MaskGenerationMethod Classe

Définition

Représente la classe abstraite dont doivent dériver tous les algorithmes du générateur de masque.

C#
public abstract class MaskGenerationMethod
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public abstract class MaskGenerationMethod
C#
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class MaskGenerationMethod
Héritage
MaskGenerationMethod
Dérivé
Attributs

Exemples

L’exemple de code suivant montre comment dériver de la MaskGenerationMethod classe .

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

namespace Contoso
{
    class MaskGenerator : System.Security.Cryptography.MaskGenerationMethod
    {
        private String HashNameValue;

        // Initialize a mask to encrypt using the SHA256 algorithm.
        public MaskGenerator() 
        {
            HashNameValue = "SHA256";
        }

        // Create a mask with the specified seed.
        override public byte[] GenerateMask(byte[] seed, int maskLength)
        {
            HashAlgorithm hash;
            byte[] rgbCounter = new byte[4];
            byte[] targetRgb = new byte[maskLength];
            uint counter = 0;

            for (int inc = 0; inc < targetRgb.Length; )
            {
                ConvertIntToByteArray(counter++, ref rgbCounter);
                hash = (HashAlgorithm)
                    CryptoConfig.CreateFromName(HashNameValue);

                byte[] temp = new byte[4 + seed.Length];
                Buffer.BlockCopy(rgbCounter, 0, temp, 0, 4);
                Buffer.BlockCopy(seed, 0, temp, 4, seed.Length);
                hash.ComputeHash(temp);

                if (targetRgb.Length - inc > hash.HashSize/8) 
                {
                    Buffer.BlockCopy(
                        hash.Hash,
                        0,
                        targetRgb,
                        inc,
                        hash.Hash.Length);
                }
                else
                {
                    Buffer.BlockCopy(
                        hash.Hash,
                        0,
                        targetRgb,
                        inc,
                        targetRgb.Length - inc);
                }
                inc += hash.Hash.Length;
            }
            return targetRgb;
        }

        // Convert the specified integer to the byte array.
        private void ConvertIntToByteArray(
            uint sourceInt,
            ref byte[] targetBytes)
        {
            uint remainder;
            int inc = 0;

            // Clear the array prior to filling it.
            Array.Clear(targetBytes, 0, targetBytes.Length);

            while (sourceInt > 0) 
            {
                remainder = sourceInt % 256;
                targetBytes[3 - inc] = (byte) remainder;
                sourceInt = (sourceInt - remainder)/256;
                inc++;
            }
        }
    }
// This class demonstrates how to create the MaskGenerator class 
// and call its GenerateMask member.
    class MaskGeneratorImpl
    {
      public static void Main(string[] args)
      {
          byte[] seed = new byte[] {0x01, 0x02, 0x03, 0x04};
          int length = 16;
          MaskGenerator maskGenerator = new MaskGenerator();
          byte[] mask = maskGenerator.GenerateMask(seed, length);
          Console.WriteLine("Generated the following mask:");
          Console.WriteLine(System.Text.Encoding.ASCII.GetString(mask));

          Console.WriteLine("This sample completed successfully; " +
                "press Enter to exit.");
          Console.ReadLine();
      }
  }
}
//
// This sample produces the following output:
//
// Generated the following mask:
// ?"TFd(?~OtO?
// This sample completed successfully; press Enter to exit.

Remarques

Classes dérivées de masques de MaskGenerationMethod calcul utilisés dans les algorithmes d’échange de clés tels que le remplissage optimal du chiffrement asymétrique (OAEP).

Cette classe est utilisée par les implémentations d’algorithmes d’échange de clés uniquement pour la génération de masque. Le code d’application n’utilise pas cette classe directement.

Constructeurs

MaskGenerationMethod()

Initialise une nouvelle instance de la classe MaskGenerationMethod.

Méthodes

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
GenerateMask(Byte[], Int32)

En cas de substitution dans une classe dérivée, génère un masque de la longueur spécifiée à l’aide de la valeur initiale aléatoire spécifiée.

GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

S’applique à

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