Export (0) Print
Expand All

MaskGenerationMethod Class

Represents the abstract class from which all mask generator algorithms must derive.

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)] 
public ref class MaskGenerationMethod abstract
/** @attribute ComVisibleAttribute(true) */ 
public abstract class MaskGenerationMethod
ComVisibleAttribute(true) 
public abstract class MaskGenerationMethod
Not applicable.

Derived classes of MaskGenerationMethod compute masks that are used in key exchange algorithms such as Optimal Asymmetric Encryption Padding (OAEP).

This class is used by implementations of key exchange algorithms only for mask generation. Application code does not use this class directly.

The following code example demonstrates how to derive from the MaskGenerationMethod class.

#using <System.dll>

using namespace System;
using namespace System::Security::Cryptography;

namespace Contoso
{
    ref class MaskGenerator: MaskGenerationMethod
    {
    private:
        String^ hashNameValue;

    public:
        // Initialize a mask to encrypt using the SHA1 algorithm.
        MaskGenerator()
        {
            hashNameValue = "SHA1";
        }

        // Create a mask with the specified seed.
        virtual array<Byte>^ GenerateMask(array<Byte>^ seed, int maskLength) override
        {
            HashAlgorithm^ hash;
            array<Byte>^ rgbCounter = gcnew array<Byte>(4);
            array<Byte>^ targetRgb = gcnew array<Byte>(maskLength);
            UInt32 counter = 0;
            for (int inc = 0; inc < targetRgb->Length; )
            {
                ConvertIntToByteArray(counter++, rgbCounter);
                hash = (HashAlgorithm^)CryptoConfig::CreateFromName(
                    hashNameValue);
                array<Byte>^ temp = gcnew array<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;
        }

    private:
        // Convert the specified integer to the byte array.
        void ConvertIntToByteArray(UInt32 source,
            array<Byte>^ targetBytes)
        {
            UInt32 remainder;
            int inc = 0;

            // Clear the array prior to filling it.
            Array::Clear(targetBytes, 0, targetBytes->Length);
            while (source > 0)
            {
                remainder = source % 256;
                targetBytes[ 3 - inc ] = (Byte)remainder;
                source = (source - remainder) / 256;
                inc++;
            }
        }
    };

    // This class demonstrates how to create the MaskGenerator class 
    // and call its GenerateMask member.
    ref class MaskGeneratorImpl
    {
    public:
        void static Work()
        {
            array<Byte>^ seed = gcnew array<Byte>(4){
                0x01,0x02,0x03,0x04};
            int length = 16;
            MaskGenerator^ maskGenerator = gcnew MaskGenerator;
            array<Byte>^ mask = maskGenerator->GenerateMask(seed,
                length);
            Console::WriteLine("Generated the following mask:");
            Console::WriteLine(System::Text::Encoding::
                ASCII::get()->GetString(mask));
            Console::WriteLine("This sample completed successfully;"
                " press Enter to exit.");
            Console::ReadLine();
        }
    };
}

void main()
{
    Contoso::MaskGeneratorImpl::Work();
}

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

import System.*;
import System.Security.Cryptography.*;

class MaskGenerator extends System.Security.Cryptography.MaskGenerationMethod
{
    private String hashNameValue;

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

    // Create a mask with the specified seed.
    public ubyte[] GenerateMask(ubyte seed[], int maskLength)
    {
        HashAlgorithm hash;
        ubyte rgbCounter[] = new ubyte[4];
        ubyte targetRgb[] = new ubyte[maskLength];
        int counter = 0;
        for (int inc = 0; inc < targetRgb.get_Length(); ) {
            ConvertIntToByteArray(counter++, rgbCounter);
            hash = ((HashAlgorithm)CryptoConfig.CreateFromName(hashNameValue));
            ubyte temp[] = new ubyte[4 + seed.get_Length()];
            Buffer.BlockCopy(rgbCounter, 0, temp, 0, 4);
            Buffer.BlockCopy(seed, 0, temp, 4, seed.get_Length());
            hash.ComputeHash(temp);

            if (targetRgb.get_Length() - inc > hash.get_HashSize() / 8) {
                Buffer.BlockCopy(hash.get_Hash(), 0, targetRgb, inc, 
                    hash.get_Hash().get_Length());
            }
            else {
                Buffer.BlockCopy(hash.get_Hash(), 0, targetRgb, inc, 
                    targetRgb.get_Length() - inc);
            }
            inc += hash.get_Hash().get_Length();
        }
        return targetRgb;
    } //GenerateMask

    // Convert the specified integer to the byte array.
    private void ConvertIntToByteArray(int sourceInt, ubyte targetBytes[])
    {
        int remainder;
        int inc = 0;
        // Clear the array prior to filling it.
        Array.Clear(targetBytes, 0, targetBytes.get_Length());
        while (sourceInt > 0) {
            remainder = sourceInt % 256;
            targetBytes.set_Item((3 - inc), (Int32)remainder);
            sourceInt = (sourceInt - remainder) / 256;
            inc++;
        }
    } //ConvertIntToByteArray
} //MaskGenerator

// This class demonstrates how to create the MaskGenerator class 
// and call its GenerateMask member.
class MaskGeneratorImpl
{
    public static void main(String[] args)
    {
        ubyte seed[] = new ubyte[] { 0x1, 0x2, 0x3, 0x4 };
        int length = 16;
        MaskGenerator maskGenerator = new MaskGenerator();
        ubyte mask[] = maskGenerator.GenerateMask(seed, length);
        Console.WriteLine("Generated the following mask:");
        Console.WriteLine(System.Text.Encoding.get_ASCII().GetString(mask));
        Console.WriteLine("This sample completed successfully; " 
            + "press Enter to exit.");
        Console.ReadLine();
    } //main
} //MaskGeneratorImpl
//
// This sample produces the following output:
//
// Generated the following mask:
// ?"TFd(?~OtO?
// This sample completed successfully; press Enter to exit.

System.Object
  System.Security.Cryptography.MaskGenerationMethod
     System.Security.Cryptography.PKCS1MaskGenerationMethod

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft